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

Legacy GM [SOLVED] irandom() issues in Create Event

T

TheRBZ

Guest
I've made an obj_tree which can choose a variation out of 4 different sprites (so the object doesn't look repeated) but when I try my code, all trees are the same type.
I can confirm that this is not an issue with the sprite_index applying to all instances* because every time I run the game they are of a variation '0'.

Why is my irandom(3) function always outputting 0?
Yes, the sprites it can choose from are different.

*instances are put in the room manually, only for testing so using the create event per instance won't cut it

Code:
randomize();

image_xscale = 2;
image_yscale = 2;

var type = 1;
var variation = irandom(3);

sprite_index = "sprite_tree_big_type"+string(type)+"_"+string(variation);
 
T

TheRBZ

Guest
Resource IDs are not strings, stop treating them like one.

Use asset_get_index() to convert that string name back into a sprite ID, then set sprite_index to it.
Code:
sprite_index = asset_get_index("sprite_tree_big_type"+string(type)+"_"+string(variation));
Fixed! Thank you.
 
Top