Jump to content

Performance question


Recommended Posts

So... I wanted to make a character, and get him to be attacked by both pigman and spiders, and I had the idea of copying the ReTarget from the pigman prefab and pasting it in the AddPrefabPostInit with the changes I wanted.

local function PigmanPostInit(pig)	local function NormalRetargetFn(inst)		return GLOBAL.FindEntity(			inst,			TUNING.PIG_TARGET_DIST,			function(guy)				return (guy.LightWatcher == nil or guy.LightWatcher:IsInLight())					and inst.components.combat:CanTarget(guy)			end,			{"_health", "_combat"}, -- see entityreplica.lua			inst.components.follower.leader ~= nil and			{ "playerghost", "INLIMBO", "abigail" } or			{ "playerghost", "INLIMBO" },			{"monster", "esc"}		)	end	pig.components.combat:SetRetargetFunction(3, NormalRetargetFn)endAddPrefabPostInit("pigman", PigmanPostInit)

It works as expected, but I have a question regarding Add***Init functions, is my piece of code "replacing" the execution of the old SetRetargetFunction() from the pigman.lua prefab, or is the game executing the origianl prefab one and then executing my function to finally place the results from the one I made instead.

 

Just wanted some insight of how the mod handling actually works.

Link to comment
Share on other sites

@CremeLover, in order to make it compatible with other mods you will need to do something like the following:

local function PigmanPostInit( self )    if self.components.combat then        local _targetfn = self.components.combat.targetfn                self.components.combat.targetfn = function( inst )            return _targetfn(inst) and --[[ input new code here. ]]        end    end    return selfendAddPrefabPostInit("pigman", PigmanPostInit) 

Note: This is simply an example of how to create compatible functions with other mods. We honestly need a better way to do this because if a huge amount of mods are doing it for the same prefab you're going to get some tremendous performance issues.

Link to comment
Share on other sites

@Kzisor I can see how that could become a huge deal for a server running multiple mods, and I'm not a programmer expert enought to provide for a proper solution other than chargin all the functions into a big dumpster and then executing only the ones with the higher priority, and that's not really a fix.

 

And thank you again for the big help. I really appreciate it ^^.

Edited by CremeLover
Link to comment
Share on other sites

If you put a

inst:AddTag("monster")

then spiders will hate you and pigmen will hate you.

 

No extra stuff needed, in this case.

 

 

EDIT: I stand corrected, there are some extra checks for warriors that take monster into account.

Edited by DarkXero
Link to comment
Share on other sites

@Kzisor That sounds amaizing! But depending on how you do it, not only Klei, but all the mod creators will have to rewrite their code, although that's how most IOS apps works today. Besides, those guys at Klei are awesome, if your method is worth it, they will surely change a few lines of code in the game. If they have spare people for the job that is. Wish you the best of luck!

 

@DarkXero That was actually the first thing I tried, and both spiders, warrior and normal ignored me, so I tried a different approach. I was trying another way, but if FindEntity doesn't find a suitable entity, it returns nil. So unless I find a way to access other entities that were found, or another workaround, I think I'm gonna have to write a second FindEntity function after the first one returns nil. Not really what I was looking for, but it fixes the compatibility issue.

local function PigmanPostInit(inst)	local _targetfn = inst.components.combat.targetfn		local function NormalRetargetFn(inst, objetive)		return _targetfn(inst) or GLOBAL.FindEntity(			inst,			TUNING.PIG_TARGET_DIST,			function(guy)				return (guy.LightWatcher == nil or guy.LightWatcher:IsInLight())					and inst.components.combat:CanTarget(guy)			end,			{ "esc", "_combat" },			inst.components.follower.leader ~= nil and			{ "playerghost", "INLIMBO", "abigail" } or			{ "playerghost", "INLIMBO" }		)	end	inst.components.combat:SetRetargetFunction(3, NormalRetargetFn)end
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...