Jump to content

Recommended Posts

Hello,

   Is there a list of functions like AddPlayerPostInit and AddClassPostConstruct and what they do?  I tried to search for one but was unable to since I am not sure what they are called in the API. Are these events,  components or what?

 

Good day.

@Molex1701, These are functions that are made specifically for the mod environment, they are defined in data/scripts/modutil.lua:

 

local function InsertPostInitFunctions(env, isworldgen)	env.postinitfns = {}	env.postinitdata = {}	env.postinitfns.LevelPreInit = {}	env.AddLevelPreInit = function(levelid, fn)		initprint("AddLevelPreInit", levelid)		if env.postinitfns.LevelPreInit[levelid] == nil then			env.postinitfns.LevelPreInit[levelid] = {}		end		table.insert(env.postinitfns.LevelPreInit[levelid], fn)	end	env.postinitfns.LevelPreInitAny = {}	env.AddLevelPreInitAny = function(fn)		initprint("AddLevelPreInitAny")		table.insert(env.postinitfns.LevelPreInitAny, fn)	end	env.postinitfns.TaskPreInit = {}	env.AddTaskPreInit = function(taskname, fn)		initprint("AddTaskPreInit", taskname)		if env.postinitfns.TaskPreInit[taskname] == nil then			env.postinitfns.TaskPreInit[taskname] = {}		end		table.insert(env.postinitfns.TaskPreInit[taskname], fn)	end	env.postinitfns.RoomPreInit = {}	env.AddRoomPreInit = function(roomname, fn)		initprint("AddRoomPreInit", roomname)		if env.postinitfns.RoomPreInit[roomname] == nil then			env.postinitfns.RoomPreInit[roomname] = {}		end		table.insert(env.postinitfns.RoomPreInit[roomname], fn)	end	env.AddLevel = function(...)		arg = {...}		initprint("AddLevel", arg[1], arg[2].id)		require("map/levels")		AddLevel(...)	end	env.AddTask = function(...)		arg = {...}		initprint("AddTask", arg[1])		require("map/tasks")		AddTask(...)	end	env.AddRoom = function(...)		arg = {...}		initprint("AddRoom", arg[1])		require("map/rooms")		AddRoom(...)	end	env.AddGameMode = function(game_mode, game_mode_text)		initprint("AddGameMode", game_mode, game_mode_text)		require("gamemodes")		return AddGameMode(game_mode, game_mode_text)	end	env.GetModConfigData = function( optionname, get_local_config )		initprint("GetModConfigData", optionname, get_local_config)		return GetModConfigData(optionname, env.modname, get_local_config)	end	env.postinitfns.GamePostInit = {}	env.AddGamePostInit = function(fn)		initprint("AddGamePostInit")		table.insert(env.postinitfns.GamePostInit, fn)	end	env.postinitfns.SimPostInit = {}	env.AddSimPostInit = function(fn)		initprint("AddSimPostInit")		table.insert(env.postinitfns.SimPostInit, fn)	end	env.AddGlobalClassPostConstruct = function(package, classname, fn)		initprint("AddGlobalClassPostConstruct", package, classname)		AddGlobalClassPostConstruct(package, classname, fn)	end	env.AddClassPostConstruct = function(package, fn)		initprint("AddClassPostConstruct", package)		AddClassPostConstruct(package, fn)	end	------------------------------------------------------------------------------	-- Everything above this point is available in Worldgen or Main.	-- Everything below is ONLY available in Main.	-- This allows us to provide easy access to game-time data without	-- breaking worldgen.	------------------------------------------------------------------------------	if isworldgen then		return	end	------------------------------------------------------------------------------	env.AddAction = function( id, str, fn )		local action        if type(id) == "table" and id.is_a and id:is_a(Action) then			--backwards compatibility with old AddAction            action = id        else			assert( str ~= nil and type(str) == "string", "Must specify a string for your custom action! Example: \"Perform My Action\"")			assert( fn ~= nil and type(fn) == "function", "Must specify a fn for your custom action! Example: \"function(act) --[[your action code]] end\"")			action = Action()			action.id = id			action.str = str			action.fn = fn		end		action.mod_name = env.modname		assert( action.id ~= nil and type(action.id) == "string", "Must specify an ID for your custom action! Example: \"MYACTION\"")					initprint("AddAction", action.id)		ACTIONS[action.id] = action		--put it's mapping into a different IDS table, one for each mod		if ACTION_MOD_IDS[action.mod_name] == nil then			ACTION_MOD_IDS[action.mod_name] = {}		end		table.insert(ACTION_MOD_IDS[action.mod_name], action.id)		ACTIONS[action.id].code = #ACTION_MOD_IDS[action.mod_name]		STRINGS.ACTIONS[action.id] = action.str				return ACTIONS[action.id]	end	env.AddComponentAction = function(actiontype, component, fn)		-- just past this along to the global function		AddComponentAction(actiontype, component, fn, env.modname)	end	env.postinitdata.MinimapAtlases = {}	env.AddMinimapAtlas = function( atlaspath )		initprint("AddMinimapAtlas", atlaspath)		table.insert(env.postinitdata.MinimapAtlases, atlaspath)	end	env.postinitdata.StategraphActionHandler = {}	env.AddStategraphActionHandler = function(stategraph, handler)		initprint("AddStategraphActionHandler", stategraph)		if not env.postinitdata.StategraphActionHandler[stategraph] then			env.postinitdata.StategraphActionHandler[stategraph] = {}		end		table.insert(env.postinitdata.StategraphActionHandler[stategraph], handler)	end	env.postinitdata.StategraphState = {}	env.AddStategraphState = function(stategraph, state)		initprint("AddStategraphState", stategraph)		if not env.postinitdata.StategraphState[stategraph] then			env.postinitdata.StategraphState[stategraph] = {}		end		table.insert(env.postinitdata.StategraphState[stategraph], state)	end	env.postinitdata.StategraphEvent = {}	env.AddStategraphEvent = function(stategraph, event)		initprint("AddStategraphEvent", stategraph)		if not env.postinitdata.StategraphEvent[stategraph] then			env.postinitdata.StategraphEvent[stategraph] = {}		end		table.insert(env.postinitdata.StategraphEvent[stategraph], event)	end	env.postinitfns.StategraphPostInit = {}	env.AddStategraphPostInit = function(stategraph, fn)		initprint("AddStategraphPostInit", stategraph)		if env.postinitfns.StategraphPostInit[stategraph] == nil then			env.postinitfns.StategraphPostInit[stategraph] = {}		end		table.insert(env.postinitfns.StategraphPostInit[stategraph], fn)	end	env.postinitfns.ComponentPostInit = {}	env.AddComponentPostInit = function(component, fn)		initprint("AddComponentPostInit", component)		if env.postinitfns.ComponentPostInit[component] == nil then			env.postinitfns.ComponentPostInit[component] = {}		end		table.insert(env.postinitfns.ComponentPostInit[component], fn)	end	-- You can use this as a post init for any prefab. If you add a global prefab post init function, it will get called on every prefab that spawns.	-- This is powerful but also be sure to check that you're dealing with the appropriate type of prefab before doing anything intensive, or else	-- you might hit some performance issues. The next function down, player post init, is both itself useful and a good example of how you might	-- want to write your global prefab post init functions.	env.postinitfns.PrefabPostInitAny = {}	env.AddPrefabPostInitAny = function(fn)		initprint("AddPrefabPostInitAny")		table.insert(env.postinitfns.PrefabPostInitAny, fn)	end	-- An illustrative example of how to use a global prefab post init, in this case, we're making a player prefab post init.	env.AddPlayerPostInit = function(fn)		env.AddPrefabPostInitAny( function(inst)			if inst and inst:HasTag("player") then fn(inst) end		end)	end	env.postinitfns.PrefabPostInit = {}	env.AddPrefabPostInit = function(prefab, fn)		initprint("AddPrefabPostInit", prefab)		if env.postinitfns.PrefabPostInit[prefab] == nil then			env.postinitfns.PrefabPostInit[prefab] = {}		end		table.insert(env.postinitfns.PrefabPostInit[prefab], fn)	end	-- the non-standard ones	env.AddBrainPostInit = function(brain, fn)		initprint("AddBrainPostInit", brain)		local brainclass = require("brains/"..brain)		if brainclass.modpostinitfns == nil then			brainclass.modpostinitfns = {}		end		table.insert(brainclass.modpostinitfns, fn)	end	env.AddIngredientValues = function(names, tags, cancook, candry)		require("cooking")		initprint("AddIngredientValues", table.concat(names, ", "))		AddIngredientValues(names, tags, cancook, candry)	end	env.cookerrecipes = {}	env.AddCookerRecipe = function(cooker, recipe)		require("cooking")		initprint("AddCookerRecipe", cooker, recipe.name)		AddCookerRecipe(cooker, recipe)		if env.cookerrecipes[cooker] == nil then	        env.cookerrecipes[cooker] = {}	    end	    if recipe.name then	        table.insert(env.cookerrecipes[cooker], recipe.name)	    end	end	env.AddModCharacter = function(name, gender)		initprint("AddModCharacter", name, gender)		AddModCharacter(name, gender)	end	env.AddRecipe = function(...)		arg = {...}		initprint("AddRecipe", arg[1])		require("recipe")		mod_protect_Recipe = false		local rec = Recipe(...)		mod_protect_Recipe = true		rec:SetModRPCID()		return rec	end		env.Recipe = function(...)		print("Warning: function Recipe in modmain is deprecated, please use AddRecipe")		return env.AddRecipe(...)	end	env.Prefab = Prefab	env.Asset = Asset	env.Ingredient = Ingredient	env.LoadPOFile = function(path, lang)		initprint("LoadPOFile", lang)		require("translator")		LanguageTranslator:LoadPOFile(path, lang)	end	env.RemapSoundEvent = function(name, new_name)		initprint("RemapSoundEvent", name, new_name)		TheSim:RemapSoundEvent(name, new_name)	end	env.AddReplicableComponent = function(name)		initprint("AddReplicableComponent", name)		AddReplicableComponent(name)	end	env.AddModRPCHandler = function(namespace, name, fn)		initprint("AddModRPCHandler", namespace, name)		AddModRPCHandler(namespace, name, fn)	end	env.SendModRPCToServer = function(id_table)		initprint("SendModRPCToServer", id_table.namespace, id_table.id)		SendModRPCToServer(id_table)	end	env.MOD_RPC = MOD_RPC    env.SetModHUDFocus = function(focusid, hasfocus)        initprint("SetModHUDFocus", focusid, hasfocus)        if ThePlayer == nil or ThePlayer.HUD == nil then            print("WARNING: SetModHUDFocus called when there is no active player HUD")        end        ThePlayer.HUD:SetModFocus(env.modname, focusid, hasfocus)    end    end 

 

There's also an API examples mod that goes through very basic usage of all of them.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...