Jump to content

Trouble removing component


Recommended Posts

I need help with something. Although I´m a seasoned programmer, It´s my first time in lua and modding for DS/DST and I´m having a hard time with something. Sorry to bother you guys, I tried really hard but I hit the wall.
 
I want to replace the hunter component, the one that handles all the animal tracks and dirt piles and Koalefant spawning. It is part of the forest prefab.
 
Code is simple and goes like this:

function ReplaceHunter (inst)	print "replacing hunter component"	inst:RemoveComponent("hunter")	--inst:AddComponent("betterhunter")endAddPrefabPostInit("forest", ReplaceHunter)

So, the RemoveComponent works but the game crashes because the constructor of the hunter component registers a DoTaskInTime:

local Hunter = Class(function(self, inst)  -- stuffself.inst:DoTaskInTime(0, function(inst) inst.components.hunter:StartCooldown() end)end)

The crash comes when the scheduler fires and ".hunter" reference is nil, because I destroyed it 1 frame before.

 

I tried looking at pendingtasks list of Periodic objects so I could Cancel() it but the problem is that once the task is scheduled, I can´t identify from which component it originated, because the GUID that is passed to the scheduler is entities´. I also can´t cancel all of them, because of course that could break half the world and other mods.

 

So, unless someone more knowledgeable has an easy solution, I suppose I will have to inject code into Klei´s hunter.lua (modifying some of its methods) so it stops firing scheduler tasks and instead fires a callback function in modmain.lua that removes the component when it is safe to do so. 

 

I thank you all in advance for any better idea!

 

Edited by Brain
Link to comment
Share on other sites

@Brain

function ReplaceHunter (inst)    print "replacing hunter component"    self.inst:DoTaskIntTime(1, function(inst) inst:RemoveComponent("hunter") end)    --inst:AddComponent("betterhunter")end AddPrefabPostInit("forest", ReplaceHunter)
Link to comment
Share on other sites

If you are replacing the hunter component by your betterhunter component, what you can do is just rename betterhunter into hunter.

That way your component will override all uses of hunter, because that's what the game will load.

You would have to include all the operations of hunter though (just in case something from outside requires the hunter component), plus the new ones you want to use.

 

Alternatively, if you want to make the hunter component exist but be void, instead on injecting and doing weird callbacks, you can put a hunter component file in your mod, that will overwrite the current hunter, that has everything empty, like:

return Class(function(self, inst)	self.inst = inst	OnUpdateHunt = function(hunt)	end	function self:OnDirtInvestigated(pt, doer)	end	function self:LongUpdate(dt)	end	function self:GetDebugString()	endend)

Edited by DarkXero
Link to comment
Share on other sites

@Kzisor, smart thinking but that won´t work because the task that runs in frame 0 will schedule another, and they keep scheduling things forever.

 

@DarkXero, thank you. I didn´t know that equal names would force an override. Anyway I was trying to avoid any override of hunter.lua because someone might make a mod that uses it together with my mod, or any other similar mod. After all they are all spawners of dirt piles, animal tracks and creatures, you could in theory have all of them together.

 

Your suggestion gave me a solution. Instead of replacing the components, I´m gonna let them both exist in parallel, and I´m gonna research a way to just disable its functionality... either calling some method or messing with TUNING. There is really no need to deallocate it. I THINK that calling Hunter:StopCooldown() at frame 1 as @Kzisor hinted could just make it stay idle until someone calls StartCooldown(). Will have to dig into the code more.

 

Thanks a lot guys.

 

By the way as soon as I have a basic prototype working I´m going to post the idea of the mod on the forum for public critique / suggestions / feedback.

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...