Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Let's say I have a game written in C++. But I want to add some modding or scripting functionality to it. How would one go about adding a scripting functionality to your game?

share|improve this question
    
The exact steps depend massively on the language you want to add. Have a look at gamedev.stackexchange.com/questions/11/… –  tenpn Jul 15 '10 at 12:03
add comment

3 Answers

up vote 17 down vote accepted

First of all, you should decide what part of your game is scripted. One option is to have a fully scripted game in the sense that while the time-critical backend operations are coded in C++, all the game logic is in the scripting language. Designers use the backend as a library called from the high level scripting language. On the other extreme, you can have few specific places where scripts are used, such as the user interface or scripted sequences, with the majority of the game code is still in C++. There are advantages to each approach (speed, flexibility, compilation time, scope of game, etc.), but you need to decide that beforehand.

Once you know how you want to use scripting, you now need to decide if you're going to use an existing scripting language or your own. Today there are many scripting languages to choose from with different design goals and target audiences, so I'm not sure if it's worth creating your own anymore. You can read about implementing your own scripting engine here.

Some popular scripting languages include Lua, which is lightweight and easy to embed. It uses a stack for communication between the host and embedded language and it was successfully used in many professional games. Another option is AngelCode, which allows you to call C and C++ functions directly. Python and Ruby are a bit more complex to embed but once you have done that they are very pleasant to program in. If you want to embed Python then take a look at Boost.Python. You can also try embedding JavaScript and take advantage of the fast scripting engines developed for browsers. For my game we are using Google's V8 engine which is very fast and easy to embed, but Mozilla's SpiderMonkey is another option.

share|improve this answer
2  
I've implemented a scripting language for my current game project, and the primary advantage is total control over language primitives. Prebuilt scripting solutions are generic, and you may not need all of the features that they provide. You absolutely should not think about rolling your own language unless you're certain that you can make something that's better for your game than an existing solution. –  Jon Purdy Sep 28 '10 at 16:29
    
Personally I like Guile, it has great integration with C. Only problem is the speed, but with the 2.0 release in the next couple of months that should improve too. –  Joe D Nov 14 '10 at 1:38
    
Excellent answer. The first paragraph can be summed up as "game developers create an API which is consumed for content." –  ashes999 Oct 23 '11 at 17:52
add comment

The most popular language:

See Lua (Programming language)
and Lua (Programming langugae) - C API

Example:

IBM has a great article on this: Embedding LUA

share|improve this answer
add comment

I wrote a post about embedding GameMonkey script to a game using DragonFire SDK here: http://bit.ly/96vndP Basically the idea is to expose your C\C++ functions to your chosen scripting language and use them from your script. In my tutorial, I exposed 2 functions from DragonFire SDK. At game start, I call onStart function from script and on update, I call onTimer function from script.

Hope that helps!

share|improve this answer
1  
I've also uploaded a video demo here: bit.ly/dnCkar –  astk Sep 24 '10 at 3:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.