Transport Fever 2

Transport Fever 2

h4e API
 This topic has been pinned, so it's probably important
homieforever  [developer] 26 Aug, 2024 @ 3:34pm
Modding Documentation
Modding Documentation

All accessible functions are described here one by one and examples are also shown.

h4e.version

Returns the current version of the API used. For version 1.20, 20 is returned.

h4e.api.getRepo(repoName)

Returns the list of all installed and dynamic resources of the desired repo. repoName is the name of the repo according to the modding documentation of Transport Fever 2:

  • autoGroundTexRep
  • bridgeTypeRep
  • buildingTypeRep
  • cargoTypeRep
  • constructionRep
  • moduleRep
  • multipleUnitRep
  • railroadCrossingTypeRep
  • streetTypeRep
  • trackTypeRep
  • trafficLightTypeRep
  • tunnelTypeRep

Return format:

{ [0] = "filePathToRes.lua", }

Example for modular_station.con

Vanilla Code:

local function makeEmptyTrack(type, catenary) return { type = "TRACK", params = { type = type, catenary = catenary, }, edges = { }, snapNodes = { }, tag2nodes = { }, } end

Will be changed to:

local function makeEmptyTrack(type, catenary) return { type = "TRACK", params = { type = type, catenary = catenary, }, edges = { }, snapNodes = { }, tag2nodes = { }, } end local function findOrMakeNewEdgeListNum(result, trackType, catenary) for k,v in pairs(result.edgeLists) do if v.params.type == trackType and v.params.catenary == catenary then return k end end result.edgeLists[#result.edgeLists + 1] = makeEmptyTrack(trackType, catenary) return #result.edgeLists end

Vanilla Code:

result.edgeLists = { makeEmptyTrack("standard.lua", false), makeEmptyTrack("standard.lua", true), makeEmptyTrack("high_speed.lua", false), makeEmptyTrack("high_speed.lua", true), }

Will be changed to:

result.edgeLists = { } findOrMakeNewEdgeListNum(result, "standard.lua", false) findOrMakeNewEdgeListNum(result, "standard.lua", true) findOrMakeNewEdgeListNum(result, "high_speed.lua", false) findOrMakeNewEdgeListNum(result, "high_speed.lua", true) for k, trackType in pairs(h4e.api.getRepo("trackTypeRep")) do findOrMakeNewEdgeListNum(result, trackType, false) findOrMakeNewEdgeListNum(result, trackType, true) end

And that's it, the station can also handle mod tracks! If, for example, it is desired that the track can already be selected in the params, getRepo can also be used there to retrieve the tracks.

Important: h4e API as a prerequisite

Now that your mod can use various functions of the h4e API, only one thing is important. Add the h4e API under Steam as a required item and point out in the description that the h4e API must be loaded for your mod to function fully. Once this is done you can check back here and post your mod!
Last edited by homieforever; 30 Aug, 2024 @ 9:22pm