Lycaon Posted December 5, 2014 Share Posted December 5, 2014 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) endend Link to comment Share on other sites More sharing options...
DeathDisciple Posted December 5, 2014 Share Posted December 5, 2014 Your example is exactly how you can/should do it, you may want to check this thread for example/detailed explanation. Link to comment Share on other sites More sharing options...
Lycaon Posted December 6, 2014 Author Share Posted December 6, 2014 That's what I thought, but when I try that, I get "attempt to index 'self' (a nil value) in the class constructor in the -untouched- unedited perishable.lua file. Link to comment Share on other sites More sharing options...
Mobbstar Posted December 6, 2014 Share Posted December 6, 2014 There's a ComponentPostInit function (in modutil.lua I think). I never used it, but maybe it can help here? Link to comment Share on other sites More sharing options...
DeathDisciple Posted December 6, 2014 Share Posted December 6, 2014 Oh, sorry I didn't look at the actual file, I had erroneously assumed it's class function. There is no legit way to override local function. I suppose you could either override longupdate/startperishing themselves. Or maybe there is some way to abuse debug lib. Link to comment Share on other sites More sharing options...
Lycaon Posted December 6, 2014 Author Share Posted December 6, 2014 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 endif self.inst == nil then return end local owner = self.inst.components.inventoryitem and self.inst.components.inventoryitem.owner or nilif owner and owner:HasTag("statis") thenreturnelsereturn old_startperishing()endend Link to comment Share on other sites More sharing options...
DeathDisciple Posted December 6, 2014 Share Posted December 6, 2014 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 More sharing options...
simplex Posted December 7, 2014 Share Posted December 7, 2014 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.^In Lua,foo:baz(x, y, z)is syntactic sugar forfoo.baz(foo, x, y, z) Link to comment Share on other sites More sharing options...
Lycaon Posted December 9, 2014 Author Share Posted December 9, 2014 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 :StopPerishinglocal 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 instendThe 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 More sharing options...
chromiumboy Posted December 10, 2014 Share Posted December 10, 2014 In line 3, self is used, but that local function only has inst, item, slot, pos as inputs. You probably just need to change 'self' to 'inst' in that line. Or maybe 'item'. It's hard to tell from your code what you are trying to achieve. Link to comment Share on other sites More sharing options...
Recommended Posts
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.