Jump to content

How to call a LUA method as a response to a Zomboid Event?


ZombiesLoveBrainiacs

Recommended Posts

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

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 by ZombiesLoveBrainiacs
Link to comment
Share on other sites

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.

image.png.63d191464fc4249b62c3e4468f6723cf.png

 

 

Edited by ATPHHe
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
×
×
  • Create New...