Overriding a function?


Lycaon

Recommended Posts

Sorry about the potentially stupid question, but I am used to more... structured languages like C++....  If I need to modify a function in the perishable.lua component, the only way I've seen to do this is to actually yoink the file and edit it, and place it in my own mod folder.  I suspect this is a terrible practice and would break compatibility with any other mod that also made modifications to perishable.lua.

 

Is there any way for me to properly do this?   Following code example illustrates what I'd like to do.

 

local old_func = Perishable.Update

 

Perishable.Update = function(inst, dt)

    if somethingiwanttohandle then

        dostuffiwanttodo

    else

        old_func(inst, dt)

    end

end

Link to comment
Share on other sites

I've gone with attempting to override Start/StopPerishing, but the game is still crashing with "attempt to index self (a nil value) in 'old_startperishing'

 

Is there some caveat I don't know about, like needing to pass the current self to the old function?  I have the following code, do I need to literally call old_startperishing(self)?

 

local Perishable = require("components/perishable")
 
local old_startperishing = Perishable.StartPerishing
 
function Perishable:StartPerishing()
if self == nil then return end
if self.inst == nil then return end
 
local owner = self.inst.components.inventoryitem and self.inst.components.inventoryitem.owner or nil
if owner and owner:HasTag("statis") then
return
else
return old_startperishing()
end
end
Link to comment
Share on other sites

 

I've gone with attempting to override Start/StopPerishing, but the game is still crashing with "attempt to index self (a nil value) in 'old_startperishing'

 

Is there some caveat I don't know about, like needing to pass the current self to the old function?  I have the following code, do I need to literally call old_startperishing(self)?

 

Yes, just old_startperishing(self), since 'self' is implicitly passed on : operator but you have to call it directly here, you have to supply it manually.

 

Link to comment
Share on other sites

Thank you all for the information so far.  I'm still getting crashes with the mod, though.  I have tried to switch to events (itemget and itemlose) for the container, and have set up two functions and added listeners like so (the following code is all smashed together, it's not how I have the file actually structured)

 

Nevermind on the crash.  damn period versus colon on :StopPerishing

local function OnItemGet(inst, item, slot, pos)	if item.item.components.perishable then		item.item.components.perishable.StopPerishing(self)		print("Stopping perishing of item " .. item.item.name)	endendlocal function OnItemLost(slot)	print("Type: " .. type(slot))endlocal function fn(Sim)    -- other initialization junk    inst.AddComponent("container")    inst:ListenForEvent("itemlose", OnItemLost)    inst:ListenForEvent("itemget", OnItemGet)    return instend

The mod loads, but when I try to put something in the chest, the game crashes wish "self is undeclared" with a stacktrace referencing 'fn' in my prefab file, which stems from a PushEvent call in container.lua.

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.