Maris Posted April 11, 2015 Share Posted April 11, 2015 (edited) 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 April 12, 2015 by Maris Link to comment Share on other sites More sharing options...
DarkXero Posted April 11, 2015 Share Posted April 11, 2015 An example for the usefulness of this? What would you like to PreInit in a player? Link to comment Share on other sites More sharing options...
Maris Posted April 12, 2015 Author Share Posted April 12, 2015 Useful function is AddPlayersPostInit Link to comment Share on other sites More sharing options...
rezecib Posted April 12, 2015 Share Posted April 12, 2015 @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 More sharing options...
Maris Posted April 12, 2015 Author Share Posted April 12, 2015 (edited) 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) endendHere is a bug. This code does not affect character mods with lower priority. Edited April 12, 2015 by Maris Link to comment Share on other sites More sharing options...
Maris Posted April 12, 2015 Author Share Posted April 12, 2015 Also it doesn't matter how many mods will use this code. There will be no conflict between mods. Link to comment Share on other sites More sharing options...
DarkXero Posted April 12, 2015 Share Posted April 12, 2015 @rezecib, you are right, I wasn't thinking efficiency but functionality. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now