Question about picking coordinates at random

I'm relatively new to GameSalad, so please bear with me.

I have 16 coordinates which I want to make available for the actor to spawn in. However, these coordinates have to be picked at random.

So let's say I want to spawn 2 actors, and I have the coordinates:
579x486, 579x574, 579x665 and 579x753

I only want to actors to spawn, so not all coordinates will be used, but I don't want them to constantly spawn at the first 2 either.

So far I've only found tutorials on how to spawn actors at completely random coordinates, so I would love some help with this.

How do I make the actors spawn at a random set of coordinates?

Comments

  • KevinCrossKevinCross London, UKMember Posts: 1,894
    edited June 2015

    I would probably go with the table route. Create a table and call it something like "random spawn coordinates" with at least two columns for X and Y. And 16 rows with all of your positions you want it to spawn.

    Then on the actor create an attribute called randomSpawnRow (call it what you like)

    And add code like this:

    Change Attribute: self.randomSpawnRow = random(1, tableRowCount(game.random spawn coordinates))
    Change Attribute: self.Position.X = tableCellValue(game.random spawn coordinates, self.randomSpawnRow, 1)
    Change Attribute: self.Position.Y = tableCellValue(game.random spawn coordinates, self.randomSpawnRow, 2)

    The numbers 1 and 2 in the last two lines are the column numbers I'm assuming you'd put the X and Y coordinates in the table.

    If you don't want an actor to spawn in the same place as another then you need to create a copy of the table with Copy Table, and then delete the row from the table that has just been picked with the Add/Remove Row behaviour.

    I recommend looking for guides/tutorials on how to use tables if you're not sure how to use the table functions and behaviours like tableCellValue and Copy Table etc.

    If you don't want to go the tables route then do something like this:

    Change Attribute: self.randomSpawnNo = random(1, 16)

    Rule: If self.randomSpawnNo = 1
    Change Attribute: self.Position.X = 579
    Change Attribute: self.Position.Y = 486

    Rule: If self.randomSpawnNo = 2
    Change Attribute: self.Position.X = 579
    Change Attribute: self.Position.Y = 574

    Rule: If self.randomSpawnNo = 3
    Change Attribute: self.Position.X = 579
    Change Attribute: self.Position.Y = 665

    Rule: If self.randomSpawnNo = 4
    Change Attribute: self.Position.X = 579
    Change Attribute: self.Position.Y = 753

    And so on for all 16 positions you want. This becomes harder to stop actors spawning in the same position though.

  • MassiveMaplerMassiveMapler Member Posts: 8

    @KevinCross Thanks for your answer, would it be possible to do something similiar with another actor acting as a spawner?

    I'm trying to make multiple different actors spawn at two different coordinates from the tables in a loop, after a button is pressed. I could just make the button change an attribute which executes the spawning, but then I would have no idea on how to control how many actors it spawns.

    I feel like what I just wrote is quite confusing, so I'll summarize it:
    1. Press button
    2. Different actors spawn at a different randomly picked set of coordinates
    3. The amount of actors spawning isn't always the same, and has to be able to be determined with an attribute
    4. The actors don't spawn at the same time, and only once.

    So my idea was to make the button something like Game.Spawn# +1, which then spawns the first actor, which also performs Game.Spawn# +1 spawning the 2nd actor, etc.

    The number of actors is determined with game.level, but I have no idea how to control this without using a spawner button.

    I'm sorry for asking so much, but I would really appreciate the help.

  • KevinCrossKevinCross London, UKMember Posts: 1,894
    edited June 2015

    I can't see if this will work as I'm not near a copy of GameSalad but I might try something like this:

    Let's say the number of actors to spawn is set in a game attribute which I'm going to call game.noOfActorsToSpawn. You set this number whenever you want. like level changes for example.

    Your button spawner actor would look like this:

    If touch is pressed
    ...Timer: Every 1 second (you said you didn't want them to spawn all at once so if that's the case change the length of time between each spawn)
    ......If self.spawnCounter <= game.noOfActorsToSpawn
    .........Change Attribute: self.spawnCounter = self.spawnCounter + 1
    .........Spawn Actor: your actor name, and set the coordinates as 0, 0 on the Spawn Actor behavior.

    On the actor that's spawned you would have the code I gave above to randomly position them once they've spawned.

    If the timer doesn't work and I misunderstood about you not wanting them all spawning at once then you could maybe try this:

    If touch is pressed
    ...Loop while self.spawnCounter <= game.noOfActorsToSpawn
    .........Change Attribute: self.spawnCounter = self.spawnCounter + 1
    .........Spawn Actor: your actor name, and set the coordinates as 0, 0 on the Spawn Actor behavior.

    And again, place the code I gave in my first comment on the actor that's being spawned to randomly position them once they've spawned.

    If you're still having trouble later I can maybe put together a basic template file once I'm home. Let me know if so.

  • MassiveMaplerMassiveMapler Member Posts: 8

    @KevinCross

    .........Spawn Actor: your actor name, and set the coordinates as 0, 0 on the Spawn Actor behavior.

    I'm trying to spawn different actors, not copies of the same actor, so I can't really work with Actor Name, right?

    I'll try to explain the result that I'm trying to accomplish a bit.

    1. Player presses Spawn Button
    2. Actor1 spawns, and after 1 second self.graphics.visible get's set to 0.
    3. Actor2 spawns, and the same thing happens.
    4. Actor3 MIGHT spawn, and the same thing happens.
    5. Actor4 MIGHT spawn, and the same thing happens.
    6. etc.

    Then, when all the actors are invisible, the player has to press Actor1, and everywhere else on the board is wrong. After pressing actor 1, the player now has to press Actor2.

    When the required numbers of actors are pressed, the player advances a level, and more actors will spawn.

    I.E.:
    1. Player opens game, level = 2
    2. Player presses spawn button
    3. Button 1+2 spawn
    4. Player presses actor 1>2, level +1
    5. Player presses spawn button
    6. Button 1+2+3 spawn

    So the actors need different attributes because of the order they have to be pressed in, which is why I think I need to use different actors.

    I hope this explained the result that I'm trying to accomplish a little better.

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    I think I have a better understanding on what you're after. I'll see if I can throw together a basic project doing the same thing later on when I'm home. If you haven't sorted it out yourself by then.

    It reminds me a little of a Simon says type game. Only instead of lights going out you're hiding the lights/actors completely and they're hidden in set positions on the scene

  • MassiveMaplerMassiveMapler Member Posts: 8

    @KevinCross

    If you've played Gen1 Pokémon, it looks quite a bit like the garbage cans at the 3rd gym. You press the right garbage can first, after which the second becomes available. If you press the second one before pressing the first one, it counts as the wrong garbage can.

    Main difference is that there COULD be more than 2 garbages cans.

  • KevinCrossKevinCross London, UKMember Posts: 1,894
    edited June 2015

    @MassiveMapler I've never played a Pokémon game but I watched a quick video of the trash cans in the gym and get what you mean about having to hit them in a correct order. The only difference in that game is that the trash cans aren't spawning or disappearing after x amount of seconds.

    Basically I will create something that does the following:

    1. Player opens game, level = 2
    2. Player presses spawn button
    3. Button 1+2 spawn (willl spawn these one after the other with no real delay between each, but if I can add a timer to it I will so you can change this if you do want a delay)
    4. After 1 second the buttons 1 and 2 will disappear in the order that they were spawned. You did say you wanted them to disappear after 1 second.
    5. Player presses actor 1>2, level +1. If they don't press the hidden buttons in the correct order the number is reset so that they have to hit button 1 again, and then button 2 etc.
    6. New level. Player presses spawn button
    7. Button 1+2+3 spawn
    8. Repeat

    Hopefully I'm on the right page with the above steps/requirements!

  • KevinCrossKevinCross London, UKMember Posts: 1,894
  • MassiveMaplerMassiveMapler Member Posts: 8

    @KevinCross Sorry for the late response, dealing with time zones etc.

    This is almost exactly what I was looking for, thanks a lot!

    The only problem is the conditions for "If touch is pressed". If Touch is the only condition, or if it gets set to "any", it works perfectly. However, if I try to add another condition (Like your 0.2 alpha condition) it doesn't work.

    You've already done a lot for me, so I'll do my best to find a solution myself.

    Once again, thanks a lot!

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    The only problem is the conditions for "If touch is pressed". If Touch is the only condition, or if it gets set to "any", it works perfectly. However, if I try to add another condition (Like your 0.2 alpha condition) it doesn't work.

    Is the actor alpha set as 0.2 when you're touching it or have you hidden it completely? It's hard for me to understand what's up, but I'm sure you'll solve it :) good luck.

  • MassiveMaplerMassiveMapler Member Posts: 8

    @KevinCross Even with your original project it's not working for me. No matter if you wait for the Alpha Timer or not. It works perfectly with just the Touch condition though.

Sign In or Register to comment.