Topic was automatically imported from the old Question2Answer platform.
Asked By
grymjack
I have a lot of dictionaries I keep in memory due to constant references. Is is possible to have ‘virtual’ variable names in an array to iterate through and do things with? You could store the variables themselves in the array, but if they are big, that could be expensive memory-wise.
var varnames = ['varname1','varname2','varname3','varname4']
for loop in range(0,varnames.size()):
var variable_name = varnames[loop]
if (variable_name == 'blah'):
## do something
var dictionary = {
'varname1' : 1,
'varname2' : 2,
'varname3' : 3,
'varname4' : 4
}
for variable_name in dictionary.keys():
if (variable_name == 'blah'):
## do something
Side note
For some reason classes load faster than dictionaries with the same data so you can also try that with get_property_list()
This has the same problem as the array. It gets the name of the variable, not the value of the variable.
func testvar():
var varname1 = 10
var varname2 = 20
var varname3 = 30
var varname4 = 40
var dictionary = {
'varname1' : 1,
'varname2' : 2,
'varname3' : 3,
'varname4' : 4
}
for variable_name in dictionary.keys():
if (variable_name == 10):
alert(variable_name,'10 Test')
grymjack | 2023-02-13 15:08
Most of the variables I am dealing with are dictionaries, so perhaps it would make more sense if I put it like this.
var varnames = ['varname1','varname2','varname3','varname4']
for loop in range(0,varnames.size()):
var variable_name = varnames[loop]
if (variable_name.class_member == 'blah'):
## do something
grymjack | 2023-02-13 15:10
Hmmm so is it that you want to get the variable name values of the class?
Or the value of the dictionary?
Because it’s easy to get a key value pair with
var value = dictionary[variable_name]
And for the class
var value = get(variable_name)
The reason for opting to loop the variable variant instead of an integer is because this line var variable_name = varnames[loop] creates unnecessary work for the garbage collection