Referring to global variable by name - possible?

0 favourites
  • 6 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.
  • Is it possible in event sheet to refer to a global variable by string and get the value of this global variable?

  • 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?...

  • 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 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.

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

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