Access Global Variable with SDK from Custom Plugin

For developers using the Construct 2 Javascript SDK

Post » Tue Jun 12, 2012 2:35 am

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.
Epic Space Online - Google it!
B
34
S
10
G
5
Posts: 165
Reputation: 6,637

Post » Tue Jun 12, 2012 2:47 pm

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.Ashley2012-06-12 14:47:56
Scirra Founder
B
357
S
214
G
72
Posts: 22,946
Reputation: 178,228

Post » Tue Jun 12, 2012 3:28 pm

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?
Epic Space Online - Google it!
B
34
S
10
G
5
Posts: 165
Reputation: 6,637

Post » Tue Jun 12, 2012 4:15 pm

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.)
Scirra Founder
B
357
S
214
G
72
Posts: 22,946
Reputation: 178,228

Post » Tue Jun 12, 2012 4:53 pm

Thanks Ashley you rock.
Epic Space Online - Google it!
B
34
S
10
G
5
Posts: 165
Reputation: 6,637

Post » Sun Sep 09, 2012 7:39 pm

I got it.Taurian2012-09-09 19:52:15
Epic Space Online - Google it!
B
34
S
10
G
5
Posts: 165
Reputation: 6,637


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests