• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Way to reference dog_1 or dog_2 with variable "i" equaling "1" or "2"?

PE_Game

Member
Hi all. Lets say you have an object that defines dog_1, dog_2, dog_3. Could you create a referenceable dog_1 with something like this?:

var i = 1
current_dog = "dog_" + string(i)

I know the above code would simply create a string "dog_1" and clearly can't be referenced from anything else, but is there a way to make this referenceable without an array? Or is the answer simply, "use an array and a for loop dummy! Any other way would look ridiculous and be inefficient!"

edit: nevermind, guess it is not possible

Thanks,
 
Last edited:

Roldy

Member
Hi all. Lets say you have an object that defines dog_1, dog_2, dog_3. Could you create a referenceable dog_1 with something like this?:

var i = 1
current_dog = "dog_" + string(i)

I know the above code would simply create a string "dog_1" and clearly can't be referenced from anything else, but is there a way to make this referenceable without an array? Or is the answer simply, "use an array and a for loop dummy! Any other way would look ridiculous and be inefficient!"

Thanks,
Well for the most part it seems like you know you should probably just map these to an array.

But to answer your question:

If you are talking about OBJECTs named 'dog_1' and 'dog_2' then you can do something like:

GML:
var _i = 1;

current_dog  = asset_get_index("dog_" + string(_i));

_i = 2;

current_dog = asset_get_index("dog_" + string(_i));

// But you really would be better off simply doing an array

my_dog_indices = [dog_1, dog_2];

// OR

var _i = 1;
var _j = 2;
my_dog_indices = [asset_get_index("dog_" + string(_i)), asset_get_index("dog_" + string(_j))];

_i = 1;

current_dog  = my_dog_indices[_i - 1];

_i = 2;

current_dog = my_dog_indices[_i - 1];
 

FrostyCat

Redemption Seeker
Hi all. Lets say you have an object that defines dog_1, dog_2, dog_3. Could you create a referenceable dog_1 with something like this?:

var i = 1
current_dog = "dog_" + string(i)
GML:
current_dog = asset_get_index("dog_" + string(i));
edit: nevermind, guess it is not possible
You really need to stop being this sloppy with your research habits, and start reading the timestamps and exact context on things you reference.

That topic was talking about converting strings to variables, not to resource IDs. The variable_global_* and variable_instance_* functions were reintroduced in GMS 1.4 well after the topic was last updated, and asset_get_index has been available for resource IDs since GMS 1.2.
 

PE_Game

Member
GML:
current_dog = asset_get_index("dog_" + string(i));
You really need to stop being this sloppy with your research habits, and start reading the timestamps and exact context on things you reference.

That topic was talking about converting strings to variables, not to resource IDs. The variable_global_* and variable_instance_* functions were reintroduced in GMS 1.4 well after the topic was last updated, and asset_get_index has been available for resource IDs since GMS 1.2.
edit: the code below does indeed == 12 and works. I'll learn how to make this work with variables deep embedded in structs, likely with variable_struct_get. Thanks.

I may have not phrased my question correctly (or my efforts at trying your solution are failing).
asset_get_index appears to try and pull the index # of a list of assets, whereas I am trying to pull the value of a variable within an object.
variable_instance_get does seem like it is what I need, but in the code below, variable two_dogs fails to == 12 when I was hoping for that result. Am I using this incorrectly?


Create event

dog_1 = 5
dog_2 = 7


Step event

var i = 1;
if mouse_check_button_pressed(mb_left) == true
{
two_dogs = variable_instance_get(id, ("dog_" + string(i))) + variable_instance_get(id, ("dog_" + string(i+1)))
}


I get your point about sloppy research, but being so new I sort of struggled on how to search for this answer. I tried the following searches in the forum with no luck until resorting to google's algorithm, where the forum thread I linked seemed to discuss converting strings to variables, which is what I was looking for (albeit outdated). If you have tips on better search terms for my problem, please do let me know:
"create variable from strings"
"callable variable made up of strings"
"reference variable made up of strings"
 
Last edited:

Roldy

Member
variable_instance_get does seem like it is what I need, but in the code below, variable two_dogs fails to == 12 when I was hoping for that result. Am I using this incorrectly?

Create event

dog_1 = 5
dog_2 = 7


Step event

var i = 1;
if mouse_check_button_pressed(mb_left) == true
{
two_dogs = variable_instance_get(id, ("dog_" + string(i))) + variable_instance_get(id, ("dog_" + string(i+1)))
}
I am starting to think you may have put me on ignore from the time before when I was 'mean' to you.

The code you posted works for me.

GML:
// All in the create event

dog_1 = 5;
dog_2 = 7;

var i = 1;

two_dogs = variable_instance_get(id, ("dog_" + string(i))) + variable_instance_get(id, ("dog_" + string(i+1)));

show_debug_message(two_dogs);  // This outputs 12
Perhaps, like in the previous thread, you need to post more relevant code so we can help figure out what might be going wrong.
 
Top