Page 1 of 1

keeping track of time

Posted: Fri Jun 19, 2015 7:16 am
by semvoz
Hello,

What is the recommended way to keep track of the passing time?

I have check the base mode on how it handles the landing of the fleet, but there look to be specific method developed specifically for that, which would be pretty much useless to me.

Code: Select all

local timeleft = player.force.gettimetoland()
For now I am writing utility methods and keep track of the ticks in the ontick event, basically adding one second each 60 ticks.
But that is not really convenient and we cannot get any idea of the date/time that way either.

As far as I know we cannot access os.clock as the os module is disabled, which makes sense for security reasons.
But still, date and time utilities would be really useful for mods/scenarios coding..

How do you guys handle that in your mods?
Am I missing something?

Re: keeping track of time

Posted: Fri Jun 19, 2015 9:37 am
by ThaPear
There is a global counter game.tick, divide by 60 and voila, you have the current game time.

Re: keeping track of time

Posted: Sun Jun 21, 2015 2:03 am
by semvoz
ThaPear wrote:There is a global counter game.tick, divide by 60 and voila, you have the current game time.
Thanks for the info, much easier to use this one.

However, there is no way to know the date/time right?
That would be useful for the mod I am writing.

Re: keeping track of time

Posted: Tue Jun 30, 2015 6:51 am
by dalsio
Are you looking for the time of day in the game, or the time in the real world?

In-game time can be found as a global variable: game.daytime

You can read from or write to it just like any variable. It's represented as a number from 0-1, 0 is noon, 0.25 is evening, 0.5 is midnight, 0.75 is morning, and 0.99 is just short of noon again. From there, you can use various math functions to convert it to whatever format you want.

If you're looking for real-world time, LUA can communicate with the clock in the user's OS. Of course, if their OS time is inaccurate or the user changed it, what you would get would be wrong but it's the best I know how short of linking to a proper global clock via the internet.

http://www.lua.org/pil/22.1.html --guide on getting and using OS time

It's a bit long because there's multiple ways to get it and display it, but it's all explained there (stuff at the bottom might be more relevant to you). Hope this helps.

Re: keeping track of time

Posted: Tue Jun 30, 2015 7:03 am
by semvoz
dalsio wrote:Are you looking for the time of day in the game, or the time in the real world?

In-game time can be found as a global variable: game.daytime

You can read from or write to it just like any variable. It's represented as a number from 0-1, 0 is noon, 0.25 is evening, 0.5 is midnight, 0.75 is morning, and 0.99 is just short of noon again. From there, you can use various math functions to convert it to whatever format you want.

If you're looking for real-world time, LUA can communicate with the clock in the user's OS. Of course, if their OS time is inaccurate or the user changed it, what you would get would be wrong but it's the best I know how short of linking to a proper global clock via the internet.

http://www.lua.org/pil/22.1.html --guide on getting and using OS time

It's a bit difficult because it's represented in an odd way, but it's all explained there. Hope this helps.
I am looking into the real-world time (would be quite useful for timestamp based unique id for instance).
the os library is disabled for mods in factorio, so we cannot use it, already tried :)

Re: keeping track of time

Posted: Tue Jun 30, 2015 7:48 am
by dalsio
Yes I can see that now. That presents a problem for me too... well I don't know what else to do. If the OS library is disabled, then IO is probably also disabled preventing any sort of work-around. The only thing I can think of is either modifying the base Factorio programming (which may present a whole slew of problems) or simply request access to real-world time from the developer. There's a thread for requesting stuff for the mod interface: https://forums.factorio.com/forum/viewforum.php?f=28. Probably your best bet. Blocking access to the OS lib is probably a safety feature, but having access to os.clock and os.date is quite useful.

Re: keeping track of time

Posted: Tue Jun 30, 2015 7:54 am
by semvoz
For now I opted on a pseudo-timestamp solution using game.tick but you can still get conflict with different games if you get real unlucky, that's good enough in my case

Re: keeping track of time

Posted: Tue Jun 30, 2015 8:00 am
by dalsio
Yea, bare minimum you can poll game.tick at the beginning, write a getTime fuction that subtracts the current game.ticks from the starting game.ticks, then call getTime any time you want the number of ticks that have passed since the program started running. This of course would not count the time spent while the game is paused, but it's something.

Re: keeping track of time

Posted: Tue Mar 22, 2016 12:42 pm
by kisPocok
+1 for the topic. We shoul have at least one timestamp.

Re: keeping track of time

Posted: Tue Mar 22, 2016 4:20 pm
by Rseding91
Why do you want access to the operating system time in a mod? I don't see how that could be useful in any way.

Re: keeping track of time

Posted: Tue Mar 22, 2016 4:49 pm
by kisPocok
Rseding91 wrote:Why do you want access to the operating system time in a mod? I don't see how that could be useful in any way.
I'm making a voting system which has time limits. It has to be countdown from T-5 minutes.
Secondly, I would like to save the player's connection time to determinate the current playtime.

I'm not sure the os time is the right way for that, but any other timestamp would be nice!

Re: keeping track of time

Posted: Tue Mar 22, 2016 4:50 pm
by Zeblote
But you can already do that with game.tick, 60 ticks = 1 second.

Re: keeping track of time

Posted: Wed Mar 23, 2016 7:03 am
by zurisar
Rseding91 wrote:Why do you want access to the operating system time in a mod? I don't see how that could be useful in any way.
example 1: I need to generate random number, but math.rand() out same number each cycle, with current timestamp I can always get 'really random' numbers
example 2: Like TS said, for unique id, someone can make some event for own server and for some events he need unique ids, timestamp is the best and easiest way for this

Re: keeping track of time

Posted: Wed Mar 23, 2016 7:31 am
by DaveMcW
I don't understand your objection to math.rand(). Timestamp is one of the worst ways to generate random numbers.

Unique id is easy.

Code: Select all

global.unique_id = global.unique_id + 1
local unique_id = global.unique_id

Re: keeping track of time

Posted: Wed Mar 23, 2016 8:18 am
by kisPocok
Zeblote wrote:But you can already do that with game.tick, 60 ticks = 1 second.
Does it depend on the computer speed? (sorry for the noob question)

Re: keeping track of time

Posted: Wed Mar 23, 2016 8:41 am
by Zeblote
It'll use game time, so if you pause the game, it'll pause the timer. You can also save & load a game later with the timer still running.

If you have less than 60 ups it'll run slower.

Re: keeping track of time

Posted: Wed Mar 23, 2016 8:45 am
by NoPantsMcDance
Rseding91 wrote:Why do you want access to the operating system time in a mod? I don't see how that could be useful in any way.
I actually have a use for this. I was going to setup something that prints to chat how long until the server restart is going to be and use a countdown.

Re: keeping track of time

Posted: Wed Mar 23, 2016 9:54 am
by Adil
For that it's probably better to directly request features of server, like running commands in its console.
As far as I remember, os.time is blocked in factorio due to desyncs it could introduce.

Re: keeping track of time

Posted: Wed Mar 23, 2016 11:24 am
by NoPantsMcDance
Adil wrote:For that it's probably better to directly request features of server, like running commands in its console.
As far as I remember, os.time is blocked in factorio due to desyncs it could introduce.
Ah yeah that makes sense.