GameMaker: Studio

GameMaker: Studio

View Stats:
Burgertime Jan 14, 2015 @ 12:46pm
Assigning sprites using strings?
I know you can change sprites by setting sprite_index to the index of the sprite you want, but is there any way to point at said index via a string (something like: sprite_index = "player_up")? The closest thing I could find online was execute_string, and that's apparently a function that was taken out when Studio was released.
< >
Showing 1-9 of 9 comments
Blind Jan 14, 2015 @ 12:49pm 
There's no need to use a string as all sprites, objects, rooms, scripts, backgrounds and so on have their own storage variable of their own name.

If you have a sprite "player_up" then setting sprite_index = player_up will work.
Burgertime Jan 14, 2015 @ 1:23pm 
Originally posted by BBX:
There's no need to use a string as all sprites, objects, rooms, scripts, backgrounds and so on have their own storage variable of their own name.

If you have a sprite "player_up" then setting sprite_index = player_up will work.

That's true, but what if I have many sprites with "_up" and "_down" variants and am trying to change them dynamically? In the RPG I'm making, I'm trying to make it so that the NPCs can turn towards me when I'm talking to them, however, I want to use just one NPC object who has a variable determining its type, that way when it's talked to from the right, for example, the code to turn them would be something like 'sprite_index = type + "_" + direction', except to my knowledge there's nothing in GameMaker allowing for this kind of code to happen (except for execute_string which, as I've already mentioned, has been removed)
Blind Jan 14, 2015 @ 1:39pm 
I don't think you can do that, I know you can in QBasic. You can try a combination of val(string(type)) which might work, but I'm sure it adds a space.

Another possible option (untested) is to make an ennumerator on start up that contains your facings, then you han have type.direction (as opposed to _) - might not work.
Sera Jan 14, 2015 @ 2:20pm 
it appears sprite_get_string() can fetch a sprite's resource name as string, and conversely asset_get_index() can use such a string to look up the sprite (though the latter's link is 404'ing for me on the online doc atm. The former[docs.yoyogames.com] should give a proper direction, tho).
Last edited by Sera; Jan 14, 2015 @ 2:20pm
Blind Jan 14, 2015 @ 2:41pm 
Ya, I have a backup of the documentation. They give an example of
var obj = asset_get_index("obj_Enemy_" + string(global.Level)); if obj > -1 { instance_create(random(room_width), random(room_height), obj); }

In this case instead of string(global.Level) you'd be using "direction".
Burgertime Jan 14, 2015 @ 7:50pm 
Originally posted by Zaron X:
it appears sprite_get_string() can fetch a sprite's resource name as string, and conversely asset_get_index() can use such a string to look up the sprite (though the latter's link is 404'ing for me on the online doc atm. The former[docs.yoyogames.com] should give a proper direction, tho).

Originally posted by BBX:
Ya, I have a backup of the documentation. They give an example of
var obj = asset_get_index("obj_Enemy_" + string(global.Level)); if obj > -1 { instance_create(random(room_width), random(room_height), obj); }

In this case instead of string(global.Level) you'd be using "direction".

asset_get_index() did the job perfectly, thank you very much for your help!
VegaBiggs Dec 4, 2016 @ 9:03am 
Originally posted by BBX:

asset_get_index("obj_Enemy_" + string(global.Level));

I know it's an old post but I really want to thank you, I was really stuck on that one! I love internet. And thanks ❄Goodyob❄ for asking the question in the first place.:steamhappy:
JopGamerNL May 20, 2021 @ 12:28am 
Originally posted by VegaBiggs:
Originally posted by BBX:

asset_get_index("obj_Enemy_" + string(global.Level));

I know it's an old post but I really want to thank you, I was really stuck on that one! I love internet. And thanks ❄Goodyob❄ for asking the question in the first place.:steamhappy:


ik this is now a really old post but funny enough this was the only correct method that fixed my problem so thank yall!!
ᴀɴα τααѕ Jul 1, 2024 @ 2:46am 
Originally posted by JopGamerNL:
Originally posted by VegaBiggs:

I know it's an old post but I really want to thank you, I was really stuck on that one! I love internet. And thanks ❄Goodyob❄ for asking the question in the first place.:steamhappy:


ik this is now a really old post but funny enough this was the only correct method that fixed my problem so thank yall!!
Ik this is now even older post but even funnily enough this thread was the solution to my problem. Thanks for the OP.
< >
Showing 1-9 of 9 comments
Per page: 1530 50