Converting string to object name to return object ID

GunnyBoy

Member
I have objects used for the player to collide into to change rooms called "o_transition". Each object has a unique with a naming convention (current_room_to_target_room) and has a a variable for the current room it's in and the target room to move to in the creation code. I'm trying to find a way to automatically build the unique ID of the target rooms transition object using those variables but I'm having some trouble. It currently works fine if I just the objects unique ID name in a variable, but if I try to build the unique as a string from variables and use "asset_get_index" it doesn't work.

Here's a bit of the code:
Code:
_current_room = room;
_target_room = rm_2;
_target_spawn = rm_2_to_rm_1;
_target_spawn_test = asset_get_index(string(room_get_name(_target_room) + "_to_" + room_get_name(_current_room)));
"_target_spawn" returns "100045" which works fine with the rest of my code but "_target_spawn_test" returns "-1" even though the code inside "asset_get_index" returns exactly as "rm_2_to_rm_1"

So my thinking is both ways should return the ID, but I can't seem to get the second one to work. Maybe "asset_get_index" isn't what I should be using to return the ID of named object? My end goal here is be able to build a string from variables that read the same as the object it's named as.
 

TsukaYuriko

☄️
Forum Staff
Moderator
_target_spawn's value is an instance ID (100000+), not an object ID. You say it's an object, but that can't be right. Its ID would be way lower. It sounds like it's a variable that stores an instance ID, in which case asset_get_index will not help you as it is for objects (or other assets), not variables. For reference, the corresponding function for "variable name to variable" is the variable_instance_* family of functions.


A better approach would probably be to store these associations in a 2D array. Current room on one axis, target room on the other. The value of the cell at that position is whatever transition you'd want that association to produce.
 

GunnyBoy

Member
_target_spawn's value is an instance ID (100000+), not an object ID. You say it's an object, but that can't be right. Its ID would be way lower. It sounds like it's a variable that stores an instance ID, in which case asset_get_index will not help you as it is for objects (or other assets), not variables. For reference, the corresponding function for "variable name to variable" is the variable_instance_* family of functions.


A better approach would probably be to store these associations in a 2D array. Current room on one axis, target room on the other. The value of the cell at that position is whatever transition you'd want that association to produce.
Is there a way to get an instance ID from a string? The string would be unique since every one of those objects will be named manually and uniquely.
 

FrostyCat

Member
Named instances cannot be fetched using asset_get_index(). You need to scrap your name-based method, and instead use another approach based on variables in the o_transition instances.
GML:
///@func o_transition_find(from, to)
///@param from
///@param to
with (o_transition) {
    if (_current_room == argument0 && _target_room == argument1) return id;
}
return noone;
 

GunnyBoy

Member
Named instances cannot be fetched using asset_get_index(). You need to scrap your name-based method, and instead use another approach based on variables in the o_transition instances.
GML:
///@func o_transition_find(from, to)
///@param from
///@param to
with (o_transition) {
    if (_current_room == argument0 && _target_room == argument1) return id;
}
return noone;
That was basically my backup plan if this wasn't going to work out, I was just trying to automate as much of as I could.
 
Top