Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

how to randomly spawn actors inside a 6x7 grid?

FamTechAdminFamTechAdmin Posts: 47Member
edited May 2015 in Working with GS (Mac)


I was just wondering how to randomly spawn actors inside a 6x7 grid?

I want to spawn actors onto 41 spots inside the grid and leave 1 spot empty (located at the bottom-right corner).

it should randomly choose between 4 types of actors and spawn only once, at the beginning of a level.

Im trying to create a game similar to candy crush and free fall...
...well, it only looks similar at the beginning of the game.

update: I watched this tutorial, How To Spawn Random, Non-Repeating Actors and was wondering if it is possible to use this logic on a grid? if yes, then how?


  • ArmellineArmelline Posts: 4,830Member, PRO

    Spawn the actors and assign a "type" to them at random afterwards. That way you're just spawning a simple grid and doing a change attribute in each actor.

    Is there something more you'd be after?

  • FamTechAdminFamTechAdmin Posts: 47Member

    Thanks for the tip but I´m not sure if I understood how to assign a "type" to them to be able to spawn a simple grid?

    yes, once the grid is set and I have 41 actors on it and 1 empty square (the bottom-right corner), I will only be able to move the actors closest to the empty square. which means the actors that are next to it on either side or above or below it.

  • ookami007ookami007 Posts: 581Member

    @FamTechAdmin - I think what he means is something I implemented in my game.

    I spawn the actor. The first rule of the actor is to assign a random number between 1 and 3 to a self.myType integer attribute.

    I then change the graphic of the actor, based on the value of self.myType using IF statements.

    If you do this, you don't really need to randomly place them, you just fill all empty squares with an actor and as soon as it's placed, it becomes one of the 4 different types.

  • FamTechAdminFamTechAdmin Posts: 47Member

    I tried accordingly to this tutorial, How To Spawn Random, Non-Repeating Actors but only one out of 41 actors seem to work. changing to a different image I mean.

    but I think that I´ll try your method instead... sounds less complicated than the one I just tried.

  • ookami007ookami007 Posts: 581Member

    @FamTechAdmin - Here's a demo for you of two ways to do it. The first way (first scene... hit the START button) will generate a 6x7 grid with the same object. The object itself will make a random version of itself (change number and color).

    The second version (2nd scene), hit the start button and it will generate a 6x7 grid with 4 different objects randomly placed.

    I hope that helps you.

  • FamTechAdminFamTechAdmin Posts: 47Member


    thank you for the demo, it was exactly how I wanted the actors to be added to the grid. I´ll experiment a little to see if I can get it as I pictured it in my head :)

  • ant_ladant_lad Posts: 222Member

    @ookami007 Thank you for this

  • ookami007ookami007 Posts: 581Member

    @FamTechAdmin / @ant_lad - Glad to help.

  • FamTechAdminFamTechAdmin Posts: 47Member


    I have a few questions regarding the grid, maybe you can help?

    1. Where do you change the position of the squares? I tried changing the x-y values on each square, but they didn´t move to the new position.
    2. if i want to use images instead of colored squares, shall I just remove the change attribute in the random object actor and use a change image instead?
    3. how to interpolate so that row 1 falls down into place from above followed by row two and so on until all rows are in place in the grid?
    4. how to make sure that only 2 of the same kind (in this case numbers and color) end up next to each other, horizontally and vertically when i press start?
  • ookami007ookami007 Posts: 581Member

    1) If you're using my template, when the objects are spawned they are spawned on specific x,y coordinates that are calculated based on what row/column is being drawn. The calculation is in the spawn actor behavior. You just have to tweak that until it's in the right place.

    2) Yes, if you want to use images, you just change the image to whatever you want based on the number... or if you're using the separate object spawner, just set the image in each object.

    3) That's a complex question and it really depends on what you want to happen. Are you thinking something along the lines of a match 3 game, where the gems, etc. fall down when there is space below them?

    4) You'll most like have to generate it in a table, then loop through the table a few times to check and fix any issues.

  • FamTechAdminFamTechAdmin Posts: 47Member
    edited June 2015
    1. thank you.
    2. ok :)
    3. your answer was actually to a question, that I was going to ask later. ;) so yes, that´s one of the things I was thinking. but i was also thinking if it´s possible to make them fall down once the start button has been pressed. just like a cool effect instead of just make them appear on the screen one row at a time.
    4. I tried that, before creating this thread but it only worked with one of the actors.. the rest didn´t react (I based it on this tutorial How To Spawn Random, Non-Repeating Actors
Sign In or Register to comment.