Discuss and collaborate on GML scripts

You are not logged in.

#1 2010-03-04 14:05:35

Dark Matter
Registered: 2010-03-04
Posts: 1


I've seen this question on the GMC quite a few times and I thought I might try to create a script for it. I made this, with the help of a friend. It returns the sprite index of the sprite with the name of argument0. This was the fastest way that I could think to do it.

**  Usage:
**      sprite_get_from_string(spritename);
**  Arguments:
**     argument0 = the name (a string) of a sprite
**  Returns:
**      the sprite_index of the sprite with that name, -1 if no sprite with that name was found
**  Notes:
**      No special notes

var spr,spr_n;
spr=sprite_create_from_screen(0,0,1,1,0,0,0,0) //creates a new sprite
for (spr_n=0;spr_n<spr;spr_n+=1){ //repeats for all sprites
    if sprite_exists(spr_n){ //if it exists
        if sprite_get_name(spr_n)=argument0{ //if its name is argument0
        sprite_delete(spr) //delete the created sprite
        return spr_n; //return the number of the sprite
sprite_delete(spr) //delete the created sprite
return -1;

Is there any way to make this faster, more efficient, or better in general?
What do you think of the uses of the actual code too?

Oh, also, if I haven't formatted my code, I'm sorry, my usual way of formatting it is very messy, so I hope I've done it right.

Last edited by Dark Matter (2010-03-04 18:10:15)


#2 2010-03-27 16:38:25

Registered: 2007-08-18
Posts: 1,240

Re: sprite_get_from_string

There is indeed a better way and it's been provided by paul23.

Once the map is built (at the start of the game), simply use the name as a key to return the value of the sprite from the map data structure. There are similar scripts for all of the other kinds resources as well.

Abusing forum power since 1986.


Board footer

Powered by FluxBB