• 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!

Resource IDs are Numbers, Not Strings

samspade

Member
GM Version: GMS 2.2.5 and 2.3
Target Platform: All
Download: NA
Links:
YouTube Video

A Micro Tutorial for Someone New to Gamemaker and a Reference for the Rest of Us

Resources ids are numbers not strings. Knowing this will help you prevent some bugs and allow you to do things like create generic code that can load text or loop through resources.

Resources ids (or asset indexes depending upon how you want to think about it) are the names of the various resources in your resource tree (and also script functions) and will change color when you type them in GameMaker. These are numbers (currently always an integer equal to or greater than zero) and not strings. If you want to get the string form of a resource name you need to use one of the specific *_get_name(resource_id)—where * is replaced with the type of resource you want such as object_get_name(object) or sprite_get_name(sprite). If you want to use the string to get the index, you use asset_get_index(string).

So assume that you have the following resources in a project: an object named obj_player and a sprite named spr_player. You can do the following to get the string form:
GML:
obj_name = object_get_name(obj_player);
spr_name = sprite_get_name(spr_player);
And you could get the resource ids with:
GML:
obj_id = asset_get_index("obj_player");
spr_id = asset_get_index("spr_player");

At least one reason why is this important

Virtually all GameMaker functions that use resources/assets use the resource id/asset index, not the resource name as a string. And not only are resource ids numbers, but they are numbers set by GameMaker and can change as you add and remove resources. So if you want to use a GameMaker function like instance_create_layer or audio_play_sound or room_goto you need the resource id of those things. Most of the time you can simply type the resource name in the text editor without quotes and that will do it (e.g. instance_create_layer(0, 0, "Instances", obj_player), but there are circumstances where that doesn't work. For example, if you want to do something like loading in an external file that specifies what enemies to load, you can't use the resource id (the number) because that will change over time. Instead you would want to use the string name of that resource and then use asset_get_index to return the resource id so that you can use it with gamemaker functions. Or if you wanted to save out the various enemies in the room you would want to save the string name of those enemies (using object_get_name) and then when loading convert that string back to a resource id with asset_get_index.


 
Top