Questions About Managing Embedded Scripts in C++

Started by
3 comments, last by lauris71 11 years, 5 months ago
I've coded a few 2D games now, and for my next project, I'd like to include simple cutscenes and some more complicated AI. I know that these are two things which, arguably, are best tackled by scripting languages, and so I've started researching them. Lua seems easy to embed with LuaBind, and I've gotten a small demo up and running; however, the actual Lua code must be written into a string which is passed to a function that parses the code, which is not at all what I expected.

If the Lua code is literally in the C++ code, then I immediately lose the huge benefit of being able to make changes to the script without having to recompile. I know one solution is to load the code as a string of characters from some outside file and pass that to the function. Or, if I just have the wrong attitude about this, what might be some wise options for loading up the script from some outside source? Right now my only plan is to write it up in a text editor with highlighting (like gedit), save it as a text file and load it up manually from there using the filestream. Plan B is to use pugixml (an xml parser) in a hacky way to read the script in.

Also, feel free to let me know if I'm just confronting this problem completely wrongly. Maybe dealing with a scripting language isn't the right move at all?

Thanks
Advertisement
What's wrong with loading script from file? I guess that is how we all do

I know one solution is to load the code as a string of characters from some outside file and pass that to the function. Or, if I just have the wrong attitude about this, what might be some wise options for loading up the script from some outside source?


It's not quite that complicated. What you want is luaL_dofile. No need to manage the file streams yourself.

Between Scylla and Charybdis: First Look <-- The game I'm working on

Object-Oriented Programming Sucks <-- The kind of thing I say

How to load and execute the script doesn't relate to the binding engine, which you are using Luabind.
Also, the test/example in Luabind is using C++ string for script doesn't mean you must do it.

You can always load the script from anywhere you want, an external file, an XML, what ever, and call Lua API to execute, no matter which script binding engine you are using.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

The correct way of managing scripts depends on how you manage other assets (models, sprites, textures, images, shaders...). There is nothing very special about scripts - they are just another kind of datablocks that are used to put together a game.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

This topic is closed to new replies.

Advertisement