Search Unity

Getting errors after converting from C# to js[RESOLVED]

Discussion in 'Scripting' started by MrHammy36, Nov 22, 2017.

  1. MrHammy36

    MrHammy36

    Joined:
    Apr 4, 2017
    Posts:
    31
    I purchased a code converter from the asset store to convert most of my scripts from C# to JS for implementation of In App Purchases on another App store not listed in Unity. However when I tested it with one of my cs files after converting it to JS this one in general

    Code (csharp):
    1.  
    2. import System.Collections.Generic;
    3. import UnityEngine.UI;
    4. import GameFramework.GameStructure;
    5. namespace CSFramework {
    6.     /// <summary>
    7.     /// A base UI class you can use for your slot game.
    8.     /// It is not necessary to inherit this class
    9.     /// </summary>
    10.     class BaseSlotGameUI extends MonoBehaviour
    11.     {
    12.         var slot: CustomSlot;
    13.        
    14.         Text textMoney, textRoundCost, textRound, textIncome, textBet, textFreeSpin, textBonus, debugText;
    15.         GameObject goFreeSpin, goBonus;
    16.         public var betList: List.<int> = new List.<int>() {1, 10, 100};
    17.         var targetFrameRate: int = 70;
    18.         private var betIndex: int = 0;
    19.  
    20.         protected virtual function Awake (): void {
    21.             slot.callbacks.onActivated.AddListener(OnActivated);
    22.             slot.callbacks.onDeactivated.AddListener(OnDeactivated);
    23.             slot.callbacks.onRoundStart.AddListener(OnRoundStart);
    24.             slot.callbacks.onReelStart.AddListener(OnReelStart);
    25.             slot.callbacks.onReelStop.AddListener(OnReelStop);
    26.             slot.callbacks.onProcessHit.AddListener(OnProcessHit);
    27.             slot.callbacks.onRoundComplete.AddListener(OnRoundComplete);
    28.             slot.callbacks.onSlotStateChange.AddListener(OnSlotStateChange);
    29.             slot.callbacks.onSlotModeChange.AddListener(OnSlotModeChange);
    30.             slot.callbacks.onLineSwitch.AddListener(OnLineSwitch);
    31.             var TotalCoins: int = PlayerPrefs.GetInt("P0.TotalCoins");
    32.             PlayerPrefs.SetInt("game1_balance", TotalCoins);
    33.             GameManager.Instance.Player.UpdatePlayerPrefs();
    34.             PlayerPrefs.Save();
    35.             slot.Activate();
    36.             Initialize();
    37.         }
    38.  
    39.         virtual function Initialize (): void {
    40.             Application.targetFrameRate = targetFrameRate;
    41.             RefreshMoney();
    42.             RefreshBet();
    43.             RefreshRoundCost();
    44.             goFreeSpin.SetActive(false);
    45.             goBonus.SetActive(false);
    46.             slot.SetBet(betList[betIndex]);
    47.         }
    48.  
    49.         /// <summary>
    50.         /// A callback method subscribed to CS's onActivated event.
    51.         /// It is invoked when CS is activated, precisely after an intro animation event(parallel progression).
    52.         /// </summary>
    53.         virtual function OnActivated (): void { ShowDebugText("onActivated"); }
    54.  
    55.         /// <summary>
    56.         /// A callback method subscribed to CS's onDeactivated event.
    57.         /// It is invoked when CS is deactivated, after an Out-Transition is completed.
    58.         /// </summary>
    59.         virtual function OnDeactivated (): void { ShowDebugText("onDeactivated"); }
    60.  
    61.         /// <summary>
    62.         /// A callback method subscribed to CS's onRoundStart event.
    63.         /// It is invoked at the start of every round.
    64.         /// </summary>
    65.         virtual function OnRoundStart (): void {
    66.             ShowDebugText("onRoundStart");
    67.             RefreshRoundInfo();
    68.         }
    69.  
    70.         /// <summary>
    71.         /// A callback method subscribed to CS's onStartSpin event.
    72.         /// It is invoked each time a reel starts.
    73.         /// </summary>
    74.         virtual function OnReelStart (info: ReelInfo): void {
    75.            
    76.         PlayerPrefs.SetInt("P0.TotalCoins", int.Parse(textMoney.text));
    77.            
    78.             ShowDebugText("onStartSpin");
    79.             if (info.isFirstReel) {
    80.                 RefreshRoundInfo();
    81.                 RefreshMoney();
    82.             }
    83.  
    84.         }
    85.        
    86.         /// <summary>
    87.         /// A callback method subscribed to CS's onStopReel event.
    88.         /// It is invoked each time a reel stops.
    89.         /// ReelInfo contains information like which reel was stopped.
    90.         /// </summary>
    91.         virtual function OnReelStop (info: ReelInfo): void { ShowDebugText("onStopReel"); }
    92.  
    93.         /*
    94.         /// <summary>
    95.         /// A callback method subscribed to CS's onRoundInterval event.
    96.         /// It is invoked when all the reels stop spinning and before Hit Check starts.
    97.         /// </summary>
    98.         virtual function OnRoundInterval (): void { ShowDebugText("onRoundInterval"); }
    99.         */
    100.  
    101.         /// <summary>
    102.         /// A callback method subscribed to CS's onProcessHit event.
    103.         /// It is invoked when a Hit Check is performed on a line or a scatter and if it is successful.
    104.         /// Information like which symbol holders were the subjects of the hit, how many chains the hit made and etc etc
    105.         /// are passed with HitInfo.
    106.         ///
    107.         /// HitInfo also has a reference to a DOTween sequence(<see cref="DG.Tweening.DOTween.Sequence"/> that plays highlighting effects for the hit symbols.
    108.         ///
    109.         /// </summary>
    110.         virtual function OnProcessHit (info: HitInfo): void {
    111.             PlayerPrefs.SetInt("P0.TotalCoins", int.Parse(textMoney.text));
    112.             GameManager.Instance.Player.UpdatePlayerPrefs();
    113.             PlayerPrefs.Save();
    114.             ShowDebugText("onProcessHit");
    115.             if (info.hitSymbol.payType == Symbol.PayType.Normal) RefreshMoney();
    116.             RefreshRoundInfo();
    117.         }
    118.  
    119.         /// <summary>
    120.         /// An UnityAction subscribed to CS's onRoundComplete event.
    121.         /// It is invoked at the end of every round.
    122.         /// </summary>
    123.         virtual function OnRoundComplete (): void { ShowDebugText("onRoundComplete"); }
    124.  
    125.         /// <summary>
    126.         /// A callback method subscribed to CS's onSlotStateChange event.
    127.         /// It is invoked when CS's state changes.
    128.         /// </summary>
    129.         virtual function OnSlotStateChange (): void { ShowDebugText("onSlotStateChange"); }
    130.  
    131.         /// <summary>
    132.         /// A callback method subscribed to CS's onSlotModeChange event.
    133.         /// It is invoked when CS's mode changes(e.g going to free spin mode).
    134.         /// </summary>
    135.         virtual function OnSlotModeChange (info: SlotModeInfo): void {
    136.             ShowDebugText("onSlotModeChange");
    137.             if (info.lastMode == slot.modes.freeSpinMode) ToggleFreeSpin(false);
    138.             if (info.lastMode == slot.modes.bonusMode) ToggleBonus(false);
    139.             if (slot.currentMode == slot.modes.freeSpinMode) ToggleFreeSpin(true);
    140.             if (slot.currentMode == slot.modes.bonusMode) ToggleBonus(true);
    141.             RefreshFreeSpin();
    142.             RefreshBonus();
    143.         }
    144.  
    145.         /// <summary>
    146.         /// An UnityAction subscribed to CS's onLineSwitch event.
    147.         /// It is invoked every time a line is turned on/off.
    148.         /// The current state of the line can be checked from the passed LineInfo.
    149.         /// </summary>
    150.         virtual function OnLineSwitch (info: LineInfo): void {
    151.             ShowDebugText("onLineSwitch");
    152.             RefreshRoundCost();
    153.             RefreshRoundInfo();
    154.         }
    155.  
    156.         virtual function ShowDebugText (detail: String): void {
    157.             if (!debugText) return;
    158.             debugText.text = "Last Callback: " + detail;
    159.         }
    160.        
    161.         virtual function RefreshRoundCost (): void { textRoundCost.text = "" + slot.gameInfo.roundCost; }
    162.         virtual function RefreshMoney (): void { textMoney.text = "" + slot.gameInfo.balance; GameManager.Instance.Player.RemoveCoins(int.Parse(textIncome.text)); }
    163.         virtual function RefreshBet (): void { textBet.text = "" + slot.gameInfo.bet; }
    164.        
    165.         virtual function RefreshRoundInfo (): void {
    166.             textRound.text = "Round. " + (slot.gameInfo.roundsCompleted + 1);
    167.             textIncome.text = "( " + slot.gameInfo.roundBalance + " )";
    168.             RefreshFreeSpin();
    169.             RefreshBonus();
    170.         }
    171.  
    172.         virtual function ToggleFreeSpin (enable: boolean): void { goFreeSpin.SetActive(enable); }
    173.         virtual function RefreshFreeSpin (): void { textFreeSpin.text = "" + slot.gameInfo.freeSpins; }
    174.         virtual function ToggleBonus (enable: boolean): void { goBonus.SetActive(enable); }
    175.         virtual function RefreshBonus (): void { textBonus.text = "" + slot.gameInfo.bonuses; }
    176.  
    177.         virtual function SetBet (index: int): boolean {
    178.             if (!slot.isIdle || index < 0 || index >= betList.Count) return false;
    179.             betIndex = index;
    180.             slot.SetBet(betList[betIndex]);
    181.             RefreshBet();
    182.             RefreshRoundCost();
    183.             var true: return;
    184.         }
    185.  
    186.         virtual function RaiseBet (): void { SetBet(betIndex + 1); }
    187.         virtual function LowerBet (): void { SetBet(betIndex - 1); }
    188.  
    189.         virtual function EnableNextLine (): void { slot.lineManager.EnableNextLine(); }
    190.         virtual function DisableCurrentLine (): void { slot.lineManager.DisableCurrentLine(); }
    191.     }
    192. }
    I get these errors from Unity

    Assets/CustomSlots/Script/BaseSlotGameUI.js(9,9): BCE0044: expecting }, found 'class'.
    Assets/CustomSlots/Script/BaseSlotGameUI.js(9,15): BCE0044: expecting EOF, found 'BaseSlotGameUI'.

    Nothing was changed in this script it was just converted from C# to JS, Can anyone help fix this issue so I know whats going on?


    Just on a side note it says Csharp but it is JavaScript.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't believe UnityScript supports namespaces. (I'm not very experienced with UnityScript though)
     
    Last edited: Nov 22, 2017
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    ^ This.

    https://unity3d.com/learn/tutorials/topics/scripting/namespaces
    If you click on the JS tab, it says:
     
  4. MrHammy36

    MrHammy36

    Joined:
    Apr 4, 2017
    Posts:
    31
    Ok, Thank you @TaleOf4Gamers that helped alot, now my only other code issue I am running into is these two lines
    Code (csharp):
    1.  
    2. Text textMoney, textRoundCost, textRound, textIncome, textBet, textFreeSpin, textBonus, debugText;
    3. GameObject goFreeSpin, goBonus;
    the converter doesn't know how to handle them and I am not sure if it is coded right.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  6. MrHammy36

    MrHammy36

    Joined:
    Apr 4, 2017
    Posts:
    31
    Most of this code is C# @Joe-Censored I am converting it from C# to Javascript. Removing the UnityScripting Tag because I think it's throwing people off.
     
  7. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Is this a script that's meant to be read and executed by Unity? If so, that's not really Javascript. It's UnityScript. Hence the confusion.

    It looks like JavaScript, even Unity calls it "JavaScript", but it's UnityScript. If it's a straight C# to JS tool (like, real actual JavaScript), I wouldn't get my hopes up about it working flawlessly in Unity without some fixes or even major reworking.

    I'd be careful with this. For one thing, Unity is sunsetting UnityScript, so UnityScript-based assets may not be long for this world. For another, I'd read the TRCs very carefully as certain platform maintainers can be pretty particular in what's allowed to go into your app.
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm not understanding anymore then. You said you were getting errors in Unity, which implies you're converting to UnityScript for use in Unity, and are just doing the common thing of misusing the term JavaScript when you mean UnityScript.

    Are you actually converting to UnityScript? (any file in a Unity project with .js file extension is UnityScript) Or are you converting to JavaScript? (meaning is this .js not intended for compilation by Unity and will be outside of any Unity project)
     
    TaleOf4Gamers likes this.
  9. MrHammy36

    MrHammy36

    Joined:
    Apr 4, 2017
    Posts:
    31
    OK, First of all I have to issue an apology to @Joe-Censored I was totally misunderstanding the diffference between java and unity script. The converter I purchased more then likely converts to Unity script So that is my bad on my fault. Secondly I have decided for now to scrap the Idea of converting to .js files and stick with the C# code that is in my current game because once UnityScript is fully gone at least it will be safe. Again sorry to everyone for getting confused on this.
     
    BlackPete likes this.