Randomizer
![mariusiancu](http://forums.gamesalad.com/applications/dashboard/design/images/defaulticon.png)
Hi.
I want to spawn instances of an actor at random periods of time.
I have an 'actor generator' that spawns this actors. The generator has a rule that says "Every random (1,3) seconds spawn that actor".
Now, the problem is that the timer rule chooses a random number and sticks with it. If it chooses 2 then it spawns actors every 2 seconds for ever. I want the time period to by actually random. Like 1,2,1,3,2,2,3 etc.
How do I do that?
I want to spawn instances of an actor at random periods of time.
I have an 'actor generator' that spawns this actors. The generator has a rule that says "Every random (1,3) seconds spawn that actor".
Now, the problem is that the timer rule chooses a random number and sticks with it. If it chooses 2 then it spawns actors every 2 seconds for ever. I want the time period to by actually random. Like 1,2,1,3,2,2,3 etc.
How do I do that?
Best Answer
-
HC_DK Posts: 92
One way to do this is to use a RULE instead of the TIMER behaviour.
In the actor you use to spawn add 2 REAL attributes, called "Next Time" and "Random Time"
This is how your actor´s code should look like:
------------------------------------------------------------
Change Attribute: self.Next Spawn to random(1,3)
Rule - All - Attribute: self.Next Spawn = self.Time
- Change Attribute: self.Random Time To: random(1,3)
- Change Attribute: self.Next Spawn To: self.Time+self.Random Time
------------------------------------------------------------
Explaination:
The first Change Attribute will only be runned 1 time. This is to activate the RULE the first time before the "loop" starts.
When self.Next Spawn = self.Time the Rule will be activated and self.Random Time gets a new value (can be the same number again)
The new self.Random Time value will be added to self.Time and now the "loop" begins.
You can DL the file I made for testing here: http://dl.dropbox.com/u/12878764/Random Timer.zip
I have added a self.Changed index attribute to make sure that the rule was in a "loop" and displayed the 3 values in a DISPLAY TEXT behaviour.
HC
Answers
Thank you for this great tutorial.
I never noticed the Time atribute.
Thank you again!