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

custom name problem

S

stepup2000

Guest
im having a lot of enemies all called obj_zombie_1, obj_zombie_2 etc. im trying to create a line of codes now to make my life easier and only having to change the number for a different enemy. right now i have lines like:

number = choose(1, 2)

if number = 1 {
image_index = spr_zombie_1
}

if number = 2 {
image_index = spr_zombie_2
}

(note that i ahve like 15 enemies so this is very time consuming and not efficient. Now i tried a different code to make this a lot easier but it is not working this is what i tried:

number = choose(1, 2)
sprite_index = "spr_zombie_"+string(number)

but this doesnt seem to work, anyone a idea?
 

samspade

Member
im having a lot of enemies all called obj_zombie_1, obj_zombie_2 etc. im trying to create a line of codes now to make my life easier and only having to change the number for a different enemy. right now i have lines like:

number = choose(1, 2)

if number = 1 {
image_index = spr_zombie_1
}

if number = 2 {
image_index = spr_zombie_2
}

(note that i ahve like 15 enemies so this is very time consuming and not efficient. Now i tried a different code to make this a lot easier but it is not working this is what i tried:

number = choose(1, 2)
sprite_index = "spr_zombie_"+string(number)

but this doesnt seem to work, anyone a idea?
sprite_index takes a number, not a string. You want asset_get_index. There's an example in there that is pretty close to what you want.
 
S

stepup2000

Guest
sprite_index takes a number, not a string. You want asset_get_index. There's an example in there that is pretty close to what you want.
so i used this:
var obj = asset_get_index("obj_zombie_" + string(number));
but i dnt understand how this would help me, since this just returns -1 if the string exists. but you told me yourself i cant use strings for an index number. i cant just say: image_index = obj
 
Last edited by a moderator:

samspade

Member
asset_get_index returns a number, not a string. It returns -1 if the asset is not found. It will return the number of the asset if it is found which you can then use for things like sprite_index or instance_create and so on.
 
L

Ludo Design

Guest
The function asset_get_index takes in a string (which should be comprised of "spr_zombie_" and the string version of the number you're providing), and it brings back a number representing the ID number of the asset you are looking for. This asset ID is the sprite ID that you want to assign to your zombie's sprite_index. In your original code, you're getting a -1 because you do not have sprites that are named "obj_zombie_1." They are named "spr_zombie_1."

So in other words, what you want is:

Code:
var number_chosen = choose( 1, 2 );
var my_sprite_index = asset_get_index( "spr_zombie_" + string( number_chosen ) );
sprite_index = my_sprite_index;
Because of your naming convention, you won't need to do any comparisons. You will already have the sprite ID ready to assign to your zombie's sprite_index.

To further clarify:
  • sprite_index refers to the sprite asset.
  • Sprites are composed of 1 or more images (in an animation, for example).
  • image_index represents the current image (or animation frame, we could say) of the current sprite.
 

FrostyCat

Redemption Seeker
Don't use 15 distinct objects if you don't have to. Create just one obj_zombie and give its instances distinct numbers. Also, read the Manual and make a distinction between sprite_index and image_index.

Example:
Code:
with (instance_create_layer(x, y, "Instances", obj_zombie)) {
  number = irandom_range(1, 15);
  sprite_index = asset_get_index("spr_zombie_" + string(number));
}
 
L

Ludo Design

Guest
But yes, objects are typically used as templates, and if your sprite_index is the only major variation, just make instances of them and change their sprite_indexes, either randomly or manually after creating them in code.
 
Last edited by a moderator:
S

stepup2000

Guest
Don't use 15 distinct objects if you don't have to. Create just one obj_zombie and give its instances distinct numbers. Also, read the Manual and make a distinction between sprite_index and image_index.

Example:
Code:
with (instance_create_layer(x, y, "Instances", obj_zombie)) {
  number = irandom_range(1, 15);
  sprite_index = asset_get_index("spr_zombie_" + string(number));
}
yeah sorry i didnt say it right (not my native language sorry) i meant i have one object that just uses a lot of lines to choose a random sprite. but thanks for your clarification anyways :)
 
S

stepup2000

Guest
But yes, objects are typically used as templates, and if your sprite_index is the only major variation, just make instances of them and change their sprite_indexes, either randomly or manually after creating them in code.
thank you, you explained everything very well, im gonna try tomorrow if it works.
 
S

stepup2000

Guest
But yes, objects are typically used as templates, and if your sprite_index is the only major variation, just make instances of them and change their sprite_indexes, either randomly or manually after creating them in code.
yeah it worked, so thanks a lot man, i owe you one
 
Top