[FEATURE REQUEST] - GetTimer() function

Started by Danvzare, Sun 06/10/2024 13:42:17

Previous topic - Next topic

Danvzare

Hello.

What I'm suggesting here is a GetTimer() function, which simply returns the number of cycles left on a timer. My reason for this is twofold. Firstly, sometimes you'll want to be able to do something in the middle of a timer, without setting up another timer. Secondly, it would provide a way to see whether or not a timer is running, without incurring the IsTimerExpired()'s OFF state.

Basically with that second reason, what I'm suggesting, is a function similar to IsTimerExpired(), but where it never goes into an OFF state. It always returns true or false depending on whether it's counting down or not. In this case, it would be something along the lines of if(GetTimer(1) > 0).

This is because there have been numerous times that OFF state feature of IsTimerExpired() has caused me headaches, simply because I've either forgotten about it or it runs counter-productive to what I'm trying to accomplish. A quick look through the forums for problems that people have had with timers, and it seems as though I'm not the only one.

The thing is though, that OFF state is very useful. But it'd be nice if we had a function which didn't have it. And being able to get the exact time left on a timer, would do exactly that.
Alternatively, a IsTimerRunning() function, which returns true if the timer is above 0, or false if it's below 0, could also work. But that would be much more limiting.

I really hope this feature isn't something that's already in the editor. I've looked through the manual, and couldn't see any mention of it.
But none the less, thanks for hearing me out.


EDIT:
By the way, I know you can do this by simply implementing your own timers. But to me, it really seems like it's something that should be part of the editor itself.

Crimson Wizard

#1
There's an idea to replace timer functions completely with something similar to this module:
https://www.adventuregamestudio.co.uk/forums/modules-plugins-tools/module-timer-0-9-0-alternate-variant/

Following is the ticket opened for this task:
https://github.com/adventuregamestudio/ags/issues/1958


But, while this is not worked on yet, I suppose adding one small function, that does not require any extra data, to the existing API won't hurt... and old api will get deprecated altogether eventually.

EDIT: except, I would not use exactly "GetTimer" name, as that sounds like getting a Timer object.

EDIT2: oh, I see, it is suggested as "GetTimer" because there's "SetTimer"....


eri0o

I tried to use this function by adding the following in the room script (and properly linking it)

Code: ags
function room_AfterFadeIn()
{
  SetTimer(1, 10);
}

function room_RepExec()
{
  int timeLeft = GetTimerPos(1);
  if (timeLeft == 0)
  {
    System.Log(eLogInfo, "Timer 1 is not running");
  }
  else if (timeLeft == 1)
  {
    System.Log(eLogInfo, "Timer 1 is about to expire!");
  }
}


Now the result of this code is that I get the following log

Code: text
[Main][Alert]: Adventure Game Studio v3.6 Interpreter
Copyright (c) 1999-2011 Chris Jones and 2011-2024 others
Engine version 3.6.2.3, 32-bit LE

[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
[Script][Info]: Timer 1 is about to expire!
...

Yeahp, it goes like this forever. I thought it would give a ton of Timer 1 is not running logs, but actually that branch just never happens. I think there is either a bug in the implementation or I am doing something incorrectly that I can't understand.

Crimson Wizard

@eri0o this is the problem with the current Timer implementation. It stays at 1 UNTIL user calls IsTimerExpired.
Add IsTimerExpired check somewhere in the script, and that should fix this infinite timer.


eri0o

Ahh, ok, I will change the code to use IsTimerExpired!


Crimson Wizard

#7
But if you don't use IsTimerExpired, you may as well just stop the timer yourself at 1.

Code: ags
function room_RepExec()
{
  int timeLeft = GetTimerPos(TIMER_ID);
  if (timeLeft == 0)
  {
    System.Log(eLogInfo, "Timer %d is not running", TIMER_ID);
  }
  else if (timeLeft == 1)
  {
    System.Log(eLogInfo, "Timer %d is about to expire!", TIMER_ID);
    SetTimer(TIMER_ID, 0); // stop the timer
  }
}

EDIT:
I think maybe this should be fixed... at least in ags4.

eri0o

#8
Thanks, I made a note about using SetTimer(TIMER_ID, 0) too.

Here

https://adventuregamestudio.github.io/ags-manual/Globalfunctions_General.html#gettimerpos

SMF spam blocked by CleanTalk