Access Global Variable with SDK from Custom Plugin

0 favourites
  • 7 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'm having a problem with a plugin not doing something I need. I have an idea for a fix, but I need to access a global variable "game_id" in the game.

    Is there a quick way to grab/change this variable with the C2 sdk?

    I read somewhere that I would need to use the system object, but i'm pretty sure that's for construct classic.

    I've tried: t = system.global('game_id');

    console.log(t);

    Obviously it doesn't work. Let me know if you can point me in the right direction.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Try accessing the array this.runtime.all_global_vars. It is a list of all global cr.eventvariable objects. You can learn more by browsing the source, but the important members of an event variable are:

    ev.name // variable name

    ev.vartype // the variable type, 0 = number, 1 = string

    ev.data // the value

    You can use this to look up and retrieve global variables by name, but note if you change a value, you *must* respect the vartype member, else you will break the runtime. (i.e. don't set 'data' to a number if vartype indicates it's a string.)

    Edit: moving to Javascript SDK forum.

  • Awesome. Here's what I have so far:

    var ev = this.runtime.all_global_vars;

    console.log(ev);

    So naturally I get an array.

    My question is, if I select an element by it's key [0], or [20],

    var ev = this.runtime.all_global_vars[2];

    console.log(ev.name);

    Does that key usually change whenever I add a new global variable or restart the game?

    If so, how would I select it by it's name? Or would this require a loop of some kind?

  • You'll have to loop through the array to look it up. However once you've got the result you can keep it (global variables don't move or change during the game, apart from their 'data' members.)

  • Thanks Ashley you rock.

  • I got it.

  • Taurian,

    You can access name of that variable, how do you access value of that variable?

    Ashley,

    Can you let me know structure of global variable, will make it easy to access particular thing from it.

    Thanks.

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