How do I dynamically point to a variable?

0 favourites
  • 5 posts
From the Asset Store
Connect the dots in the correct order and draw happy animals!
  • Hey all,

    I am attempting to perform checks on a sequence of string variables, for argument's sake let's call them object.variable1 - 4.

    I am trying to check through these variables in a for loop where the loop index serves as the number next to the variable. Is there a way to accomplish this? I tried to do reference something along the lines of

    object.variable+(loopindex+1)[/code:12h13u1e] to get construct2 to print the data inside object.variable1, but as you can imagine, I was met with the error "'variable' is not an expression in object". 
    
    Is there a correct way to do this? 
    
    Cheers all
  • Assuming Variable = a string and a global

    + One more global variable containing the basestriing ... lets call it 'Base'

    Condition > System > for ... name "index" ... start any ... end any

    Action > System > Set Value .. Variable=Variable .. Value = Base&str(loopindex("index"))

    str() converts a number to a string

    & is the operator to combine two strings

    loopindex(name) = the loopindex of the 'for' as an expression

    I probaly understood the question wrong, i dont see any use in setting a variable this way in a loop. A whole loop happens inside 1 tick.

    I you want to add 'x' to variable every tick. Then you just use the tick loop.

    In this case you need a globale 'counter' (wich is a number)

    Condition > empty or > System > Every tick

    Action > System > add to > Variable = counter .. value = x

    Action > System > Set Value .. Variable = Variable .. Value = Base&str(counter)

  • Thanks, but I think I phrased the question incorrectly. Let's say that object.variable1 = "apples" and object.variable2 = "oranges". I am trying to use a for loop to process through object.variable1-4 in a way that I could say object.variable&indexloop would reference the data inside each variable, so on the first loop it would handle "apples" and in the second loop it would handle "oranges" and so on and so forth.

  • You can't generate variable names dynamically. You could use an array or dictionary for your data.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can't generate variable names dynamically. You could use an array or dictionary for your data.

    Ah I see, thanks

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