keeping track of time

Place to get help with not working mods / modding interface.
User avatar
semvoz
Inserter
Inserter
Posts: 30
Joined: Wed Jun 17, 2015 12:03 pm
Contact:

keeping track of time

Post 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?
User avatar
ThaPear
Fast Inserter
Fast Inserter
Posts: 226
Joined: Fri May 30, 2014 8:05 am
Contact:

Re: keeping track of time

Post by ThaPear »

There is a global counter game.tick, divide by 60 and voila, you have the current game time.
User avatar
semvoz
Inserter
Inserter
Posts: 30
Joined: Wed Jun 17, 2015 12:03 pm
Contact:

Re: keeping track of time

Post 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.
dalsio
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sun Jun 21, 2015 9:56 pm
Contact:

Re: keeping track of time

Post 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.
Last edited by dalsio on Tue Jun 30, 2015 7:35 am, edited 1 time in total.
User avatar
semvoz
Inserter
Inserter
Posts: 30
Joined: Wed Jun 17, 2015 12:03 pm
Contact:

Re: keeping track of time

Post 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 :)
dalsio
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sun Jun 21, 2015 9:56 pm
Contact:

Re: keeping track of time

Post 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.
User avatar
semvoz
Inserter
Inserter
Posts: 30
Joined: Wed Jun 17, 2015 12:03 pm
Contact:

Re: keeping track of time

Post 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
dalsio
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sun Jun 21, 2015 9:56 pm
Contact:

Re: keeping track of time

Post 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.
kisPocok
Burner Inserter
Burner Inserter
Posts: 15
Joined: Fri Jun 20, 2014 11:05 pm
Contact:

Re: keeping track of time

Post by kisPocok »

+1 for the topic. We shoul have at least one timestamp.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14269
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: keeping track of time

Post 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.
If you want to get ahold of me I'm almost always on Discord.
kisPocok
Burner Inserter
Burner Inserter
Posts: 15
Joined: Fri Jun 20, 2014 11:05 pm
Contact:

Re: keeping track of time

Post 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!
Zeblote
Filter Inserter
Filter Inserter
Posts: 973
Joined: Fri Oct 31, 2014 11:55 am
Contact:

Re: keeping track of time

Post by Zeblote »

But you can already do that with game.tick, 60 ticks = 1 second.
zurisar
Burner Inserter
Burner Inserter
Posts: 13
Joined: Sun Mar 13, 2016 8:31 pm
Contact:

Re: keeping track of time

Post 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
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3716
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: keeping track of time

Post 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
kisPocok
Burner Inserter
Burner Inserter
Posts: 15
Joined: Fri Jun 20, 2014 11:05 pm
Contact:

Re: keeping track of time

Post 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)
Zeblote
Filter Inserter
Filter Inserter
Posts: 973
Joined: Fri Oct 31, 2014 11:55 am
Contact:

Re: keeping track of time

Post 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.
NoPantsMcDance
Filter Inserter
Filter Inserter
Posts: 478
Joined: Fri Jul 17, 2015 6:56 pm
Contact:

Re: keeping track of time

Post 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.
Looking for a multiplayer server? Check out my servers Vanilla Server
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: keeping track of time

Post 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.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
NoPantsMcDance
Filter Inserter
Filter Inserter
Posts: 478
Joined: Fri Jul 17, 2015 6:56 pm
Contact:

Re: keeping track of time

Post 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.
Looking for a multiplayer server? Check out my servers Vanilla Server
Post Reply

Return to “Modding help”