Transport Fever

Transport Fever

View Stats:
Querying current game date / year in Lua
Is it possible to query the current in-game year (or date) in Lua code?

The availability.yearFrom / yearTo in the .con file controls when something appears in the menu for building, but I want to differentiate behavior based on what year it is when you construct something.
< >
Showing 1-6 of 6 comments
Dirkels Dec 20, 2017 @ 2:10pm 
+1 Would love to know that as well :-)
SoftwareSimian Apr 15, 2018 @ 9:43am 
Thanks to canophone for providing a link to the documentation[transportfever.com].

Answer:
game.interface.getGameTime() returns a 2-element table with "date" and "time".
"time" is a float, not sure offhand what the timescale is
"date" is a table with 3 entries: "year", "month", "day"
local gameInterfaceGetGameTime = game.interface.getGameTime() print("* in-game date: " .. gameInterfaceGetGameTime["date"]["year"] .. "-" .. gameInterfaceGetGameTime["date"]["month"] .. "-" .. gameInterfaceGetGameTime["date"]["day"] .. " " .. math.round(gameInterfaceGetGameTime["time"]))
gives you something like:
in-game date: 1952-8-25 1755
Last edited by SoftwareSimian; Apr 15, 2018 @ 9:45am
SoftwareSimian Apr 16, 2018 @ 8:30am 
Caveat: when generating a new map, industries (probably everything else too) are placed before time starts, or even exists, and TpF will crash with "attemt to index field 'interface' (a nil value)" if you try to access game.interface.getGameTime() before time starts running. If you wrap a check around it you can avoid the crash:
if ((game ~= nil) and (game.interface ~= nil)) then local gameInterfaceGetGameTime = game.interface.getGameTime() print("* in-game date: " .. gameInterfaceGetGameTime["date"]["year"] .. "-" .. gameInterfaceGetGameTime["date"]["month"] .. "-" .. gameInterfaceGetGameTime["date"]["day"] .. " " .. math.round(gameInterfaceGetGameTime["time"])) else print("* in-game date: not available (probably placing industries on a new map)") end
Last edited by SoftwareSimian; Apr 16, 2018 @ 8:31am
canophone Apr 16, 2018 @ 8:56am 
Originally posted by SoftwareSimian:
Caveat: when generating a new map, industries (probably everything else too) are placed before time starts, or even exists, and TpF will crash with "attemt to index field 'interface' (a nil value)" if you try to access game.interface.getGameTime() before time starts running. If you wrap a check around it you can avoid the crash:
if ((game ~= nil) and (game.interface ~= nil)) then local gameInterfaceGetGameTime = game.interface.getGameTime() print("* in-game date: " .. gameInterfaceGetGameTime["date"]["year"] .. "-" .. gameInterfaceGetGameTime["date"]["month"] .. "-" .. gameInterfaceGetGameTime["date"]["day"] .. " " .. math.round(gameInterfaceGetGameTime["time"])) else print("* in-game date: not available (probably placing industries on a new map)") end

The planner mod uses the reference script functions, what I gather from there is that they must be placed in a script location.
SoftwareSimian Apr 16, 2018 @ 9:38am 
Originally posted by canophone:
The planner mod uses the reference script functions, what I gather from there is that they must be placed in a script location.
Not sure what you mean? My above code works fine (for my purposes anyways).
canophone Apr 16, 2018 @ 10:09am 
Originally posted by SoftwareSimian:
Originally posted by canophone:
The planner mod uses the reference script functions, what I gather from there is that they must be placed in a script location.
Not sure what you mean? My above code works fine (for my purposes anyways).

Maybe I'm overthinking its needs!
< >
Showing 1-6 of 6 comments
Per page: 1530 50

Date Posted: Dec 18, 2017 @ 8:57pm
Posts: 6