Search code examples
luawxwidgetsselfwxlua

wxlua self changing value?


I'm trying to draw some stuff using wxlua.

ExampleClass = {}

function ExampleClass:New(someWxPanel)
  local obj = {}
  setmetatable(obj, self)
  self.__index = self
  self.m_panel = someWxPanel
  return obj
end

function ExampleClass:OnPaint()
  local dc = wx.wxPaintDC(self.m_panel)
   --paint some stuff
end


local example = ExampleClass(somePanel)
somePanel:Connect(wx.wxEVT_PAINT, example.OnPaint)

I get the following error message: wxLua: Unable to call an unknown method 'm_panels' on a 'wxPaintEvent' type.

While in any other function I define as Example:SomeFunction() self points to my Example instance and I can perfectly access its members here self is a wxPaintEvent?

How did this happen? Does the Connect somehow change self? How can I access my members now?

Appreciate any help!


Solution

  • When you register example.OnPaint as the event handler in somePanel:Connect(wx.wxEVT_PAINT, example.OnPaint), it always gets the event as the first parameter, but your method expects the object (self) passed as the first parameter, hence the error you get. You need to replace the registration with something like this:

    somePanel:Connect(wx.wxEVT_PAINT, function(event) example:OnPaint(event) end)