How to make a random behaviour NOT select the same value twice?

Slayre77Slayre77 Member, PRO Posts: 115

Hello guys, in my game, which is a sidescrolling runner, I want enemies to spawn in a different pattern everytime. I used a table to decide which pattern is going to spawn (I figured using tables would be easier because as the game goes on, I want to allow more types of enemies to spawn, so in a table I can select which rows / columns I want to "spawn" depending on how long the run has gone). So, I used a random behaviour to select one of 3 different columns, and when one is selected, a particular pattern of enemies will spawn. However, I am encountering this problem whereby if the random behaviour selects the same value TWICE, it doesnt spawn any enemies for a second time, so sometimes there are gaps between waves of enemies. Perhaps I am understanding the problem wrongly, but whatever it is, any help is appreciated! Thanks!!

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    You need to initially copy the table so that you have something to restore later (via Copy Table) and then use the copied table to choose random rows. After you choose a row, delete it. That way you avoid duplicates.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • Slayre77Slayre77 Member, PRO Posts: 115

    @tatiang I tried what you said but it didn't work. Can you please explain how it avoids duplicates? For example, i have 3 values (1,2,3). Then the value 1 is selected, and the pattern of enemies spawns. Then, the table is copied and then 1 is selected again and nothing spawns. Maybe im doing something wrong, I dont know!!??

  • Slayre77Slayre77 Member, PRO Posts: 115

    Actually, I wouldnt mind if the random selected the same number twice if the enemies would spawn twice, but they don't .

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited December 2014

    Sorry, there's a step I didn't mention. When you choose a random number, don't choose random(1,3). Choose random(1,tableRowCount(tableName)). That way, as the table shrinks, the max random value shrinks with it.

    I have a demo of this (attached).

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Sign In or Register to comment.