Page 1 of 1

Set many variables at once

Posted: Wed Mar 24, 2021 2:37 pm
by amyren
If I have many bool variables named variable_1, variable_2, variable_3 etc. and I want to set all of them to either true or false.
Is there a simple way to do this without using a single line for each variable?
I tried a few things, but ended up making 50 lines just to get it done.

The example below does not work, since the StringToVariable command is not existing, but it shows what I was looking for.

Code: Select all

for i = 1 to 50
variablename$ = "varialble_"..i
StringToVariable(variablename$) = False

Re: Set many variables at once

Posted: Wed Mar 24, 2021 4:58 pm
by jPV
Just use tables:

Code: Select all

myvar = {}
For Local i = 1 To 50
    Local variablename$ = "variable_"..i
    myvar[variablename$] = False
Next
DebugPrint(myvar.variable_2)
DebugPrint(myvar["variable_2"])
Or skip the string indices and use numbers:

Code: Select all

myvar = {}
For Local i = 1 To 50
    myvar[i] = False
Next
DebugPrint(myvar[2])

Re: Set many variables at once

Posted: Wed Mar 24, 2021 7:19 pm
by amyren
Thank you:)