Using Lua to add scripting to a c++ project

Using Lua to add scripting to a c++ project
0

#1

I’ve been interesting in scripting from a C_ project for ages - and I’ve done quite a bit of R&D, including using Chaiscript (see http://pooperpig.com/?p=145 )

But I always figured it would be better to use Lua - just because you can write cocos2d-x with Lua really!

But everything I read suggested it wasn’t that easy.

So, I figured the easiest way to do it - and it works just fine.

IN my case, I wasn’t really worried about binding every class and method from cocos2d-x to allow the script to use it - so I just started from scratch and worked out how to add Lua to a standard cocos2d-x project, and how to map my own classes to allow them to be accessed by Lua…

My notes are at http://pooperpig.com/?p=150 if you’re interested.
It’s not exactly a well written post - but it should help you if this is what you want to do…

Looking at Lua vs ChaiScript I previously was thinking that I should use Lua just because it’s seems to be so ubiquitous - but comparing it ti chaiscript, I think I might use ChaiScript in my game.

I’d really be interested in anyone’s thoughts…


#2

I use the lua script binding tool that comes with cocos2d-x on one of my projects, it’s fairly easy to set up and requires very little work to add new functions and classes - just add a #include to the bindings header, a class/functions declaration to the config file, run the script and voilà, instant binding.

There are a few limitations though, such as no support for std::function bindings, which makes callbacks between c++ and lua a little awkward, and it can be a little cumbersome to add type conversions, but it definitely saves a lot the pain in having to maintain bindings code.

I read your blog on chai script and have to say it looks quite interesting, I really like the template approach to accessing functions and executing them. Will have to give it a whirl soon :smiley:


#3

I’m back playing with Chaiscript again - and liking it more than Lua in many ways.

A bit of a learning curve due to my lack of technical skills!

But it does seem to be relatively simple to set up and use once you understand some of the stuff (like there are Globals, Locals and sorta ‘not quite’ globals - which makes sense once you find out about it, but really puzzled me when I first encountered an issue (which I found because I’d done it wrong previously, but a bug allowed it to work, then I updated ChaiScript and my code stopped working!)

I’m going to bite the bullet and use Chaiscript in my current game as a real evaluation…

Fingers crossed.


#4

I use the lua script binding tool that comes with cocos2d-x on one of my projects, it’s fairly easy to set up and requires very little work to add new functions and classes - just add a #include to the bindings header, a class/functions declaration to the config file, run the script and voilà, instant binding.

Is there a place I can read about this more. I’m not sure which bindings header and which config file you’re talking about.

I messed around with adding the libluacocos2d project to my solution. I had to move the scripting/ folder from the cocos2dx repo into my solution’s cocos2d/ folder, and that didn’t work either because the linker couldn’t find the LuaEngine::getInstance so I gave up there. I might have been close but I was running blind, so who knows.