Anyway to spawn different actors randomly in the same spawner?

FlyboyTrevy_FlyboyTrevy_ Member, PRO Posts: 148

I have a spawner thats spawns an actor every (either 1 second, .7 seconds, or .4 seconds), would there be any way to make it so that it selects a random actor every time it spawns one?

Comments

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

    Sure. You can do something like this:

    Timer every 1 second
    Change attribute self.WhichActor (integer) to random(1,3)
    When attribute self.WhichActor=1
    Spawn Actor [actor1]
    When attribute self.WhichActor=2
    Spawn Actor [actor2]
    When attribute self.WhichActor=3
    Spawn Actor [actor3]

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

  • Village IdiotVillage Idiot Member, PRO Posts: 486

    could you please explain how to implement this - I'm not as smart as most

  • EverwildEverwild Member Posts: 48

    Basically what you are doing is giving your actor a rule to roll a random number, like a dice roll, then depending on what it rolls, you tell it what to spawn.

    I'm definitely no expert, but I use this method pretty frequently. I've used both self attributes and game attributes, depending on what kind of thing I'm doing, but for this we'll assume it's a self.attribute, and it'll be in your spawner actor.

    So in your actor, create a self attribute. I usually call these 'tickers' or 'dice', so my attribute would be self.ticker, an integer.

    Now create a timer in this actor.
    Set it so every 1 second, it changes that self.ticker to a random number, using the random(min,max) expression.

    Your min number will be the lowest number your 'dice' can roll.
    Your max number should be the number of actors you have available for spawning. So if you have five possible actors that could spawn, your random would look like 'random(1,5)'.

    Now you make a rule for spawning each actor, based on what that random number is.
    if self.ticker = 1
    -spawn actor1

    if self.ticker = 2
    -spawn actor2

    And so on.

    Now for something a little more complicated, but useful. It really simplifies things down the line -

    With the possibilities of going between 1, .7, and .4 seconds, I'm assuming you're looking to have an option to speed things up in your game. In that case, create a table. 3 rows, 1 column. We'll call it 'timetable'.
    Row 1 - 1
    Row 2 - .7
    Row 3 - .4

    Make a game.attribute, call it difficulty or speed or whatever.

    Now you can make a button (or trigger a rule) to change the speed. In the button actor, we'll tell it that when pushed, change game.speed to game.speed+1. If I were doing a button with 3 possibilities, usually I make it so once it would = 4, change it to 1, so it would go 1, 2, 3, 1, 2, 3, each time it is pushed. (if someone else has a better method for that, I'd love to know, but so far it works like it should).

    Now back in your actor, and the random number timer. Now instead of it being just one possibility of how fast it spawns, instead of putting a number of seconds there, you're going to tie it to your table, and to the button you just made.

    So instead of every 1 second... you're going to use an expression again. This time you'll use TableCellValue. For the table, select your timetable, for the row, select the game.speed attribute, for the column, 1.

    That way if those times don't end up working out for you, you just change the numbers in the table, and don't have to keep going into the actor.

  • Village IdiotVillage Idiot Member, PRO Posts: 486

    Thanks.. though my head hurts. I don't know how all you guys know this stuff. Is it self taught?

  • FlyboyTrevy_FlyboyTrevy_ Member, PRO Posts: 148

    I felt the exact same way 4 or so months ago, but as you get to know GS more you see more and more where they are coming from. Nonetheless it's still very impressive what some on here do, it's the pros like you guys that make the Gamesalad community awesome.

Sign In or Register to comment.