[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Augmenting objects on per instance basis
- From: "Max van Rooij \(van Rooij Electronics Design & Software Engineering\)" <mvrooij@...>
- Date: Mon, 7 Jul 2008 20:35:28 +0200
Title: Bericht
Dear
all,
I'm new to Lua and
very enthusiastic about it, especially in it's ability to interface with C.
I've written a GUI in C and have given the users the ability to customize
behaviours (such as mouse clicks) in Lua.
I've defined a so
called "click-able" region as a struct in C. The user can create a region that's
click-able in a lua script with a single line like this:
mywidget1 =
widget.create(100,100,10,10) --create click-able region at (x,y) = 100,100 width
10 and height 10
By implementing the
__index in C code, the user can write:
function
mywidget1:leftclick
print("hurray! a
left click!")
end
Now, the C code is
capable to relate the clicked region to the right Lua object and a get a nice
"hurray!" printed. Unfortunatly, if my user does:
mywidget1 =
widget.create(100,100,10,10)
mywidget2 =
widget.create(110,110,20,20)
function
mywidget1:leftclick
print("left click
widget 1")
end
function
mywidget2:leftclick
print("left click
widget 2")
end
I always get "left click widget 2". This is,
because the widgets share the same method table, so the last definition simply
overwrites all previous ones. How do I change my C code, so my users can have
the above code working?
Thanks
Max