Mod Settings (Modder)

Willkommen in der Transport Fever Community

Welcome to the fan community of Transport Fever and Train Fever, the economic simulators of Urban Games. The community is free for you to share and inform yourself about the game. We cultivate a friendly and objective interaction with each other and our team will be happy to answer any questions you may have.

 

Registration and use is of course free for you.

 

We wish you a lot of fun and hope for active participation.

The Team of the Transport-Fever Community

  • A short tutorial, to explain the usage of the new "Mod Settings" inside TPFMM.

    1 Introduction


    I was often annoyed that I had to supply two only slightly different mods or a main mod with submods, when adjusting a mod to the personal preferences of the users. So I created a simple Lua script (settings.lua), located in the mod folder, which contains all options in the form option = value.

    Code: settings.lua
    return {
      option1 = true,
      option2 = 4,
    }

    This file can either be read directly or with the repective functions of my modutil script (just copy the content of this archive to the mod folder). The first case is a bit more complicated, which means you should only use it, if you know Lua quite well. Therefore this tutorial will focus on the second possbility.


    2 Defining options


    Beacuse editing this file with a texteditor might be to complicated for many users, @Xanos added a new window to his TPFMM, where the options can be edited with a graphical user interface. A big "thanks" for that. :thumbup: Of course TPFMM can only display the options, if it knows what options exist and what values are possible for these options.
    We decided to read the required information from the mod.lua, which means it has to be defined there first. You can do that with the entry settings, in addition to the entry info, whre you can define as many options as you like:

    Code: mod.lua
    function data()
        return {
            info = { 
                -- ...
            },
            settings = { -- options },
        }
    end

    Each option can/must have different parameters:

    The option ID should be unique for this mod. In other words, only the last defined option with this ID will be used.
    If not stated otherwise, the given values for the optional parameters are also their default values.


    When using the modutil script, it's usefull to store the options inside a variable, which can then be used for both TPFMM and the script:

    The line modUtil.userSettings.create("mod_id", settings_def) tells the script, what options it can expect. In addition, the settings.lua file for this mod will be read (if it exists) and its data will be stored for later use. "mod_id" is used to link the settings to a specific mod. Therefore two different mods should not have the same id. That said, I think the name of the mod folder would be a good choice.


    3 Reading options


    After reading the settings.lua with the script, the values of the options can be accessed every time and in every script file (runFn inside mod.lua, .con, .mdl, .msh, etc.). If you want to access the options immediately after the definition (still inside the runFn), I recommend this code:

    Code
    local options = modUtil.userSettings.get("mod_id")

    For all other files, this code is better (otherwise you would have to include the modutil script in every file):

    Code
    if merk_modutil and merk_modutil[1] then
        local options = merk_modutil[1].userSettings.get("mod_id")
        -- process settings
    end

    In both cases, "mod_id" decides from which mod the settings are taken. This will likely be the same id as in "create", but you could also use the settings of another mod (e.g. if your own mod only extends this mod and therefore uses the same options). To access a single option, you can use options.option1.

Share