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?
-
\$\begingroup\$ The most popular language: See Lua (Programming language) and Lua (Programming langugae) - C API Example: IBM has a great article on this: Embedding LUA \$\endgroup\$– Tamara WijsmanJul 15, 2010 at 12:02
-
\$\begingroup\$ The exact steps depend massively on the language you want to add. Have a look at gamedev.stackexchange.com/questions/11/… \$\endgroup\$– tenpnJul 15, 2010 at 12:03
2 Answers
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).
-
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\$ Sep 28, 2010 at 16:29
-
-
\$\begingroup\$ Excellent answer. The first paragraph can be summed up as "game developers create an API which is consumed for content." \$\endgroup\$– ashes999Oct 23, 2011 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\$ Jan 22, 2019 at 20:11
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!
-
1\$\begingroup\$ I've also uploaded a video demo here: bit.ly/dnCkar \$\endgroup\$– astkSep 24, 2010 at 3:26