Adventure Game Studio

AGS Development => Editor Development => Topic started by: Danvzare on Sun 06/10/2024 13:42:17

Title: [FEATURE REQUEST] - GetTimer() function
Post by: Danvzare on Sun 06/10/2024 13:42:17
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.
Title: Re: [FEATURE REQUEST] - GetTimer() function
Post by: Crimson Wizard on Sun 06/10/2024 14:09:15
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"....
Title: Re: [FEATURE REQUEST] - GetTimer() function
Post by: Crimson Wizard on Wed 16/10/2024 10:49:27
I ended up adding GetTimerPos() function to 3.6.2:
https://www.adventuregamestudio.co.uk/forums/ags-engine-editor-releases/ags-3-6-2-beta-1-a-wip-3-6-update/
Title: Re: [FEATURE REQUEST] - GetTimer() function
Post by: eri0o on Sat 14/12/2024 20:04:25
I tried to use this function by adding the following in the room script (and properly linking it)

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) Select
[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.
Title: Re: [FEATURE REQUEST] - GetTimer() function
Post by: Crimson Wizard on Sat 14/12/2024 20:08:25
@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.

Title: Re: [FEATURE REQUEST] - GetTimer() function
Post by: eri0o on Sat 14/12/2024 20:11:42
Ahh, ok, I will change the code to use IsTimerExpired!
Title: Re: [FEATURE REQUEST] - GetTimer() function
Post by: eri0o on Sat 14/12/2024 20:17:15
@Crimson Wizard , I documented this behavior in the docs https://github.com/adventuregamestudio/ags-manual/wiki/Globalfunctions_General#gettimerpos
Title: Re: [FEATURE REQUEST] - GetTimer() function
Post by: Crimson Wizard on Sat 14/12/2024 20:18:36
But if you don't use IsTimerExpired, you may as well just stop the timer yourself at 1.

Code (ags) Select
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.
Title: Re: [FEATURE REQUEST] - GetTimer() function
Post by: eri0o on Sat 14/12/2024 20:30:49
Thanks, I made a note about using SetTimer(TIMER_ID, 0) too.

Here

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