Having trouble storing results from a Javascript into Global variable.

0 favourites
  • 3 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • I am creating a game to create math problems. I pass the number of digits for each operand plus the operator (+,-,x,/). The script generates two random operands and the adds them (for now).

    Here is the calling sequence:

    runtime.globalVars.ans=genproblem(runtime.globalVars.n1Digits,runtime.globalVars.n2Digits, runtime.globalVars.operator);

    The header for the JS:

    function genproblem(n1digits, n2digits, operator)

    It does what it is supposed to and returns the sum of the two numbers. I also need the two operands stored in C3 global variables in order to display the entire problem.

    I read this:

    globalVars

    An object with a property for each global variable on an event sheet in the project. For example if the project has a global variable on an event sheet named Score, then runtime.globalVars.Score provides access to the global variable from script.

    In some cases, event variables may have names that aren't valid JavaScript identifiers. In this case you can use the string property syntax, e.g. runtime.globalVars["Score"].

    I added the following:

    runtime.globalVars.n1 = n1;

    Then I get a runtime error:

    action.js:31 Unhandled exception running script Event sheet 1, event 1, action 2: ReferenceError: runtime is not defined

    at genproblem (Script.js:17)

    at EventSheet1_Event1_Act2 (scriptsInEvents.js:8)

    at Action.RunUserScript (action.js:31)

    at EventBlock._RunActions_ReturnValue (eventBlock.js:26)

    at EventBlock._RunAndBlock (eventBlock.js:23)

    at EventBlock.Run (eventBlock.js:20)

    at EventSheet._ExecuteTrigger (eventSheet.js:18)

    at EventSheet._TriggerForClass (eventSheet.js:12)

    at EventSheet._Trigger (eventSheet.js:11)

    at EventSheetManager._Trigger (eventSheetManager.js:13)

    RunUserScript action.js:31

    async function (async)

    RunUserScript action.js:31

    _RunActions_ReturnValue eventBlock.js:26

    _RunAndBlock eventBlock.js:23

    Run eventBlock.js:20

    _ExecuteTrigger eventSheet.js:18

    _TriggerForClass eventSheet.js:12

    _Trigger eventSheet.js:11

    _Trigger eventSheetManager.js:13

    Trigger runtime.js:74

    Trigger sdkInstanceBase.js:2

    _OnMouseDown instance.js:5

    _CheckButtonChange instance.js:4

    _CheckButtonChanges instance.js:3

    _OnPointerDown instance.js:3

    (anonymous) instance.js:2

    _FireAndWaitAsyncSequential handler.js:9

    dispatchEventAndWaitAsyncSequential dispatcher.js:5

    _OnEventFromDOM runtime.js:16

    _OnMessageFromDOM runtime.js:15

    _PostToRuntimeMaybeSync domHandler.js:2

    _OnPointerEvent runtimeDomEvents.js:29

    (anonymous) runtimeDomEvents.js:17

    action.js:32 Tip: run this to highlight in Construct the last script that had an error: goToLastErrorScript()

    So the question is how do I update global variables from a script file?

    Here is the file: drive.google.com/file/d/1yeHVnM6CVoJ23DN-1P0_K_WyxsKDse2K/view

    Thanks,

    Tom

  • The easiest fix is to pass the runtime itself in method parameters:

    runtime.globalVars.ans=genproblem(runtime.globalVars.n1Digits,runtime.globalVars.n2Digits, runtime.globalVars.operator, runtime);
    
    
    function genproblem(n1digits, n2digits, operator, runtime)
    {
     .......
    }
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks DOP2000,

    That worked perfectly. Wish I had known that

    a few days ago.

    Thanks again!

    Tom

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)