Jump to content

Recommended Posts

Add the following code to the beginning of modmain.lua:

_G=GLOBALif not _G.rawget(_G,"mods") then _G.rawset(_G,"mods",{}) endif not _G.mods.player_preinit_fns then	_G.mods.player_preinit_fns={}	--Dirty hack	local old_MakePlayerCharacter = _G.require("prefabs/player_common")	local function new_MakePlayerCharacter(...)		local inst=old_MakePlayerCharacter(...)		for _,v in ipairs(_G.mods.player_preinit_fns) do			v(inst)		end		return inst	end	_G.package.loaded["prefabs/player_common"] = new_MakePlayerCharacterendfunction AddPlayersPreInit(fn)	table.insert(_G.mods.player_preinit_fns,fn)endlocal player_postinit_fns = {}function AddPlayersPostInit(fn)	table.insert(player_postinit_fns,fn)endlocal done_players = {}AddPlayersPreInit(function(inst)	local s = inst.prefab or inst.name	if not done_players[s] then		done_players[s] = true		AddPrefabPostInit(s,function(inst)			for _,v in ipairs(player_postinit_fns) do				v(inst)			end		end)	endend)

 

 

Then you can initialize all players by this function:

--For ALL playersAddPlayersPostInit(function(inst)	--Your code here	inst:AddTag("yes_yes_postinit_done")	inst.was_post_init = true	--(This is an axample)end)

​Also you can use PREinit function. It will be executed just after player_common code and before custom code of character prefab:

--For ALL playersAddPlayersPreInit(function(inst)	--Your code here	inst:AddTag("yes_yes_PRE_init_done")	inst.was_pre_init = true	--(This is an axample)end)
Edited by Maris
Link to comment
Share on other sites

@DarkXero, I think Maris was trying to show a more efficient implementation of AddPlayerPostInit, which of course has a variety of uses. The provided implementation actually runs as a postinit on all prefabs, checking if it has the tag "player", and runs if so. Which is pretty atrocious when there are ~10 players and thousands of non-player prefabs. To be honest, I think this is something to bring up with Peter-- a better AddPlayerPostInit (and probably some other player hooks that are a bit more organized/obviously integrated into the mod API) would be quite helpful.

Link to comment
Share on other sites

One more thing. For example, there are 2 character mods, and each mod needs to use AddPlayersPostInit. All will work fine. And it does not matter, what is priority and what actual order of loading mods.

 

Old code that I was using:

function AddPlayersPostInit(fn)	for i,v in ipairs(_G.DST_CHARACTERLIST) do		AddPrefabPostInit(v,fn)	end	for i,v in ipairs(_G.MODCHARACTERLIST) do		AddPrefabPostInit(v,fn)	endend

Here is a bug. This code does not affect character mods with lower priority.

Edited by Maris
Link to comment
Share on other sites

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
 Share

×
  • Create New...