Jump to content

Overriding components function


Recommended Posts

Hi ! I'm trying to override Health:OnUpdate function to add healthaura like does sanity with sanityaura.

I'm using AddComponentPostInit like recommanded in this thread but this is not working :/

Some code :

modmain.lua

AddComponentPostInit('health', function(self, inst)
    --Overriding OnUpdate
	local old_OnUpdate = self.OnUpdate
	self.OnUpdate = function(dt)
		old_OnUpdate(dt)
		self:Recalc(dt)
	end
	
	function self:Recalc(dt)
		local x,y,z = self.inst.Transform:GetWorldPosition()
		local aura_delta = 0;
		local ents = TheSim:FindEntities(x, y, z, 4)
		for i, v in ipairs(ents) do
			if v.components.healthaura ~= nil and v ~= self.inst then
				aura_delta = aura_delta + v.components.healthaura:GetAura(self.inst)
			end
		end
		self:DoDelta(aura_delta, true)
	end
end)

healthaura.lua

local HealthAura = Class(function(self, inst)
	self.inst = inst
	self.aura = 0
	self.aurafn = nil
end)

function HealthAura:GetAura(observer)
	return self.aurafn == nil and self.aura or self.aurafn(self.inst, observer)
end

return HealthAura

Adding healthaura component dynamically when my entity spawns, i add dynamically the component only for 1 bloom because i'm spawning a circle of them in a radius of 4, only the middle one need the healtharea component

-- Used to spawn specific variation of bloom
-- bloom1, bloom2, bloom3, bloom4, bloom5 or bloom6
local function createBloom()
	local i = math.ceil(math.random() * 6)
	return {prefab = SpawnPrefab("bloom"..i), index = i}
end

local function spawnBloomAt(pos)
    local bloom = createBloom()
    bloom.prefab.Transform:SetPosition(pos.x, pos.y, pos.z)
    bloom.prefab:AddComponent("healthaura")
    bloom.prefab.components.healthaura.aura = 1
    print(bloom.prefab.components.healthaura ~= nil) --true
end

 

I spawned the bloom on the ground, in a radius of 4, the healthaura component should be detected by the OnUpdate function from the player health component, and start regen the player health but this it's not working :/

What is wrong with my logic ?

 

Thanks for helping me :) Happy new year !

Edited by AireAyquaza
Link to comment
Share on other sites

Do you know where in the code it is failing to execute? It must be getting dropped somewhere along the line, throw some print() fns (like  print("test1") or something) in there to see what gets run and what doesn't. Try throwing one in each of the custom functions you added/changed and let us know which ones aren't showing up. Ctrl+L brings up the active log, so you should be able to see the print messages show up in there

Link to comment
Share on other sites

Hi ! I just tests that : 

AddComponentPostInit('health', function(self, inst)
	local old_OnUpdate = self.OnUpdate
	self.OnUpdate = function(dt)
		print('Test1')
		old_OnUpdate(dt)
		print('Test2')
		self:Recalc(dt)
		print('Test3')
	end
	
	function self:Recalc(dt)
		local x,y,z = self.inst.Transform:GetWorldPosition()
		local aura_delta = 0;
		-- Finding players
		local ents = TheSim:FindEntities(x, y, z, 4)
		for i, v in ipairs(ents) do
			if v.components.healthaura ~= nil and v ~= self.inst then
				aura_delta = aura_delta + v.components.healthaura:GetAura(self.inst)
			end
		end
		self:DoDelta(aura_delta, true)
	end
	
	self.LongUpdate = self.OnUpdate
end)

But no print is logged into log file

Link to comment
Share on other sites

instead of this

self.OnUpdate = function(dt)
		print('Test1')
		old_OnUpdate(dt)
		print('Test2')
		self:Recalc(dt)
		print('Test3')
	end

try this maybe?:

function self:OnUpdate(dt)
		print('Test1')
		self:old_OnUpdate(dt)
		print('Test2')
		self:Recalc(dt)
		print('Test3')
end

 

Link to comment
Share on other sites

I found why this is not working : in Sanity component, in constructor, there is self.inst:StartUpdatingComponent(self) allowing the component to be updated everytime

In health components there is not this code line

So there is my new code part :

AddComponentPostInit('health', function(self, inst)
	local old_OnUpdate = self.OnUpdate
	self.OnUpdate = function(dt)
		old_OnUpdate(dt)
		print('Test')
		self:Recalc(dt)
	end
	
	function self:Recalc(dt)
		local x,y,z = self.inst.Transform:GetWorldPosition()
		local aura_delta = 0;
		-- Finding entities
		local ents = TheSim:FindEntities(x, y, z, 4)
		for i, v in ipairs(ents) do
			if v.components.healthaura ~= nil and v ~= self.inst then
				aura_delta = aura_delta + v.components.healthaura:GetAura(self.inst)
			end
		end
		self:DoDelta(aura_delta, true)
	end
	
	self.LongUpdate = self.OnUpdate
    -- Start update the component (method from entityscript.lua)
	self.inst:StartUpdatingComponent(self) -- This line make server crash and not starts
end)

-- Makes hounds have a healthaura
AddPrefabPostInit('hound', function(inst)
	inst:AddComponent('healthaura')
	inst.components.healthaura.aura = 1
end)

But the added line "self.inst:StartUpdatingComponent(self)" make the server crashing, i found nothing about this crash in the log file :/

Any idea ? I'm searching from my side

 

EDIT: I Think it's because the component is not initialized, so i can't update it

EDIT 2 : I just solved the issue by adding an event on locomote component to start updating health component, thx for your help :)

server_log.txt

Edited by AireAyquaza
Issue solved
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...