ZombiesLoveBrainiacs Posted March 24, 2020 Share Posted March 24, 2020 I can't get this test "class" to give me access to the player object from the OnPlayerUpdate Event. Can anyone tell me what I'm doing wrong here? local MyClass = {} function MyClass:new(o) setmetatable(o, self) self.__index = self --Events.OnPlayerUpdate.Add(self:OnPlayerUpdate) -- Error: function arguments expected near `)` Events.OnPlayerUpdate.Add(self.OnPlayerUpdate) return o end function MyClass:OnPlayerUpdate(p) print(p) --prints nil... end local o = {} local instance = MyClass:new(o) Link to comment Share on other sites More sharing options...
ZombiesLoveBrainiacs Posted March 24, 2020 Author Share Posted March 24, 2020 (edited) Ok, so: Quote self:method() is a shortcut for self.method(self) In this case, Zomboid will call self.method(p) and the result is: function MyClass:OnPlayerUpdate(p) print(p) --prints nil... print(self) --prints zombie.characters.IsoPlayer@18e25e21 end I still don't know what to do about it, tho... Edited March 25, 2020 by ZombiesLoveBrainiacs Link to comment Share on other sites More sharing options...
ATPHHe Posted March 24, 2020 Share Posted March 24, 2020 (edited) One solution is to change "MyClass:OnPlayerUpdate(p)" by removing the colon and replacing it with a period, "MyClass.OnPlayerUpdate(p)". I am not sure why this works, but it has to do with how . and : on "MyClass" both act with Zomboid's Events. local MyClass = {} function MyClass:new(o) setmetatable(o, self) self.__index = self --Events.OnPlayerUpdate.Add(self:OnPlayerUpdate) -- Error: function arguments expected near `)` Events.OnPlayerUpdate.Add(self.OnPlayerUpdate) return o end function MyClass.OnPlayerUpdate(p) print(p) end local o = {} local instance = MyClass:new(o) With this change, "print(p)" should produce this now. Edited March 24, 2020 by ATPHHe Maris and ZombiesLoveBrainiacs 1 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now