GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

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

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

sprite_get_from_string

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.

Expand/*
**  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
        exit;}
    }
}
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)

Offline

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

xot
Administrator
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.

http://www.gmlscripts.com/script/map_sprites

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.

Offline

Board footer

Powered by FluxBB