Easy way to constantly reverse boolean atribute?
Frazzle
Member Posts: 223
I'm trying to create a laser that changes it's 'ON' attribute true to false and false to true every 2 seconds, but it appears that changing self.on to -self.on using a timer doesn't work. Would I have to go the long route and do the 'when true' thing instead?
Help is appreciated, FM.
Help is appreciated, FM.
Comments
when self.on is true
after 2 seconds
change to false
when self.on is false
after 2 seconds
change to true?
✮ FREE templates at GSinvention ✮
✮ Available for hire! support@gsinvention.com ✮
Timer: Every two seconds
..Rule: If self.on is true
....Change Attribute: self.on = false
..Otherwise:
....Change Attribute: self.on = true
The dots were added to show nesting
Every 2 seconds . . . .
Change Laser to 1 - Laser.
But for the sake of variety and learning...another method is the classic toggle rule
(x+1)%2
In a rule:
every 1 second
change attribute lazer to
(lazer+1)%2
This will toggle the value from 0 and 1.
If you made it %3 it should toggle through 0,1 and 2 every second.
User attributes:
timerAmount (real or index) = 2
toggle (index) = 1
----
Rule:
If [timerAmount] = [self.Time] % [timerAmount]
change attribute toggle to ([toggle] + 1) % 2
✮ FREE templates at GSinvention ✮
✮ Available for hire! support@gsinvention.com ✮
With that said you'd need to ideally test with a timer and with the work around to see which gives better performance.
Also it depends how complex your game is, it only becomes a problem when your trying to max out what you can achieve in GameSalad on your target device.
I'm generally pretty slack at optimization to start of with, then go back and improve it all once I like the game mechanics.
Looks harder, but it's actually easier for your program. The timer behavior is a layman version of a loop. Although easy to implement, it's junk. Time is already be counted in every actor, the current scene and the game... why not just use one of them instead?
In general, using time to trigger actions is not best, but in the case of @Frazzle 's intended use, its ideal.