Jump to content
Sign in to follow this  
ZombiesLoveBrainiacs

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

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)

 

Share this post


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

Share this post


Link to post
Share on other sites
Posted (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.

image.png.63d191464fc4249b62c3e4468f6723cf.png

 

 

Edited by ATPHHe

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...