<p>I want to move various parts of my app into simple scripts, to allow people that do not have a strong knowledge of c++ to be able to edit and implement various features.</p>

<p>Because it's a real time app, I need to have some kind of multitasking for these scripts. Ideally I want it so that the c++ app calls a script function which then continues running (under the c++ thread) until either a pause point (Wait(x)), or it returns. In the case of it waiting the state needs to be saved ready for the script to be restarted the next time the app loops after the duration has expired.</p>

<p>The scripts also need to be able to call c++ class methods, ideally using the c++ classes rather than plain wrapper functions around c++ classes.</p>

<p>I don't want to spend a massive amount of time implementing this, so using an existing scripting language is preferred to writing my own. I heard that Python and Lua can be integrated into a c++ app, but I do not know how to do this to achieve my goals.</p>

<ul>
<li>The scripts must be able to call c++ functions</li>
<li>The scripts must be able to "pause" when certain functions are called (eg. Wait), and be restarted again by the c++ thread</li>
<li>Needs to be fast -- this is for a real time app and there could potentially be a lot of scripts running.</li>
</ul>

<p>I can probably roll the multitasking code fairly easily, provided the scripts can be saved and restarted (possibly by a different thread to the original).</p>

[{'answer_id': 63859, 'author': 'Ferruccio', 'author_id': 4086, 'author_profile': 'https://Stackoverflow.com/users/4086', 'pm_score': 2, 'selected': False, 'text': '<p>Take a look at the <a href="http://www.boost.org/doc/libs/1_36_0/libs/python/doc/index.html" rel="nofollow noreferrer">Boost.Python</a> library. It looks like it should be fairly straightforward to do what you want.</p>\n'}, {'answer_id': 63865, 'author': 'TrolleFar', 'author_id': 3602, 'author_profile': 'https://Stackoverflow.com/users/3602', 'pm_score': 3, 'selected': False, 'text': '<p>I can highly recommend that you take a look at <a href="http://sourceforge.net/projects/luabind/" rel="noreferrer">Luabind</a>. It makes it very simple to integrate Lua in your C++ code and vice versa. It is also possible to expose whole C++ classes to be used in Lua.</p>\n'}, {'answer_id': 63894, 'author': 'dmazzoni', 'author_id': 7193, 'author_profile': 'https://Stackoverflow.com/users/7193', 'pm_score': 2, 'selected': False, 'text': '<p>You can definitely do what you want with Python.  <a href="http://docs.python.org/ext/embedding.html" rel="nofollow noreferrer">Here are the docs on embedding Python into an application.</a>  I\'m pretty sure Lua would work too, I\'m just less familiar with it.</p>\n\n<p>You\'re describing cooperative multi-tasking, where the script needs to call a Break or Wait function periodically.  Perhaps a better solution would be to run the scripting language in its own thread, and then use mutexes or lock-free queues for the interfaces between the scripting language and the rest of your program.  That way a buggy script that doesn\'t call Break() often enough can\'t accidentally freeze your program.</p>\n'}, {'answer_id': 63932, 'author': 'Kevin', 'author_id': 6386, 'author_profile': 'https://Stackoverflow.com/users/6386', 'pm_score': 3, 'selected': False, 'text': '<p>Your best bet is to embed either lua (www.lua.org) or python (www.python.org).  Both are used in the game industry and both access extern "C" functions relatively easily with lua having an edge here (because data types are easier to translate between lua and C).  Interfacing to C++ objects will be a bit more work on your end, but you can look up how to do this on Google, or on lua or python discussion forums.</p>\n\n<p>I hope that helps!</p>\n'}, {'answer_id': 64277, 'author': 'moswald', 'author_id': 8368, 'author_profile': 'https://Stackoverflow.com/users/8368', 'pm_score': 1, 'selected': False, 'text': '<p>You can also embed C/C++ scripts using <a href="http://softintegration.com/" rel="nofollow noreferrer">Ch</a>.  I\'ve been using it for a game project I\'m working on, and it does well.  Nice blend of power and adaptability.</p>\n'}, {'answer_id': 64682, 'author': 'Mark', 'author_id': 4405, 'author_profile': 'https://Stackoverflow.com/users/4405', 'pm_score': 6, 'selected': True, 'text': '<p>You can use either Lua or Python.  Lua is more "lightweight" than python.  It\'s got a smaller memory footprint than python does and in our experience was easier to integrate (people\'s mileage on this point might vary).  It can support a bunch of scripts running simultaneously.  Lua, at least, supports stopping/starting threads in the manner you desire.</p>\n\n<p>Boost.python is nice, but in my (limited) experience, it was difficult for us to get compiling for our different environments and was pretty heavyweight.  It has (in my opinion) the disadvantage of requiring Boost.  For some, that might not be a problem, but if you don\'t need Boost (or are not using it), you are introducing a ton of code to get Boost.python working.  YMMV.</p>\n\n<p>We have built Lua into apps on multiple platforms (win32, Xbox360 and PS3).  I believe that it will work on x64.  The suggestion to use Luabind is good.  We wound up writing our own interface between the two and while not too complicated, having that glue code will save you a lot of time and perhaps aggravation.</p>\n\n<p>With either solution though, debugging can be a pain.  We currently have no good solution for debugging Lua scripts that are embedded into our app.  Since we haven\'t used python in our apps I can\'t speak to what tools might be available there, but a couple of years ago the landscape was roughly the same -- poor debugging.  Having scripting to extend functionality is nice, but bugs in the scripts can cause problems and might be difficult to locate.</p>\n\n<p>The Lua code itself is kind of messy to work with if you need to make changes there.  We have seen bugs in the Lua codebase itself that were hard to track down.  I suspect that Boost::Python might have similar problems.</p>\n\n<p>And with any scripting language, it\'s not necessarily a solution for "non-programmers" to extend functionality.  It might seem like it, but you will likely wind up spending a fair amount of time either debugging scripts or even perhaps Lua.</p>\n\n<p>That all said, we\'ve been very happy with Lua and have shipped it in two games.  We currently have no plans to move away from the language.  All in all, we\'ve found it better than other alternatives that were available a couple of years ago.  Python (and IronPython) are other choices, but based on experience, they seem more heavy handed than Lua.  I\'d love to hear about other experiences there though.</p>\n'}, {'answer_id': 64743, 'author': 'Harold Ekstrom', 'author_id': 8429, 'author_profile': 'https://Stackoverflow.com/users/8429', 'pm_score': 2, 'selected': False, 'text': '<p>Take a look at <a href="http://www.swig.org" rel="nofollow noreferrer">SWIG</a>. I\'ve used it to interface with Python, but it supports many other languages.</p>\n'}, {'answer_id': 64863, 'author': 'yrp', 'author_id': 7228, 'author_profile': 'https://Stackoverflow.com/users/7228', 'pm_score': 2, 'selected': False, 'text': "<p>One more vote for Lua. It's small, it's fast, it doesnt consume much memory (for games your best bet is to allocate big buffer at the initialization and re-direct all Lua memory allocations there). We used tolua to generate bindings, but there are other options, most of them much smaller/easier to use (IMO) than boost.python.</p>\n"}, {'answer_id': 383536, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>As for debugging Lua (if you go that route), I have been using DeCoda, and it has not been bad. It pretends to be an IDE, but sorta fails at that, but you can attach The debugging process to visual studio, and go down the call stack at break points. Very handy for Tracking down that bug.</p>\n'}]
['https://Stackoverflow.com/questions/63784', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6266/']