ThatGourmand Posted April 17, 2020 Share Posted April 17, 2020 Hi folks, I'm trying to add some stuff to the existing eater:Eat function, seemed simple enough. Like so: local function EaterPostInit(self) local oldEat = self.Eat function self:Eat(food) oldEat(food) --custom function goes here end end AddComponentPostInit("eater", EaterPostInit) The problem with this is that when saving the original Eat function to a local variable and calling it afterwards will always result in the following error: /data/scripts/components/eater.lua:119: attempt to call method 'CanEat' (a nil value) This error is caused by the CanEat function of Eater that gets called in the original Eater:Eat: function Eater:Eat( food ) if self:CanEat(food) then -- etc, etc The thing that I don't get is that I thought that in local function EaterPostInit(self) the "self" parameter would refer to the Eater component, and would contain all its properties like, monsterimmune, strongstomach, etc but also its methods. Although I would be able to call self.CanEat(food) inside of my EaterPostInit function, it won't recoginise that method when the original Eat method calls it. If anyone could explain this to me it would be great, this stuff is driving me insane, sanity - 100 Link to comment Share on other sites More sharing options...
CarlZalph Posted April 17, 2020 Share Posted April 17, 2020 2 hours ago, ThatGourmand said: Hi folks, I'm trying to add some stuff to the existing eater:Eat function, seemed simple enough. Like so: local function EaterPostInit(self) local oldEat = self.Eat function self:Eat(food) oldEat(food) --custom function goes here end end AddComponentPostInit("eater", EaterPostInit) The problem with this is that when saving the original Eat function to a local variable and calling it afterwards will always result in the following error: /data/scripts/components/eater.lua:119: attempt to call method 'CanEat' (a nil value) This error is caused by the CanEat function of Eater that gets called The problem lies with that you're not passing self into the oldEat function. oldEat(food) -> oldEat(self, food) Me, personally, I'd setup the function as such as it's more explicit: local function EaterPostInit(self) local oldEat = self.Eat self.Eat = function(self, food, ...) local retval = oldEat(self, food, ...) -- custom code return retval end end AddComponentPostInit("eater", EaterPostInit) The dots are to help keep it future proof in case Klei edits the function down the line, as well as having a return value. To make it further futureproof, pack the return value as a table and then unpack when you return it so it supports arbitrary-lengthed return value counts. Link to comment Share on other sites More sharing options...
ThatGourmand Posted April 18, 2020 Author Share Posted April 18, 2020 @CarlZalph, thanks alot for your answer, you hit the nail on the head. The answer you provided completely solved my problem and made me remember that indeed Eater:Eat(food) is basically just a shortcut for Eat(Eater, food). 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.