wxlua self changing value? - lua

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!

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)

Related

lua - differece of : and . when define function?

hope you having great day , i'm test some stuff with lua, and i got some error
i have this codes in Object.lua
local Object = {name = "Object"}
Object.__index = Object
function Object.new()
local t = setmetatable({}, Object)
return t
end
function Object:keys(table)
local keyset={}
local n=0
for k,v in pairs(table) do
n=n+1
keyset[n]=k
end
return keyset
end
return Object
and i call Object.keys in "Other" script like this
local Object = require("lua.app.common.utils.Object")
local t = {
[1] = a,
[2] = b,
[3]= c
}
Object.keys(t)
and this cause error with
Exception has occurred: bad argument #1 to 'for iterator' (table expected, got nil),
because the parameter 'table' is passed as nil ( i don't know why thuogh debug mode just says its a nil)
on the other hand, if i fix object.keys(table) to object:keys(table), everything works fine, why this error happening?
Declaring the method keys with a colon adds an implicit self parameter as the first argument which requires you to call the method in the same way, using the colon.
Calling the method with the period notation expects you to supply the self parameter but because you only pass in t this is interpreted as self and then nil is added for your table parameter.
Declaring the method with a colon you actually have this
function Object:keys(self, table)
but calling it with the period means you have done this
Object.keys(t, nil)
hence the "table expected, got nil" error

why my value is nill? Dont understand what happen

I have this.
components=require("components")
entity={}
--test---
function entity.test(x,y)
self ={}
self.id="test"
--self.position=components.position(x,y)
return self
end
return entity
In main i have this.
entities=require("entity")
function love.load()
test1=entities.test(100,200)
print(test1.id)
end
the output is ok, but, when I add another component .
components=require("components")
entity={}
--test---
function entity.test(x,y)
self ={}
self.id="test"
self.position=components.position(x,y)
return self
end
return entity
and make another print
entities=require("entity")
function love.load()
test1=entities.test(100,200)
print(test1.id)
print(test1.position.y)
end
I got first a nill valur, and then an error because field position is nill. What happen? Thanks in advance.
my component.lua
component = {}
--position--
function component.position(x,y)
self={}
self.x=x
self.y=y
return self
end
return component
You can fix the problem by declaring your variables local, e.g. local self = {}.
When you call entity.test, that function assigns a new table to the global self. It then calls components.position which assigns a whole new table to self, which never gets the position field.

Using objects in Lua

So I am creating a system for Lua, so I can have classes and objects in it. I have the creation of objects down, the problem is creating constructors.
I have constructors like this:
a = MyClass:Create("Hello World!")
The create method has ... as its arguments which it passes on to the constructor method (OnStart). I can read the arguments just fine in the Create method but when OnStart is called the argument somehow ends up being nil instead of "Hello World!"
My code:
Object = { }
function Object:Create(...)
local instance = { }
setmetatable(instance, self)
self.__index = self
instance.Type = Object
-- Now we can call the constructor.
local arg = { ... }
instance.OnStart(table.unpack(arg))
return instance
end
function Object:OnStart(msg)
print(msg)
end
test = Object:Create("Hello World!")
print(test:ToString())
Some how in here the msg argument ends up being nil...
It seems I have found out why it was not working, the slight detail that needs to be changed is one line 9. Instead of instance.OnStart it needs to be instance:OnStart.

accessing lua self member got nil

I was trying to access a member of 'self', see following:
function BasePlayer:blah()
--do blah
end
function BasePlayer:ctor(tape) --constructor.
self.tape = tape --'tape' is a lua table, and assigned to 'self.tape'.
dump(self.tape) --i can dump table 'self.tape',obviously it's not nil.
self.coreLogic = function(dt) --callback function called every frame.
echoInfo("%s",self) --prints self 'userdata: 0x11640d20'.
echoInfo("%s",self.tape) -- Here is my question: why self.tape is nil??
self:blah() --even function too??
end
end
So my question is, in my callback function 'coreLogic', why 'self.tape' is nil but self is
vaild?
And functions can't be called either.
I'm really confused:S
When you define a function using the :, the implicit parameter is created on its own. When you are defining the function coreLogic, you'd need to pass it as the first argument:
self.coreLogic = function( self, dt )
and that would be the same as:
function self:coreLogic(dt)
self in itself doesn't exist.
Basically,
function BasePlayer:blah()
is same as writing:
function BasePlayer.blah( BasePlayer )

ES5 shim for binding functions is Javascript

Below is a ES5 shim for JS binding.I dont understand self.apply in the bound function.
I know how to use apply method, but where is self pointing to in this case ? It it supposed to be a
function, but here self looks like an object.
if ( !Function.prototype.bind ) {
Function.prototype.bind = function( obj ) {
var slice = [].slice,
args = slice.call(arguments, 1),
self = this,
nop = function () {},
bound = function () {
return self.apply( this instanceof nop ? this : ( obj || {} ), // self in this line is supposed
to // represent a function ?
args.concat( slice.call(arguments) ) );
};
nop.prototype = self.prototype;
bound.prototype = new nop();
return bound;
};
}
self is being used in the shim you have listed to accommodate the fact that this changes along with scope changes. Within the direct scope of the Function.prototype.bind function this will refer to the object on which the bind function was called.
Once you enter the scope of the nested bound function this has changed; so the author has assigned self = this within the bind function to allow the value of this at the time bind is called to remain available to the bound function via lexical scoping (closure).
Scoping within JavaScript can get pretty complicated; for a detailed explanation take a look at this article.
Everything you wanted to know about JavaScript scope.
Have in mind that in javascript almost everything is an object.
So you have it right there:
self = this
So, self is not representing anything, self is the instance.

Resources