Dynamic varibale retrieval

S

stephen2017

Guest
Hi all,

Going in a different direction to try to solve a problem I have. Is there a way to dynamically retrieve a variable. Will explain what I need to do.

In my creation code I have this.
var question2answer = obj_cat2; // this is setting an object to one of a set of questions. There are 10 questions at the moment but want to add more.

What I need to do is get an obj variable to = that obj_cat2 so I can create an instance of that object. this is the way I am doing it but I am going about it the wrong way.

obj = "question"+m[0]+"answer" // m[0] being a random number from an array so it selects a random question.

I think it is just setting obj to a string though and I need it to pull that variable as above. Any help is appreciated as this is buggin me now lol.

TIA
 
S

stephen2017

Guest
Yes but when I do it this way it does not work as it it string to find "question"+m[0]+"answer" as the obj and not obj_cat2 which I need.

instance_create(200,200,asset_get_index("question"+m[0]+"answer"))

Trying to find how to work around.
 
S

stephen2017

Guest
Oh bugger I deleted that did have it in. but even with that in coming up with non exisiting object error because I think its looking for the string I placed in there and no the object in the variable
 
D

Dark

Guest
First off you have "var" before question2answer (and probably the other variables) which means the variable question2answer is local to that code and won't exist anymore after it's done.
Second, why not just use an array? For example,
Code:
obj = questionanswer[2]
or
Code:
obj = questionanswer[m[0]]
 
S

stephen2017

Guest
I found the problem. Any dynamic string call in instance_create looks for the string as an object. Not the variable the string you have in there is (and that was my problem. So I found a workaround. I created a switch case in the creation code and set the objects directly there. this way I do not have to do it dynamically and the instance call works.
 

TheouAegis

Member
You can use a ds_map instead and it should run a bit faster than the switch, but the switch should be okay still.
 
Top