getting rid of timers

expired_012expired_012 Member Posts: 1,802
edited November -1 in Working with GS (Mac)
We all know that timers can cause some major performance issues.
Does anyone have any neat tricks to getting rid of timers completely and using something more memory-friendly instead?

Comments

  • old_kipperold_kipper Member Posts: 1,420
    for one thing I am doing I have checks against scene time rather that timers but I am not sure of the overhead being less or more. It appears to be accurate and definitely can work, but my design is pretty non processor intensive so it might be more of a problem in a more 'heavy' game.
  • RedlerTechRedlerTech Member Posts: 1,583
    Create an attribute called TimerStart1

    When you want to use a timer:
    change attribute game.TimerStart to game.Time
    (Let's say your timer is "For" 3 Seconds)
    When game.Timer Start = game.Time - 3,
    Behavior Here :D
    Matt
  • InLikeFlynnInLikeFlynn Member Posts: 171
    Similar to Nextgen's answer, but you could also use interpolate to change a value over a set period of time. For example, interpolate a real attribute from 0 to 10 over ten seconds, then set rules according to when your real attrib = 5. I think having several rules listening to one interpolate is less expensive than several timers, but I'm not sure.
  • HoneyTribeStudiosHoneyTribeStudios Member Posts: 1,792
    You can also avoid using 'every' in a timer with the following:

    Create an index/integer attribute called zero. Give it a value of 0.

    When zero=floor( self.Time *20)%2

    ...do stuff

    That will 'do stuff' every 0.1 seconds.
    Adjust the '20' to change that. Without the '20' it will do stuff every 2 seconds.

    ---
    www.HoneyTribeStudios.com
    Get Honey Tribe on iTunes
    "...a touch above the rest in the endless running genre": 148apps.com
    Say hi on Twitter
    Like us on Facebook
  • LumpAppsLumpApps Member Posts: 2,881
    I tried using interpolate but have trouble with it.

    For example
    An enemy is hit and alpha interpolated to 0
    When alfa is zero alfa is 1

    Sometimes alfa goes back to 1 sometimes it stays 0.1 (or at leat not 0. I don't know exactly where it stops)
  • expired_012expired_012 Member Posts: 1,802
    HoneyTribeStudios said:
    You can also avoid using 'every' in a timer with the following:

    Create an index/integer attribute called zero. Give it a value of 0.

    When zero=floor( self.Time *20)%2

    ...do stuff

    That will 'do stuff' every 0.1 seconds.
    Adjust the '20' to change that. Without the '20' it will do stuff every 2 seconds.

    That is awesome! Thanks for sharing that, I just got rid of most of my "every" timers.
    GeoffB said:
    Similar to Nextgen's answer, but you could also use interpolate to change a value over a set period of time. For example, interpolate a real attribute from 0 to 10 over ten seconds, then set rules according to when your real attrib = 5. I think having several rules listening to one interpolate is less expensive than several timers, but I'm not sure.

    GeoffB said:
    Similar to Nextgen's answer, but you could also use interpolate to change a value over a set period of time. For example, interpolate a real attribute from 0 to 10 over ten seconds, then set rules according to when your real attrib = 5. I think having several rules listening to one interpolate is less expensive than several timers, but I'm not sure.

    That what my original idea was as well, but I didn't know how performance heavy using interpolate would be. Im gonna run some tests now to see which is the better for performance
  • expired_012expired_012 Member Posts: 1,802
    I just ran some tests. I removed all of the timers from my actors and instead used interpolate as my timers, just like GeoffB mentioned.

    Method 1 (with timers)

    Timer: after 1.2 seconds
    --destroy

    Method 2 (without timers)

    -Interpolate: self.TimerCount to 1
    duration 1.2

    Rule: when self.TimerCount = 1
    --destroy

    I did this to about 30 actors with images just to make sure that I would get accurate data.

    The scene using method 1(with timers) had a total memory usage of 51.1 MB
    The scene using method 2 (without timers) had a total memory usage of 46.8MB

    Removing the timers saved me about 5MB of memory!

    All of the extra memory went to the "other" section
  • HoneyTribeStudiosHoneyTribeStudios Member Posts: 1,792
    I've found that interpolates can put a dent in the frame rate if used while loads of stuff is going on. For optimisation reasons, in general it seems like the best behaviour to use is change attribute. So I think Matt's answer is better. Another way to express it is the following.

    To replace a timer with 'after':

    Make a real attribute called timeStamp.

    When your event happens change timeStamp to gameTime

    When gameTime = timeStamp + 1second or whatever duration you want
    ...do stuff

    ---
    Get Honey Tribe on iTunes
    "...a touch above the rest in the endless running genre" 148apps.com
    "If you’re a nature lover, or simply a lover of well-executed games, Honey Tribe... is worth checking out. Download it today." AppAdvice.com
    Say hi on Twitter
    Like us on Facebook
    www.HoneyTribeStudios.com
  • entersimonentersimon Member, PRO Posts: 273
    Here's the rule I came up with to replace an "every" timer that is functional when using whole numbers:

    Create an integer attribute called 0 and leave it at 0 (this will never change)

    Then whenever you want to have an "every" timer used at whole second intervals you will input this rule:

    Rule:
    ...When attribute game.0 = self.time%3
    ......Change attribute a to a+1

    In this example the "3" after the modulo "%" is the seconds you want in between each change of attribute, and the "a to a+1" was just some gibberish to give an example of an attribute being changed.

    What's going on here is that as self.time changes, whenever it becomes an exact multiple of 3, the rule's conditions are valid.
Sign In or Register to comment.