Add method to return current unix epoch time

User avatar
Afforess
Filter Inserter
Filter Inserter
Posts: 422
Joined: Tue May 05, 2015 6:07 pm
Contact:

Add method to return current unix epoch time

Post by Afforess »

It seems the lua 'os' library is not included in the factorio lua sandbox, likely for security considerations. In general, this is a good idea, but in particular, a way to access the unix epoch time would be useful. Epoch time is useful for logging (timestamps), debugging slow code execution (examining the difference between start and end timestamps), setting timers / alarms (the time between ticks can vary, so doing something in 60 ticks is not the same as 1 second), etc.

I'd guess something like game.os_time() would work. Millisecond or nanoseconds would be preferable to second accuracy.

Thoughts?
sillyfly
Smart Inserter
Smart Inserter
Posts: 1101
Joined: Sun May 04, 2014 11:29 am
Contact:

Re: Add method to return current unix epoch time

Post by sillyfly »

User avatar
Afforess
Filter Inserter
Filter Inserter
Posts: 422
Joined: Tue May 05, 2015 6:07 pm
Contact:

Re: Add method to return current unix epoch time

Post by Afforess »

sillyfly wrote:Slightly related discussion: https://forums.factorio.com/forum/vie ... 99&p=89368
Thanks for the link.

I think to clarify, I don't care about player time, but machine / operating system time of the host / local player. I don't think the player time matters, just the time for the machine executing the code. I think the main usefulness here is for modders - debug logging, timing code.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14911
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Add method to return current unix epoch time

Post by Rseding91 »

The only reason we don't have it accessible now is determinism. You could never use a replay or play multiplayer with any mod that used os.time() (or any variant of it).
If you want to get ahold of me I'm almost always on Discord.
User avatar
Afforess
Filter Inserter
Filter Inserter
Posts: 422
Joined: Tue May 05, 2015 6:07 pm
Contact:

Re: Add method to return current unix epoch time

Post by Afforess »

Rseding91 wrote:The only reason we don't have it accessible now is determinism. You could never use a replay or play multiplayer with any mod that used os.time() (or any variant of it).
If the function only works in SP, that seems like an acceptable trade-off.
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Add method to return current unix epoch time

Post by Adil »

I believe. It's been raised somewhere already: devs don't support separation of multiplayer and single player mods. Even though you're not forced to ensure mp compatibility.

It would indeed be nice to have some tool for benchmarking which is not a bunch of cryptic numbers in an overlay which brings the renderer on it's knees on its own.

For anything else, it's really more reasonable to use the game's own timeline (as in, ticks) exactly because 1 second is not always 60 ticks.
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.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3733
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Add method to return current unix epoch time

Post by DaveMcW »

How about player.uptime(), which tracks how long a player has been in the current map.
Oxyd
Former Staff
Former Staff
Posts: 1428
Joined: Thu May 07, 2015 8:42 am
Contact:

Re: Add method to return current unix epoch time

Post by Oxyd »

DaveMcW wrote:How about player.uptime(), which tracks how long a player has been in the current map.
I don't see how that would be useful, as the return value would be given in ticks. (Returning time would be non-deterministic, and therefore not possible.) So if you wrote code like local t = player.uptime(); some calculation; player.print(player.uptime() - t);, it would always print 0.

You could of course use it to measure ticks between event callbacks, but then again, you can do that already without player.uptime(): Just subtract game.tick values.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14911
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Add method to return current unix epoch time

Post by Rseding91 »

Adil wrote:I believe. It's been raised somewhere already: devs don't support separation of multiplayer and single player mods. Even though you're not forced to ensure mp compatibility.

It would indeed be nice to have some tool for benchmarking which is not a bunch of cryptic numbers in an overlay which brings the renderer on it's knees on its own.

For anything else, it's really more reasonable to use the game's own timeline (as in, ticks) exactly because 1 second is not always 60 ticks.
Turn off the other debug options and it doesn't effect performance at all to have the debug times rendering.
If you want to get ahold of me I'm almost always on Discord.
orzelek
Smart Inserter
Smart Inserter
Posts: 3924
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Add method to return current unix epoch time

Post by orzelek »

Rseding91 wrote:
Adil wrote:I believe. It's been raised somewhere already: devs don't support separation of multiplayer and single player mods. Even though you're not forced to ensure mp compatibility.

It would indeed be nice to have some tool for benchmarking which is not a bunch of cryptic numbers in an overlay which brings the renderer on it's knees on its own.

For anything else, it's really more reasonable to use the game's own timeline (as in, ticks) exactly because 1 second is not always 60 ticks.
Turn off the other debug options and it doesn't effect performance at all to have the debug times rendering.
It still shows only first number for mods - all others were always 0 last time I checked.
And those missing ones are avg and max if I remember correctly?
Rseding91
Factorio Staff
Factorio Staff
Posts: 14911
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Add method to return current unix epoch time

Post by Rseding91 »

orzelek wrote:
Rseding91 wrote:
Adil wrote:I believe. It's been raised somewhere already: devs don't support separation of multiplayer and single player mods. Even though you're not forced to ensure mp compatibility.

It would indeed be nice to have some tool for benchmarking which is not a bunch of cryptic numbers in an overlay which brings the renderer on it's knees on its own.

