How do I dynamically point to a variable?

Get help using Construct 2

Post » Mon Jun 13, 2016 12:01 pm

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
Code: Select all
object.variable+(loopindex+1)
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
B
5
Posts: 29
Reputation: 733

Post » Mon Jun 13, 2016 1:53 pm

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)
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Jun 13, 2016 3:00 pm

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.
B
5
Posts: 29
Reputation: 733

Post » Mon Jun 13, 2016 3:21 pm

You can't generate variable names dynamically. You could use an array or dictionary for your data.
ImageImageImage
B
71
S
22
G
259
Posts: 3,793
Reputation: 143,702

Post » Mon Jun 13, 2016 4:30 pm

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


Ah I see, thanks :)
B
5
Posts: 29
Reputation: 733


Return to How do I....?

Who is online

Users browsing this forum: maddy467, work3, Yahoo [Bot] and 8 guests