TIGSource Forums

Developer => Technical => Topic started by: Rat Casket on May 26, 2013, 06:20:23 AM



Title: Select Random Sprite From A Group?
Post by: Rat Casket on May 26, 2013, 06:20:23 AM
So I made this little gem stacking game and I have a blank gem object that when created randomly chooses a red, green, or blue sprite. It uses this line in create to do so.

sprite_index = choose(sRed,sBlue,sGreen);

I'm adding more and more gems so what I want to do is just have it randomly choose a sprite from inside a group (a folder) of gem sprites, rather than add each new sprite manually to this code.

Thanks in advance!


Title: Re: Select Random Sprite From A Group?
Post by: aberrantmind on May 26, 2013, 04:12:12 PM
I'd be interested in seeing a solution to this too. posting so that it appears in my new replies feed. If I figure it out, I'll post up a solution as well.


Title: Re: Select Random Sprite From A Group?
Post by: Xishem on May 26, 2013, 09:07:22 PM
One option is to add sprites to a ds_list as you load them.

Then generate a random integer between 1 and the ds_list_size, and use something like:

Code:
sprite_index = ds_list_find_value(spriteList, randomnumber)


Title: Re: Select Random Sprite From A Group?
Post by: aberrantmind on May 27, 2013, 07:41:29 AM
ah cool. looks like you can even use the ds...shuffle to get a random result. would only go through each item on the list once though, which would be good if you have a deck based mechanic.


Title: Re: Select Random Sprite From A Group?
Post by: C.A. Silbereisen on May 28, 2013, 03:38:47 AM
One option is to add sprites to a ds_list as you load them.

Then generate a random integer between 1 and the ds_list_size, and use something like:

Code:
sprite_index = ds_list_find_value(spriteList, randomnumber)
actually the lowest number for ds_list indexes is 0 and the highest is list size-1, so

Code:
sprite_index= ds_list_find_value(spriteList, irandom(ds_list_size(spriteList))-1)

also you could make adding new blocks extremely easy by loading the sprites from an external folder rather than adding them to the project file itself. create a folder that contains your block sprites, named block0.png, block1.png, block2.png etc and write a script that loops through these files and loads them one after the other, using file_find_first and file_find_next