Referring to global variable by name - possible?

Get help using Construct 2

Post » Mon Jun 24, 2013 5:41 pm

Is it possible in event sheet to refer to a global variable by string and get the value of this global variable?
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,459

Post » Mon Jun 24, 2013 6:01 pm

Yes it can be used in expressions as a string...

>global variable "score" = 1000 ...add 1 to player lives

or player on collision with pickup ...add 50 to "score"

Is that what you mean?...
As long as I can move left, right and fire, I'm Happy...
B
42
S
15
G
11
Posts: 655
Reputation: 12,260

Post » Mon Jun 24, 2013 6:03 pm

Sorry to be unclear.

I am storing the name of the global variable in a text instance variable, and I want to retrieve the value of this global variable. Imagine pointer to pointer to value in C++.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,459

Post » Mon Jun 24, 2013 6:09 pm

I think the "dictionary" object may be what you need to use, it will give the value as a string or number...

quote from C2 manual...

Suppose the number 100 is stored with the key "score", and the string "Joe" stored with the key "name". The result storage looks like the following table:

"name": "Joe"
"score": 100

Retrieving the key "name" with Dictionary.Get("name") returns "Joe", and retrieving "score" likewise returns 100. Setting "score" to 50 will change the value for the key.

This is like storing data in instance variables or event variables, but since you can use strings as keys you can store any number of values.Pixel perfick2013-06-24 18:11:53
As long as I can move left, right and fire, I'm Happy...
B
42
S
15
G
11
Posts: 655
Reputation: 12,260

Post » Tue Jun 25, 2013 3:00 am

I know that I can use dictionary like that and yes dictionary is available for the whole project. But I want to refer to global variables.

It's easier to add global variables and their values on the get go with each event sheet, but for dictionary, I need to initialize it somewhere like in on layout start event first. I can work faster using global variable, unless you can suggest a better workflow for dictionary.
I got a game that you multiply, breath fire with two heads and brawl foes to oblivion with your clones: http://www.newgrounds.com/portal/view/660664 (use Chrome on Windows for best performance)

My sites:
http://twinblazar.deviantart.com
http://twinblazar.newgrounds.com
https://twitter.com/twinblazar
http://www.pixiv.net/member.php?id=15072448
B
30
S
11
G
11
Posts: 411
Reputation: 8,459

Post » Tue Jun 25, 2013 8:19 am

I'm not sure this is exactly what you need, but I needed to access global variables in some evaluations, but the variable name had to be a string parameter. So I wrote a mini-plugin doing just this, picking a global variable and returning its value by using a string as "variableName".
Here is the plugin and a sample capx describing how it works.
B
17
S
8
G
4
Posts: 461
Reputation: 6,087


Return to How do I....?

Who is online

Users browsing this forum: ErekT, JaisonAntoniazzi and 8 guests