25
\$\begingroup\$

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?

\$\endgroup\$
27
\$\begingroup\$

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 an API 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 domain-specific language. 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. If you decide to implement your own, here are some resources:

Lua is a popular lightweight and easy to embed scripting language. It uses a stack for communication between the host and embedded language and it was successfully used in many professional games. Sol2 simplifies the process of binding Lua and C++. If you're not fond of Lua's syntax, Moonscript is a language that compiles to Lua and adds a nice set of features.

Other options include AngelCode, which allows you to call C and C++ functions directly. Python and Ruby are a bit more complex to embed, but are very pleasant to program in. If you want to embed Python then take a look at Boost.Python. Another idea is embedding JavaScript to take advantage of the fast scripting engines developed for browsers (see V8 and SpiderMonkey).

\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Jon Purdy Sep 28 '10 at 16:29
  • \$\begingroup\$ 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. \$\endgroup\$ – Joe D Nov 14 '10 at 1:38
  • \$\begingroup\$ Excellent answer. The first paragraph can be summed up as "game developers create an API which is consumed for content." \$\endgroup\$ – ashes999 Oct 23 '11 at 17:52
  • \$\begingroup\$ The generic term for custom languages (scripting or otherwise) is DSL, which may help to know if you want to read up. \$\endgroup\$ – Patrick Hughes Jan 22 at 20:11
2
\$\begingroup\$

I wrote a post about embedding GameMonkey script to a game using DragonFire SDK here.

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!

\$\endgroup\$
  • 1
    \$\begingroup\$ I've also uploaded a video demo here: bit.ly/dnCkar \$\endgroup\$ – astk Sep 24 '10 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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