Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411490 Posts in 69371 Topics- by 58428 Members - Latest Member: shelton786

April 24, 2024, 07:52:52 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsC++ class derivation in Lua
Pages: 1 [2]
Print
Author Topic: C++ class derivation in Lua  (Read 16481 times)
muku
Level 10
*****


View Profile
« Reply #20 on: October 09, 2008, 11:42:26 AM »

That's quite interesting. Though if you have an online repository as well as your transfer repository on a USB stick, wouldn't you get certain headaches when trying to keep them in sync?
Logged
Hajo
Level 5
*****

Dream Mechanic


View Profile
« Reply #21 on: October 09, 2008, 12:45:51 PM »

Yes, you will (get headaches). If you have network access, an online repository will be the better solution. The USB stick idea was just meant as poor man's synchronization trick, with versioning software - and you get the full benefits of version management with it, free Gentleman
Logged

Per aspera ad astra
diwil
Level 2
**


View Profile WWW
« Reply #22 on: October 09, 2008, 12:54:30 PM »

That's a good point, really. And you won't need internet access, if you're like me and carry your USB stick (well, MP3 player in my case...) everywhere. I think I'll do that! :D
Logged
xgalaxy
Level 0
***



View Profile
« Reply #23 on: October 19, 2008, 06:36:14 PM »

You're welcome! Smiley

I used mostly luabind to see how it's done, but I'm not certain if I'll keep luabind in the project as is, if I manage to write my own definition of the functionality. I mean, having 8MB of extra sources in my project (and thus inflating my executable size) isn't something I enjoy, especially when it comes to compile times (I transfer my files back and forth between platforms through email every day, and the object files for the debug version tend to bloat).

I'll study Lua's own binding functions further next week, to see how I'll be able to implement the class binding on my own. I'll surely attach the results in the first post if I figure it out. Beer!

All of the C++ binding libraries I've seen of Lua are rather convoluted in my opinion. A lot of the binding tutorials are either extremely simple and don't cover binding classes, or unfinished examples.

After realizing this I started work on my own lua binding library and got fairly far into it. Below is a working example:
Code:
#include "mytest.h"

IMPLEMENT_SCRIPTOBJECT(MyTest);

MyTest::MyTest()
{
   _testValue = 0;
}

U32 MyTest::getValue()
{
   return _testValue;
}

void MyTest::setValue(U32 value)
{
   _testValue = value;
}

void MyTest::setMax(U32 val1, U32 val2)
{
   _testValue = ((val1 > val2) ? val1 : val2);
}

// ----------------------------------------------------------------------------
// Script Exposed Methods
// ----------------------------------------------------------------------------
ScriptMethod<MyTest, void()> MethodImpl(MyTest, reset);
ScriptMethod<MyTest, U32()> MethodImpl(MyTest, getValue);
ScriptMethod<MyTest, void(U32)> MethodImpl(MyTest, setValue);
ScriptMethod<MyTest, void(U32, U32)> MethodImpl(MyTest, setMax);

ScriptMethod is a template class that takes boost style arguments for defining the method signatures that lua will bind to. The macro IMPLEMENT_SCRIPTOBJECT is a utility to help link this class into a scripting table that is traversed at startup that eventually builds all the table information necessary for lua to function properly.

I haven't had time to work on it much in recent months however. I've been dabbling with a Lua derivative called Squirrel though, it has language support for classes which is really appealing to me.

« Last Edit: October 19, 2008, 06:39:35 PM by xgalaxy » Logged
diwil
Level 2
**


View Profile WWW
« Reply #24 on: October 20, 2008, 03:43:57 AM »

That all looks really nice! I wanted to do my own class binding as well, as luabind's pretty bloated (I'm using maybe 1% of it's functionality, yet, it compiles like 10 minutes - which is a pain in the ass) but I couldn't really find any tutorials or documentation on the class binding aspect, especially when binding variables.

I'll possibly scrap luabind after I figure it out, and write a lightweight wrapper, much like the Luna(r) headers that I see floating around the web.
Logged
jeb
Level 5
*****


Bling bling


View Profile WWW
« Reply #25 on: January 20, 2009, 11:24:44 AM »

Here's my addition to the Lua binding adventure!

http://www.oxeyegames.com/lua-binding-woes/

Droop
Logged

HannesP
Level 0
***


View Profile WWW
« Reply #26 on: January 27, 2009, 02:27:27 AM »

Code:
object_variable:method_name(parameter_list)

but

Code:
method_name(object_variable, parameter_list)

That seemed easier to create bindings for, and worked about as well. But Lua is a nice and efficient scripting language. I liked it much.
That's a very nice tip! I think calling class methods isn't too hard, just a matter of pushing the class to the stack, probably figuring out the method name from the metatable, and then executing it as you'd call a normal function.

Aren't those two exactly equivalent? function foo:bar() is only syntactic sugar for function foo.bar(foo), isn't it...?
Logged
increpare
Guest
« Reply #27 on: January 27, 2009, 03:14:47 AM »

Aren't those two exactly equivalent? function foo:bar() is only syntactic sugar for function foo.bar(foo), isn't it...?
you are correct, I believe
Logged
Ivan
Owl Country
Level 10
*


alright, let's see what we can see


View Profile
« Reply #28 on: January 27, 2009, 11:55:55 AM »

By the way, i have solved the problem of event callbacks in Lua.

Now I can do this in Lua:
Quote
listener = Tau.EventHandler()
listener.onEvent = function(e)
   print("key pressed")
end
core:getInput():addEventListener(listener, Tau.InputEvent_EVENT_KEYDOWN)

This is kind of SWIG specific, but really it's just using the lua API. What i'm doing is basically this:
http://cvs.opensolaris.org/source/xref//sfw/usr/src/cmd/swig/swig-1.3.35/Examples/lua/funcptr3/

but with C++ member instead of global C variable. I modified my event handler class to store a pointer to a lua function and if it's a valid lua function pointer, it calls it when the event fires
« Last Edit: January 27, 2009, 12:01:38 PM by toastie » Logged

http://polycode.org/ - Free, cross-platform, open-source engine.
Pages: 1 [2]
Print
Jump to:  

Theme orange-lt created by panic