Intel sponsors gamedev.net search:
The site is presently in a read-only mode while we perform some internal maintenance. Thank you for your patience.
Control Panel Register Bookmarks Who's Online Active Topics Stats FAQ Search
GameDev.Net Discussion Forums Frequently Asked Questions (for individual forums) Back to the Forums
Current Server Time - 1:06:46 AM
FAQ - Scripting Languages and Game Mods
Body

The Scripting Languages and Game Mods Forum


This forum is for threads on the related topics of scripting languages and game modifications. Examples of acceptable topics are:

- Making "mods" (modifications) for existing games.
- Making your games "moddable" or scriptable.
- Embedding scripting languages in other programs.
- Extending scripting languages using other languages (such as C or C++).
- Designing or implementing your own scripting language.
- Implementing parsers or compilers for existing languages.
- General help with the languages and mechanisms involved in modding and scripting.
- Working with level/campaign editors for existing games
- Questions and answers regarding existing mods

If you can"t find your question answered here, your next stop should be our Resources section, namely the Scripting part of the Programming category, and the Mods category. New articles on these subjects would be gratefully received by GameDev.net.

Mods

What is a Mod?

A mod is a modification to an existing game. Some games are designed to be modified, maybe incorporating their own scripting language, and others have to effectively be hacked. Some come with their own tools to create new levels and campaigns. Whatever the case, a game is considered modded if any of its default properties are changed, whether by switching weapon models, adding new levels, extending the story with new campaign or adding new gameplay elements.

Why are Mods such a Big Deal?

Ever since the Counter Strike mod team got hired by Valve to produce a commercial product, modding has become a big deal. Not only is it considered a back door into the industry, modding an already-complete product lets you experience the hassles of game development without having to worry too much about the engine, interface and so forth. Also, with more and more games being based on existing technology of some sort, the line between a mod and a new game is getting more and more blurred. Mod-making is arguably just one small step up from using a game development library like DirectX or SDL.

How do I make my game 'moddable'?

This is not a simple question to answer as there are many different approaches. Which one you choose will depend on your level of expertise, how much of the source code you're willing to share with modders, what language you are programming the game in, which aspects you wish to make modifiable, and so on. A very popular method is to use a scripting language for the game logic (see below) so that modders can replace your scripts with their own. Another common method is to place game logic into a dynamically linked library (.DLL or equivalent) that can be replaced with one developed by the user. On a very basic level, simply using open formats for your data (eg. XML or plain text) allows players to easily customise the game.


Scripting languages

Why would I want to use a scripting language in my game?

The reasons vary, but they include making the game easier to modify (ie. create mods for), allowing your designers to change the game logic without relying on the programmers, allowing rapid development by using a higher-level language that often requires no compilation or linking steps, making it easy to alter the code while the program is running (useful for online games or quick testing of new ideas in any game), providing useful high-level programming facilities that make complex algorithms a lot simpler, and so on.

Some previous discussion of this can be found here, here, here, and here.

What is a scripting language, and how does it differ from "normal" languages such as C++, Visual Basic, etc?

The main thing to remember is that there is no firm dividing line. The real difference is in how you use the language, not in the syntax. (For example, LPMUDs use a scripting language called LPC which is very similar to C, yet some applications are written entirely in languages such as Python without needing to embed it in an app written with a "normal" language.) Generally speaking though, a scripting language is a high-level language that is used to manipulate components written in other languages. They tend to emphasise speed of development and feature-completeness over performance and compile-time checking. They are typically either embedded in a host application, or rely on a large library of external routines and objects.

Some examples of what are usually termed scripting languages are: Lua, Python, Ruby, javascript, Tcl, Perl, VBScript, Squirrel, Pawn (formerly 'Small'), AngelScript, GameMonkey, EEL, Pike, TCC, NASAL, and presumably more. Additionally, some scripting languages come as part of a game, such as UnrealScript, the various languages used in the Baldur's Gate and Neverwinter Nights series, etc. Finally, some 'regular' languages such as Lisp, Java, and even C or C++ can be embedded in your applications, even though they are more commonly used as standalone languages.

How do I write my own scripting language?

There is no simple answer here, as to write a scripting language often requires some knowledge of compiler construction, programming language grammars, token parsing, expression trees, etc etc. One commonly-cited link is the Flipcode article on the subject, although GameDev.net has its own series here. Also, Jack Crenshaw"s compiler-writing series also offers a lot of good insights while taking a refreshingly down-to-earth view on things. Many languages have been created using the venerable open source tools Flex and Bison, but please be aware that there are less complex alternatives to these programs if you search for them.

How do I embed an existing scripting language into my game?


Obviously this question varies depending on the language you use, but the process usually goes along the lines of:
1) Compile the scripting language code or download the appropriate library
2) Add the headers and/or the component/library to your project or makefile
3) Edit your program to call scripts at certain points and to retrieve the values returned by the scripts.
4) Write any necessary functions in your application that scripts will need to be able to call to access and manipulate data.
5) Create the wrapper code that links those existing functions to functions in the scripting language.

Step 1 is often simple as scripting language libraries often come in .DLL form. Failing that, they are usually easy to compile.
Step 5 can also be largely automated by use of "wrapper-generating" software, the most famous of which is SWIG, although others exist such as ToLua, Boost-Python, SIP, CABLE/CABLESwig, Pyrex, Elmer, PyCXX, Py++, SQPlus, and many more. Finding a good tool for this will save you a lot of time later, so do look around for what best suits your needs.

How do I run several long running scripts at the same time?

How do I pause a script so that I can resume it later?

These questions typically address the same problem - you have a script that dictates what an NPC is going to do over a period of time, which take place concurrently with other activities, both scripted (ie. the scripts of other NPCs) and not (ie. your rendering, networking, input, etc). You might even have an infinite loop in your script (eg. a patrol route for a guard) which, if not paused, would mean that the engine itself never gets to execute.

If at all possible, consider designing your scripting in such a way that each script executes instantly and terminates. This event-based model will take a little getting used to, but will tend to be less complex and error-prone. Browser scripting works in this way. The alternative is to use something like coroutines. Languages like Lua, Squirrel, and Python offer coroutines, and they all work in a similar way - during execution the coroutine explicitly stops and returns control to the engine (called 'yielding'), perhaps returning a value when it does so. Later, your engine will resume the coroutine and it will pick up from where it left off, which is probably in the middle of the script. Typically you write this so that any long-running script operations will signal this intent to the engine, yield control back to the engine, and will be resumed by the engine at an appropriate point in the future.

Example: a function that tells the character to walk to a certain place. The scripting language has a function 'WalkTo'. When executed, this calls a pathfinding function in the engine, which stores the path with the character. The scripting language then yields control back to the engine, which moves the character along the stored path each frame. When the character reaches the end of the path, the engine knows to look for the next script command, so it resumes the coroutine and the script continues.