Jump to content

Issue with reassigning functions


Recommended Posts

I've been trying to get this code to work:

inst.oldStage1 = inst.components.growable.stages[1].fn
inst.components.growable.stages[1].fn = function(inst)
	print("test")
	inst:oldStage1() 
end

This works fine for the first entity, but then the second entity's stages[1].fn is the first entity's new stages[1].fn

Visualization:

Entity1: oldfunction = fn1, newfunction = fn2

Entity2: oldfunction = fn2, newfunction = fn2 (issue is caused here because oldfunction is Entity1's newfunction)

What I want is - Entity2: oldfunction = fn3, newfunction = fn4

I don't know how they are even interacting. Any idea what I could be doing wrong?

 

Edited by Aquaterion
Link to comment
Share on other sites

The "inst.components.growable.stages" table is shared among all instances, you only need to change it once. I recommend using a boolean to prevent editing it twice:

local growablepatched = false
AddPrefabPostInit('PREFAB', function(inst)
    -- only do this once (shared functions due to shared table)
	if not growablepatched then
		local _SetSmall = inst.components.growable.stages[1].fn
		inst.components.growable.stages[1].fn = function(self)
			_SetSmall(self)
            inst.AnimState:SetMultColour(0,0,0,1) -- sample code
		end

		growablepatched = true
	end
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...