An optimized way to reduce timers

homelesswombathomelesswombat Member Posts: 73
edited November -1 in Working with GS (Mac)
So I came up with this last night while I was building an actor with a LOT of behaviors that needed to go in a specific sequence and thought I would share it.

game.timer = 0

Master Actor
Timer
.every 1 seconds
..change game.timer to game.timer+1

Rule
.When game.timer = 5
..Behavior

Rule when game.timer >= 10
.change game.timer to 0

Follower Actor
Rule
.When game.timer >= 7
.When game.timer <= 10
..Behavior

I now use this method for making platforms fade in and out in a cycle. I use a single master actor to run one timer that all other actors check on to determine when to do different behaviors. This is pretty efficient because there is only one timer. It also allows you to fake running a timer within a timer, although if you run a timer as one of the actor behaviors, the game will still be running two timers at once.

Anyone who wants to input how to make this better I'm all ears!

Comments

  • BarkBarkCoBarkBarkCo Member Posts: 1,400
    You could do the same thing with a single constrain. I'm not sure what is better, a timer + a change OR just a constrain, but here goes.

    Master Actor:
    Constrain game.timer to floor(self.time)

    Everything else would work the same...
  • scorelessmusicscorelessmusic Member Posts: 565
    Will probably have to test performance. I know it's said that constraints are huge on performance hits.
  • BarkBarkCoBarkBarkCo Member Posts: 1,400
    True, but I've seen/heard the same with Timers, so it's anyones game...

    Let me know if you get a chance to test either method. Thanks!
  • scorelessmusicscorelessmusic Member Posts: 565
    It's going to be a matter of time for me... going to be programming in rhythm loops soon. :)
  • BarkBarkCoBarkBarkCo Member Posts: 1,400
    hehe..."matter of time". Nice!
  • PhoticsPhotics Member Posts: 4,172
    I'm not so sure that I'd want to associate something like time with a rule. I'm thinking that rules sometimes misfire when the game slows down, especially if they're buried in a behavior heavy actor.

    There's already a game clock. I think it's much more accurate to use that. With the Photics: Toolbox, I experimented with this matter heavily. It was important that the app be optimized, but also accurate. I was building a stopwatch!
  • scitunesscitunes Member, Sous Chef Posts: 4,047
    I would assume you would need to capture the game.time value at the start of each level and then subtract that value from the total game.time to get your level time.

    change game.levelStartTime to game.time

    constrain game.levelTime to game.time - game.levelStartTime
  • BarkBarkCoBarkBarkCo Member Posts: 1,400
    @sci, That is why using the actors self.time or even the scene's timer makes more sense, becauase it doesnt start until the actor/scene is created.
Sign In or Register to comment.