For anything else, it's really more reasonable to use the game's own timeline (as in, ticks) exactly because 1 second is not always 60 ticks.
Turn off the other debug options and it doesn't effect performance at all to have the debug times rendering.
It still shows only first number for mods - all others were always 0 last time I checked.
And those missing ones are avg and max if I remember correctly?
Yes, you have to give it time to generate the average and max. That information isn't saved so it refreshes every time you open the game.
If you want to get ahold of me I'm almost always on Discord.
User avatar
Afforess
Filter Inserter
Filter Inserter
Posts: 422
Joined: Tue May 05, 2015 6:07 pm
Contact:

Re: Add method to return current unix epoch time

Post by Afforess »

Adil wrote:I believe. It's been raised somewhere already: devs don't support separation of multiplayer and single player mods. Even though you're not forced to ensure mp compatibility.
What is game.local_player then? That is a SP only method.
Adil wrote:It would indeed be nice to have some tool for benchmarking which is not a bunch of cryptic numbers in an overlay which brings the renderer on it's knees on its own.

For anything else, it's really more reasonable to use the game's own timeline (as in, ticks) exactly because 1 second is not always 60 ticks.
Counting ticks is not an useful method, there needs to be a way to measure wall time of the cpu clock. I know exactly how many ticks pass already. Tick time is not helpful for debugging slow code that executes during a single tick.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14911
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Add method to return current unix epoch time

Post by Rseding91 »

Afforess wrote:
Adil wrote:I believe. It's been raised somewhere already: devs don't support separation of multiplayer and single player mods. Even though you're not forced to ensure mp compatibility.
What is game.local_player then? That is a SP only method.
game.local_player is not a SP only method. It works just fine in MP - in fact that's the reason it was added so you could use it in MP and from the console.
If you want to get ahold of me I'm almost always on Discord.
ratchetfreak
Filter Inserter
Filter Inserter
Posts: 952
Joined: Sat May 23, 2015 12:10 pm
Contact:

Re: Add method to return current unix epoch time

Post by ratchetfreak »

Afforess wrote:
Adil wrote:I believe. It's been raised somewhere already: devs don't support separation of multiplayer and single player mods. Even though you're not forced to ensure mp compatibility.
What is game.local_player then? That is a SP only method.
It only exists when the callstack originates from a console command and is the player that typed and submitted the command

just like a gui click event also contains the player that clicked
orzelek
Smart Inserter
Smart Inserter
Posts: 3924
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Add method to return current unix epoch time

Post by orzelek »

Rseding91 wrote:
orzelek wrote:
Rseding91 wrote:
Adil wrote:I believe. It's been raised somewhere already: devs don't support separation of multiplayer and single player mods. Even though you're not forced to ensure mp compatibility.

It would indeed be nice to have some tool for benchmarking which is not a bunch of cryptic numbers in an overlay which brings the renderer on it's knees on its own.

For anything else, it's really more reasonable to use the game's own timeline (as in, ticks) exactly because 1 second is not always 60 ticks.
Turn off the other debug options and it doesn't effect performance at all to have the debug times rendering.
It still shows only first number for mods - all others were always 0 last time I checked.
And those missing ones are avg and max if I remember correctly?
Yes, you have to give it time to generate the average and max. That information isn't saved so it refreshes every time you open the game.
I have never seen anything else then 0's for 2nd and 3rd mod timings.
Even after charting thousands of tiles with RSO and chart command only number changed is first one. And it takes quite a few minutes to do all that charting.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14911
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Add method to return current unix epoch time

Post by Rseding91 »

orzelek wrote:I have never seen anything else then 0's for 2nd and 3rd mod timings.
Even after charting thousands of tiles with RSO and chart command only number changed is first one. And it takes quite a few minutes to do all that charting.
Mods don't do the charting. Mods run a command which tells the game "chart this area" and the game does it slowly over the next X ticks where X is however long it takes to do the charting without lagging the game.
If you want to get ahold of me I'm almost always on Discord.
orzelek
Smart Inserter
Smart Inserter
Posts: 3924
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Add method to return current unix epoch time

Post by orzelek »

Rseding91 wrote:
orzelek wrote:I have never seen anything else then 0's for 2nd and 3rd mod timings.
Even after charting thousands of tiles with RSO and chart command only number changed is first one. And it takes quite a few minutes to do all that charting.
Mods don't do the charting. Mods run a command which tells the game "chart this area" and the game does it slowly over the next X ticks where X is however long it takes to do the charting without lagging the game.
RSO gets called every chunk generated and shows some serious time on first number - never anything on 2nd and 3rd. And first one can jump up to 2 ms or so quite frequently since it's generating a lot of ores. I can see numbers for stuff like entities/scripts etc taking much less time and all 3 numbers working.
What is needed to get 2nd/3rd number to show up for mod?
It might be that I have wrong assumption about what those two mean.
Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: Add method to return current unix epoch time

Post by Boodals »

Although this is (or was, if a moderator moves it) in Won't implement, and is stupidly old, I created a way to get performance metrics for lua scripts (I have source access), which is in 0.17.15.
LuaGameScript.create_profiler, returns a LuaProfiler, which can be used anywhere a localised string is used. You can't read the raw time values from lua. This means that it is totally desync safe.
Post Reply

Return to “Implemented mod requests”