Is using a variable in place of a timer a terrible idea?

0 favourites
  • 10 posts
From the Asset Store
Color Variable
$4.50 USD
10% off
Easily store, modify, read and manipulate colors with Color Variables!
  • Hello,

    I'd like your opinions on whether or not this is a terrible idea.

    So, I was working with some game objects and wanting them to fire off events when their particular timers ran out (in the demo I've linked below, they just do a grid move) and I was having some issues with multiple timers in a foreach object loop so I came up with the following solution:

    1) each object has a timer variable that is reduced by 1xdt each tick

    2) when the timer is equal or less than 0: Do something (ie move, call a function, reset the time, change a variable etc)

    In the tests I've done, it seems to work pretty consistently so what I'm wondering is:

    Is there a reason not to do this?

    Like, is it going to cause some issue that I haven't thought of or cause bees to start pouring in from another dimension?

    Anyway, here's a link to a capx that shows the method I am talking about.

    Thanks!

    https://www.dropbox.com/s/htnn44lqdyfhokk/dtTimerDemo.c3p?dl=0

  • That file doesn't open. What you describe is fine but I'm wondering more about the method you said wasn't working, I don't see how it would be any different. With timer behaviour it counts down (1) and then you would say on timer : do something (2).

  • In some cases it is easier to use a variable, I often do this in my projects. For example, when firing a weapon set cooldownTime to time+0.5

    There are many advantages of using Timer behavior though. You can pause/resume/cancel it. It conveniently triggers "On Timer" event for each instance. It works correctly when you change object's time scale. It has several expressions allowing to get the total or remaining time. And finally, you can run many timers, even with dynamically assigned tags.

  • lionz Weird. It seems to work when I download it.

    https://drive.google.com/file/d/1GIyhSjtVgbRYx52HXu9sPh7Yjmg6Jq-f/view?usp=sharing

    Here is a different link. hopefully this time. It is using r322 if that might be an issue.

    The issue I typically get when I run timers in a for each loop is that they seem to have issues with picking themselves 'onTimer' and it's probably down to my not having a full understanding of how to do that properly.

  • I should note,that I do use timers a fair bit with other object like the player instance. It's really just in the foreach that I've had issues.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • So just for the sake of it, I tried redoing the test I posted with a timer behaviour and it all just worked. (see below)

    So, chances are, the issues that I ran into before were the fault of something else (yay!)

    Thanks for taking the time to look anyway :)

    https://drive.google.com/file/d/1HUochV4upKGJXUfJ6fOIHYz2qA6XOdQ7/view?usp=sharing

  • I think the only non obvious thing you should take into account if you are just going to use a variable as a timer is delta time (There is a system expression to get the current delta time).

    If you just use a value by which the variable is incremented you could end up with the timer taking more or less time to complete on different devices running at different frame rates.

    Depending on your use case that might not be an issue though. For example the case of just wanting to produce extremely short delays, as in a few frames.

  • DiegoM

    What I've used is:

    Subtract 1*dt as opposed to just subtract 1

    Is that what you mean?

  • Yeah, that's it. That's the main thing the timer behaviour does under the hood.

  • DiegoM Awesome. Thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)