Is there a way to get the in-game year?

Willkommen in der Transport Fever Community

Wir begrüßen euch in der Fan-Community zu den Spielen Transport Fever und Train Fever, den Wirtschaftssimulatoren von Urban Games. Die Community steht euch kostenlos zur Verfügung damit ihr euch über das Spiel austauschen und informieren könnt. Wir pflegen hier einen freundlichen und sachlichen Umgang untereinander und unser Team steht euch in allen Fragen gerne beiseite.

 

Die Registrierung und Nutzung ist selbstverständlich kostenlos.

 

Wir wünschen euch viel Spaß und hoffen auf rege Beteiligung.

Das Team der Transport-Fever Community


  • Is there a way to call the game time/year inside a mod file?

    The only thing I've found in the game files is

    Code
    game.interface.getGameTime().date.year

    but it crashes the game and reports that "interface" is a nil value.

    I haven't been able to trace the line back to a source either.

    Anyone have any tips?


    Thanks.

  • game.interface is only valid in the runFn section of the mod.lua file.

    Is it? I thought game.interface is only available after loading.


    In fact, game.interface is accessible in the script thread and game.gui in the gui thread. More Info: Game Script


    What are you trying to do / where are you calling interface?

  • If it's a file with function data() it's loaded once while starting the game or when the ressource is requested.

    The updateFn/ runFn somethingFns are stored and executed later, so in these functions you may have access to game.

  • I think that link will give me what I need, assuming an online translator will get the necessary details across. I've only been in the English section of the Lexicon where the Game Script page isn't present.


    Basically, I was making something where inside updateFN it checks what the current year is and then an assortment of "if" statements do what they do with that information.

    I moved on to a different project, but this is good to have for when/if I transition back. At least I know it's possible. The devs say they're releasing a modding wiki with the next game update so maybe I'll wait and see if they have some instructions of their own for non-bilingual folks such as I.


    Thanks for the extra details!

  • Welp, unfortunately now that I know it exists everything I'm working on can be made better by implementing it in some way. Not necessarily the game time, but the options in general.

    Equally unfortunate, I'm still having a hard time figuring out how to get started on it and don't think I'll be able to wait until the modding wiki arrives. Cursed programmers addiction.

    So I'm hoping someone can help me get started. Even just something simple so I can see how it all interconnects. I should be able to expand from there.


    Using the original posts in-game year as an example:

    Say you have a construction item. Inside the .con there is an updateFN which triggers when something gets changed in the Settings. What would I need to do in order to access the in-game year and put it into a variable inside updateFN whenever updateFN gets triggered?

  • I also had this problem in the updateFn. (Wo ist game.interface?)

    It's not desgined that way to have access to the interface functions inside a construction. If you cannot solve your problem with the availibilty years, maybe with upgradeConstruction from gamescript.

  • Ah, alright. At least I can stop figuratively banging my head against that wall trying to figure it out. I can do it with the Availability section, and have been as part of Plan B. It just means that there will be... definitely more than 1 .con file. :D


    Thanks for clearing the topic up.

  • I am also having trouble with this: I want to be able to run a loop that checks the current year and at several milestones I'd change stocks for several industries.


    as far as I understand it in order to get the year, I need to write a script in the gui thread which sends data 'game.interface.getGameTime() . date.year' to a variable in 'save' using 'sendscriptevent' the whole process needs to be "handled" with handleEvent then I can save that value in "load" and the transference of information from gui thread to the engine thread will be complete. Meaning that I can finally use the value in my if statements.


    Is this right? it feels like way too complicated just to get a simple value of 4 digits. also if someone could help me with some examples, as the explanations I've found haven't really told me much and I have bee running tests all day to no avail.

  • Have a look at my Railway Warehouse mod. I use the construction year to determine which type of platform sign to add to the station.

    Specifically, on line 64 of dsd_warehouse_rail_2.script, I assign the incoming construction parameters to the local variable "params"

    Then, much later, on line 436, I test params.year against the value 1950 and build the signs accordingly.

    Keep in mind that this value (params.year) does not change once the particular construction is built - what you're getting is not the current year, but rather the year the station/asset/etc was first built at that particular location.

  • I am also having trouble with this: I want to be able to run a loop that checks the current year and at several milestones I'd change stocks for several industries.


    as far as I understand it in order to get the year, I need to write a script in the gui thread which sends data 'game.interface.getGameTime() . date.year' to a variable in 'save' using 'sendscriptevent' the whole process needs to be "handled" with handleEvent then I can save that value in "load" and the transference of information from gui thread to the engine thread will be complete. Meaning that I can finally use the value in my if statements.


    Is this right? it feels like way too complicated just to get a simple value of 4 digits. also if someone could help me with some examples, as the explanations I've found haven't really told me much and I have bee running tests all day to no avail.

    If you're running a game script, then you can call game.interface.getGameTime() directly in the engine thread. You don't need to call it in the gui thread to then use the horribly awkward serialization routines to get that value into the engine thread.

  • Thanks for the reply: i did take a look at it, but the date a particular building was constructed is not a value that i can use for what i wish to accomplish. I need the current in game year. the funny thing is that i left this part of my coding to the end because i figured that extracting the year was going to be the easiest part. the rest of my code works (just a simple set of if statements), I even made a table with static values and simulated my own year counter and works.

    now: I've been using this simple code to run tests,

    If I can make this one react to the year (though i eiminated that line as first i need to extract the value) I'm set, I can create most of my scripts using a macro and a spreadsheet.


    I'm already using updateFn to change the capacity here, how could i use the parameter "year" described here?: https://www.transportfever2.co…types#updatefn_parameters

  • Where are you running this code? Is this a construction modifier function within mod.lua? If so, it will not work, as that code is only run once, when the game loads.

    this is a construction modifier function within mod.lua. Yesterday my tests were with a runfn process and I was just checking if at start things would change as I wanted to. I would play with different parameters and start different games at different years and adjust things and save some pieces of code. Today I started using this simple script but now in the postrunfn. first i used a local variable within the mod.lua named Year and i'd set static numbers to that variable and again start at different times, now i can use loops because of the postrun but i didn't test them much. Next is when I discovered that getting the current year is not that simple (I just thought there was going to be some sort of global function, or at least one i coudl easily reference in a base game file) that eventually led me to this thread and I read what's been posted here. I created a new file in res/config/game_script but as i said the whole load, save thing is very confusing and I also feel that I'm not experienced enough with lua and I get confused when i see what appear to be syntax changes (at least to me).

  • If you're running a game script, then you can call game.interface.getGameTime() directly in the engine thread. You don't need to call it in the gui thread to then use the horribly awkward serialization routines to get that value into the engine thread.

    and how exactly do i use this? because if I don't have to do the whole update, handler stuff, then
    while this works in the in-game console:

    currentYear = game.interface.getGameTime() .data.year
    return currentYear

    in the script it keeps telling me that I'm trying to index a nil value (game and interface), then it also tells me that .data.year are unexpected adn of course now I'm left with a a function that's not in the file (getGameTime) and so the program tells me about how it doesn't support global functions, And that's what confuses me: I read several times the documentation they provided and I feel that their syntax changes or something is not really that clear.

  • res/config/game_script is where you need to be if you want code that is executed on an ongoing basis. Absolutely everything in mod.lua is executed at load time only, and is never touched again.

    As I mentioned above, you don't need to get hung up on the save/load serialization thing - just stick to the engine thread.

    The problem that is going to arise is this:

    You're going to need to define a custom set of production levels for the industry(ies) in question. That you can likely do in postRunFn, although I have not tried that myself.

    You will then need to trigger the updateFn from within the game_script while specifying the revised production level as one of the parameters. game.interface.getGameTime() will work here, so you can determine when you need to do this.

    I'm hoping someone else can chime in here, as you have reached the end of my knowledge on this. I'm sorry, I wish I could be of more help.

  • This is another mod I wrote that uses a game script (see line 36):

  • You're going to need to define a custom set of production levels for the industry(ies) in question. That you can likely do in postRunFn, although I have not tried that myself.

    first of all I really appreciate your help.

    for the values I was planning on linking to a table using the industry name as an id to search.

    I migrated the whole test to the game_script file, and while I'm not getting any error codes anymore I don't think I'm putting the postrun process in the correct place as nothing happen

BlueBrixx