0 Favourites

Referring to global variable by name - possible?

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

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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'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)
Similar Topics Posts Views Last Post
Unread hot topic
102 6,404
Ashley's avatar
Ashley Construct Team Founder
Unread hot topic
58 5,442
deadeye's avatar
deadeye
Unread hot topic
59 5,412
CrudeWall's avatar
CrudeWall