Download

Trying to get a random spawn that doesn't repeat

Doing an endless runner style map and spawning tiles ahead of the player.

I’ve got an array that it spawns from. But it has a tendency to throw the same tile 4 times in a row occasionally. Fairly annoying. They are large tiles and recognizable so I really want to minimize the repeating.

I tried a shuffle but not sure I’m using it right. I tried before and after the loop.

I really noticed no difference between having the shuffle and not.

I’ve got 8 tiles atm and I get a lot of 2,3 and 4 of the same tiles. Maybe 50% of the time I am getting 4 unique tiles.

I ran into a similar issue when spawning waves of enemy units in a randomized manner. What I ended up doing was adding weights to each enemy type. You can do something similar in this scenario as well.

Each of the 8 tiles can have equal weighting at the beginning. But every time you use up a tile, you reduce its weighting, which then slowly increases (till it reaches the default value) over time/update cycles. If you absolutely do not want repetition, you can even set the weighting down to zero after a tile is used. This will make sure that the tile does not repeat itself in adjacent iterations.

Have you got an example of how to do that. I’m afraid that’s getting beyond my knowledge.

I’m trying remove item and remove from index. While they do remove stuff from the array they both seem to be working on the index. So some of the items can still respawn as their index number is just resetting each time.

Hi,

I am spanish, sorry for my bad english :slight_smile:

You can use simple solution what get source array and generate other array with no repeat values:

The pass for infinite array without repeats:

  1. Create array
  2. Use index and increment position
  3. When the index is greater than array size go to pass 1

Thanks Inclán

Your english is good enough for me :wink:
Looks like my very next project.

Well, I think I got it right.

Still repeats some though. Not seeing much difference than from before really. Ran through a bunch of spawns and kept numbers.

If I spawn 4 tiles out of 8 (I was spawning 7 when I took this screen) I still get repeats 50% of the time. That’s about the average I had before. Of course I am probably doing this wrong.

’ Forest tiles ’ is an array of 8 tiles.
‘empty tiles’ is a completely empty array.

I know I am just getting a random number to spawn, out of the random shuffle I just made (wasted). But I can’t figure out how to plug the output array into the spawn actor without the GET.

I may not have enough free time to try out the implementation until weekend. I’ll message you the solution tomorrow.

Hi, the function “Random Forest Tiles” generate a new array with randomized tiles, you no need re-randomize the variable “Last index”

I attached the example, is a asset with simple sample, copy this in the content folder of your project, add it to map
test.jpg

Inclán
I appreciate the effort but I cannot get that to load up. Copy/pasted into my content folder. Restarted. Tried 4 times.

If I get rid of the random integer and just plug in last index I get 4 of the first tiles in my original index. Everytime.

If i unplug everything from the get then I ‘get’ 0, which again is just 4 of the very first index, same as above.

Do you *always *want 4 unique tiles? This will always give you 4 unique elements, it creates a temporary array and removes elements that were used:

If you want to allow for a non-unique element, iterate 3 times instead of 4, and add another element from the All Tiles list. This way you will have 3 unique and a small chance (eyeballing it at 20-25%) for a single repetition.

I always want unique tiles yes :slight_smile:

At first the game is level up mode. So it starts with 4 tiles, after the player levels up it will increase. When all levels are complete it will just be an endless runner.

So at first with only 4 tiles, if I am getting 2-4 of the same exact one it’s really noticable (large tiles so the scenery stands out. it’s not just 200 tiles with random objects where everything kind of looks the same anyway).

Once it’s ‘endless’ I will have ‘zones’ so it will spawn green tiles for a little while and by the time the index of green tiles is empty it will change to an index of red tiles, etc… eventually cycling back to green tiles.

I updated my post.

Awesome! I will try it in a few. Thanks!

Edit::

It works!!!

So I added the remove AFTER the spawn, I used the get from temp tile pool for the actor spawn.

Thanks for all the help guys!

I am going to try this with Inclán’s set up also… I’ll post a pic if I get it working in case anyone else needs it,

My example works. Generate the array of classes first and only then spawn the tiles; here it looks as if you were doing it in the middle of the loop. If in doubt show us how you wired the whole thing up.

edit: saw your update, good luck!