Easy way to constantly reverse boolean atribute?

FrazzleFrazzle Member Posts: 223
edited August 2013 in Working with GS (Mac)
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.

Comments

  • -Timo--Timo- Member Posts: 2,313
    what about
    when self.on is true
    after 2 seconds
    change to false

    when self.on is false
    after 2 seconds
    change to true?
  • KevinCrossKevinCross London, UKMember Posts: 1,894
    Does this not work then:

    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
  • SocksSocks London, UK.Member Posts: 12,822
    Make the attribute an integer (let's call it 'Laser').

    Every 2 seconds . . . .

    Change Laser to 1 - Laser.
  • FrazzleFrazzle Member Posts: 223
    Thanks for the help - Socks was the easiest. Cheers! FM
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    I like Socks way....

    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.


  • ericzingelerericzingeler Member Posts: 334
    and if you want to drop the timer behavior:

    User attributes:

    timerAmount (real or index) = 2
    toggle (index) = 1

    ----

    Rule:

    If [timerAmount] = [self.Time] % [timerAmount]

    change attribute toggle to ([toggle] + 1) % 2
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    cool.
  • -Timo--Timo- Member Posts: 2,313
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    less timers in game the better... more efficient on the device...

    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.

  • ericzingelerericzingeler Member Posts: 334
    @timolapre1998

    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.
Sign In or Register to comment.