Scripts / Avram's timer

Author
Message
Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 12th Mar 2014 00:04 Edited at: 12th Mar 2014 09:02
I was disappointed we didn't have timers for timed events in this beta (1.005) like in old FPSC (remember [e]timerstart?) and thought a bit about that and then I remembered that each operating system has system clock that is available in most programming languages via operating system API and LUA was not an exception, we do have date and time functions in LUA.



So I created this very simple LUA class that can work as regular and countdown timer. The idea is to remember system time when our timer is started [started = os.time()] and then we can always check how much time has passed since the timer is started by simply subtracting our remembered value from current system time [passed = os.time() - started]. So this is using system clock and doesn't depend on your FPS (it's independent from the game). It is precise to a second (can not check milliseconds for example) but that should be enough for most games.



Here it is in action (as a countdown timer):





The code for this video (actually only for the plant on the table; plant.lua):





The key fact to remember here is that our *_main function can be called by engine numerous times per second, so it is very important to check when we are creating timer object or (re)starting/stopping the timer. Also, the timer object must be global (out of function scope) as it needs to preserve it's state between multiple calls to *_main. That's why I have introduced global aTimers array (actually in LUA it's a table). You can use it however you want, but since we already have entity index e, I thought it would be easy to use just aTimers[e], but that's cool for one timer per entity. If you need more, figure something out (aTimers['my_custom_timer']). With this first line: aTimers[e] = aTimers[e] or aTimer:new(10) we are telling the engine to give us current entity timer or to create new countdown timer (from 10). So the first time our *_main function is called, the timer will be created. Each subsequent call will give us existing timer for this entity. Woohoo!



DOCS:



-----------------------------------------------------

aTimer:new(countdown, autostart)

Desc: Class constructor used to initialize the timer.



Arguments:

number countdown - [optional] Number to countdown from. Use as a countdown timer if greater than 0; default: 0

boolean autostart - [optional] Should start automatically (true/false); default: false



Returns: aTimer object of class aTimer

-----------------------------------------------------

aTimer:running()

Desc: Checks if the timer is running (is started).



Returns: boolean true if timer is running, false if not

-----------------------------------------------------

aTimer:start(countdown)

Desc: Starts the timer.



Arguments:

number countdown - [optional] Number to countdown from. Use as a countdown timer if greater than 0; default: inherited from constructor



Returns: boolean true if timer is started (and it is)

-----------------------------------------------------

aTimer:reset(countdown)

Desc: Alias for aTimer:start(countdown)

-----------------------------------------------------

aTimer:stop()

Desc: Stops the timer.



Returns: number Number of seconds passed since start

-----------------------------------------------------

aTimer:passed()

Desc: Gets number of seconds passed since timer was started



Returns: number Number of seconds or 0 if timer is not started

-----------------------------------------------------

aTimer:greater(seconds)

Desc: Checks if number of seconds passed since start is greater than provided value



Arguments:

number seconds - [required] Number of seconds to check



Returns: boolean true if timer is running longer than provided value if seconds

-----------------------------------------------------

aTimer:lower(seconds)

Desc: Checks if number of seconds passed since start is lower than provided value



Arguments:

number seconds - [required] Number of seconds to check



Returns: boolean true if timer is running shorter than provided value if seconds

-----------------------------------------------------

aTimer:between(min, max, inclusive)

Desc: Checks if number of seconds passed since start is between provided min and max values



Arguments:

number min - [required] Number of seconds to check (low)

number max - [required] Number of seconds to check (high)

boolean inclusive [optional] Should min and max be included in check; default: true



Returns: boolean true if timer is between min and max values, false otherwise

-----------------------------------------------------

aTimer:left()

Desc: Gets number of seconds left to count down to zero (0)



Returns: number Number of seconds or 0 if timer is not a countdown timer

-----------------------------------------------------

aTimer:expired()

Desc: Checks if the countdown timer has reached zero (0)



Returns: boolean True if zero is reached or false if not. And false also if timer is not a countdown timer

-----------------------------------------------------



Additionally, each time the timer is (re)started, it's internal aTimer.cnt counter will increase. This can be used to check if the timer is (re)started again (if you don't want to repeat timer-related actions)



Unzip attached archive to your "FPSC:R/Files" folder.

Jerry Tremble
GameGuru TGC Backer
11
Years of Service
User Offline
Joined: 5th Nov 2012
Location: Sonoran Desert
Posted: 12th Mar 2014 04:24
Nice work, thank you! Also, "Avram's Timer" just sounds cool!
PM
Pirate Myke
Forum Support
13
Years of Service
User Offline
Joined: 31st May 2010
Location: El Dorado, California
Posted: 12th Mar 2014 05:46
Scene Commander
Support Manager
16
Years of Service
User Offline
Joined: 3rd May 2008
Location:
Posted: 12th Mar 2014 08:12
Nice work that I'm sure will be very useful.



SC

i7-4770s 3.10ghz - Geforce GTX 650ti
Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 12th Mar 2014 08:55
My first post might be a little confusing as it was written when I was tired. I should post another example of using this as a regular timer, and I will do that when I find some time to make another example. If you have any questions, don't hesitate to ask.



Jerry Tremble wrote: "Nice work, thank you! Also, "Avram's Timer" just sounds cool!"




Yeah, the name is sooo original xD I wasn't so inspired last night, and I didn't want to give it generic name like "Timer" as there might be more timers in FPSC:R core in the future, so voila, there is Avram's timer (or by it's class name - aTimer).

J0linar
GameGuru TGC Backer
14
Years of Service
User Offline
Joined: 3rd Feb 2010
Location: Vienna, AT
Posted: 12th Mar 2014 12:00
really nice work there,

@Avram could you maybe include a example that performs a animation?

http://j0linar.blogspot.co.at/



PC SPECS: Windows 8 Pro 64-bit, Intel I7-3630QM 4.8GHz CPU, Nvidia GTX 675M - 2048MB GPU, 16GB DDR3 RAM
PM
Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 12th Mar 2014 14:01
I'm not sure we can control animation with LUA yet... but with this class you can delay whatever you can do regularly.

smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 12th Mar 2014 19:51
ha i actually saw that command when i was writing my timer code but didnt try it as i was sure it wouldnt be supported yet... but nice to see it's already working and good simple example

life's one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
Avram
GameGuru TGC Backer
17
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 12th Mar 2014 21:18
Yeah, I love reading LUA manual and then trying to use some nifty functions in FPSC:R. FPSC:R LUA already supports vast majority of functions from LUA manual, maybe even all of them...

Login to post a reply

Server time is: 2024-05-06 09:17:09
Your offset time is: 2024-05-06 09:17:09