Force Actors Apart

daniel.robert.campbelldaniel.robert.campbell Member Posts: 251
edited October 2012 in Working with GS (Mac)
I have an actor that is randomly spawned around the scene. The problem is that sometimes two of this actor are spawned within one another. I want to try and find a way to either force them apart, or ensure that the actors never spawn within a certain distance of each other. Anyone have any ideas? Any guidance would be very much appreciated.

Best Answer

  • MobileRocketGamesMobileRocketGames Posts: 128
    edited November 2012 Accepted Answer
    Create two game attributes. Name them Spawn1 and Spawn2.
    Open up the actor that holds your spawning behaviours. Create a new rule that happens when whatever signals your actors to spawn happens (in my example, a game attribute called SpawnLocation). Have it roll two random numbers and assign them to Spawn1 and Spawn2. Afterwards it needs to check if the locations are the same (game.CheckLocation). Then have it signal the spawning function (in this case game.SpawnActor).

    Rule:
    attribute game.SpawnLocation = true
    • change attribute game.Spawn1 = random(1,20)
    • change attribute game.Spawn2 = random(1,20)
    • change attribute game.CheckLocation = true
    • change attribute game.SpawnLocation = false
    (this assumes there are 20 spawning locations).
    Create a new rule with the "on event":
    attribute game.CheckLocation = true

    Then, place a new rule inside that rule with the "on event":
    attribute game.Spawn1 ≠ game.Spawn2
    • change attribute game.SpawnActor = true
    • change attribute game.CheckLocation = false
    and put in the otherwise section of this rule:
    • change attribute game.SpawnLocation = true
    • change attribute game.CheckLocation = false
    Obviously your spawning function needs to tell game.SpawnActor to change to false once the actors have been spawned. you can either put that in the spawning rule at the end or right in the second spawned actor itself.

    Basically what this is doing is when check location is turned on, it checks if the two numbers you rolled are the same or not. If they are the same, it turns back on the random function and rolls two new numbers and keeps doing this until it gets two numbers that arent the same. When it gets two different numbers, it turns on the spawning function. All you need to do is tell the spawning actors that if spawn1 = 1 then spawn in this location, if it = 2 spawn in this location. And so on. This will be especially easy if you use a table to store those locations (this way you only need 1 spawn behaviour and you put the link to the coordinates right into the X and Y inputs of the spawn behaviour, using the game.Spawn1 and Spawn2 in your links to the table row). I've done something similiar (exactly the same) in a game we are working on.

    There may be some slight adjustments needed as some of the rules may fire too quick and I'm not in front of my computer so i can't test it out.
    But the logic is sound and it does work.
    Awesome? maybe. Insightful? I think so :D

Answers

  • UtopianGamesUtopianGames Member Posts: 5,692
    You could make a tag (home/actors/+) and do collide with actor of tag.

    Then if they collide or spawn on top of each other they will push apart.

    Darren.
  • I gave them a collide behavior and told them to collide with each other. Does that do the same thing? If so, I tried that and it doesn't really seem to work. Either they don't move apart, or they violently move apart and throw each other across the level.
Sign In or Register to comment.