Lua scripting and quitting cigs

posted in owl's Blog
Published March 17, 2011
Advertisement
I always wanted to be able to add scripting capabilities to my programs but it was the kind of subject that always lead me to procrastination.

I've been without smoking for 3 weeks and 3 days and I'm doing quite fine. I'm already starting to feel a lot better (and smell better). I went totally cold turkey on this so the first days were a real nightmare. I still have some smoking flashbacks and dreams but compared to the first week I'm almost over it.

One of the challenges of not smoking anymore was to still being able to sit on the computer and think; so adding scripting to my engine became the excuse for such training.

As I started completely clueless I had to do some research, and after trying a few languages and a few libraries for each I decided that the best option for me would be Lua and that I'd connect it to my C++ programming using SWIG.

Why did I chose SWIG over other options (LuaBind, OOLua)? Well, mostly because it was the first that allowed me to do what I needed in almost no time and requiring me to code nothing but a few lines in a special interface file it needs to compile to generate the c++ source code that will bind my engine to the scripting. It also comes with interfaces for the standard library and a pletora of other stuff I'll probably never use.

The most important things I wanted from scripting were obviously the capability of exposing my classes to Lua and being able to callback Lua functions (in the script files) from my engine. The latter I'm still working on understanding how it works.

So far I've come to the following solution:

As I mentioned in my previous entries, one aspect of my engine is that it has it's own UI. The UI element events get fired through delegates that require function pointers being passed to them on initialization. This is exactly one of the things one cannot do with scripting. So I was forced to add a layer between the two. This layer is functors (that is classes which their instances are meant to work as functions).

So, when a UI button needs it's click event set I do the folowing from Lua:

btn = owl.create_button()
btn.text:set("rename")
btn:move(200,200);
btn:resize(100, 40);
app.gui.root:add(owl.button_to_object(btn));

--// create a functor for this event
button_event = owl.button_click_handler("btn_rename_click")
button_event:register_event(btn)

--// This function will be called back from the functor
function btn_rename_click()
print ("this wont work")
end


In C++ the functor is pretty lame. It looks like this:
class button_click_handler
{
public:

button_click_handler(const std::string& func)
{
m_func = func;
}

void register_event(boost::shared_ptr btn)
{
btn->click += fd::delegate(&button_click_handler::on_event,this);
}

void on_event()
{
std::cout << "on_event" < lua_getfield(L, LUA_GLOBALSINDEX, m_func.c_str());
int s;
if( (s=lua_pcall(L,0,0,0)) != 0)
report_errors(L, s);
}

protected:
std::string m_func;
};



What I like about SWIG and the functor solution is that I can code everything that is related to scripting in separated source files in such a way that the engine is never aware that it is actually being scripted.

As always, a screenie for the voyeurs:

gallery_16415_66_21405.jpg
1 likes 3 comments

Comments

Servant of the Lord
Excellent job on the three weeks! And excellent job in choosing Lua. [img]http://public.gamedev.net/public/style_emoticons/default/wink.gif[/img]

For my own project, I just did a simplistic wrapper (the 'Luna' wrapper example with my own features added), and it suited my needs - if a future project requires more, I'll try to keep SWIG in mind. Lua is awesome.
March 18, 2011 03:12 AM
owl
Thanks and agreed, Lua is a pretty charming language! :)
March 18, 2011 04:46 AM
Aardvajk
Wow. Not sure what impresses me more - quitting cigs or integrating a scripting language with C++. Both are challenging, require enormous will-power and constantly frustrate by failure.

Keep them both up. I'm still smoking and using my own scripting language. Have strength, bro.
March 19, 2011 10:10 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement