Scripting

From Mount&Blade Modding Wiki
Revision as of 09:23, 28 June 2012 by Swyter (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The Mount & Blade scripting language is based on an "opcode parser" built into the game engine which interprets files containing a series of plain-text numbers, and occasionally sequences of letters, and performs tasks based on what it reads from these files. While it shares most of the features of any other programming language, it is not a programming language: someone cannot do anything arbitrarily in the scripting language that has not been explicitly enabled in the game engine, meaning that scripters cannot delete files, draw graphics directly on the screen, compromise security, or otherwise manipulate an end-user's computer through scripting alone. This also somewhat limits the power of the language for more complex tasks, as some of the particular quirks of the Mount and Blade scripting language make routine programming tasks more difficult. Nevertheless, it is a very full-featured system and many Mount and Blade modders have barely scratched the surface of its potential.

Scripts in Mount & Blade are tightly integrated with the module system, and many features of the module system involve scripts in some way. Scripting is used in the conditions of dialogue, in simple triggers, and (of course) in full scripts.

Contents

[edit] Programming concepts

Scripting is divided into several core concepts.

[edit] Statements

A "statement" in programming terms is a single, discrete line of code that accomplishes a single, clearly defined task. In Mount & Blade, all statements must begin with an operation; in other languages, a statement also contains an "operator", but the operator is not usually the first thing on the line (often there may be a variable on the left, an operator in the middle, and another variable on the right). In Mount & Blade, the operation is always the very first thing on the line, due to the unique way the game engine works.

All statements in Mount and Blade scripting are made from Python tuples. In the scripting community, "tuple" is usually equivalent to "statement" in the context of the scripting language.

[edit] Blocks

A "block" in programming terms is a series of statements that accomplishes a broader goal, by repeatedly manipulating numbers and game objects. All code in a block is typically surrounded by brackets in the Mount and Blade scripting system and contains any arbitrary number of tuples. Blocks can only appear when defined by new objects, such as new dialogue entries, new triggers, and new scripts.

In the module system, a block is a Python array of tuples. As Mount & Blade does not support array operations, using the term "array" is generally avoided in preference to "block".

Unlike most other programming languages, "nested" blocks (such as a try_begin block) are represented without any specific special symbols like braces or brackets, and indentation is unimportant as long as Python will accept it.

[edit] Functions

A "function" in programming terms is a block that has been specially named. A function can be "called" by its name in order to run every statement it contains at once. It can be reused as needed, and this is one of its greatest strengths: if it contains a bug that needs to be fixed, all pieces of code that use the same function will automatically use the fixed function instead of needing to be updated. It reduces the likelihood of forgetting to update a piece of code. It also provides an advantage in that once the function is designed properly, the code that uses it doesn't care about how it works, a concept called "encapsulation": the code tells the function what it wants and the function gives the results without the person writing the other piece of code having to worry about all of the gory details. It can break large, daunting "spaghetti code" into an easily manageable set of simple features and the code that uses them.

In the module system, a function is a script -- a special piece of data found in module_scripts.py that can be run using the call_script operation. Mount & Blade does not normally use actual Python functions. Scripters generally use "function" and "script" interchangeably, but almost always mean anything that has been defined in module_scripts.py when they say "function".

[edit] Basic scripting

Scripting in Mount & Blade is not easy; not only is the syntax "a language within a language" but there is no single accepted "For Dummies" reference. Many ideas for Mount & Blade scripting will have never been done before, many operations will have never been used by anyone else, and there is still the potential to run into a show-stopping bug in the Mount & Blade game engine, even though Mount & Blade and its sequels have been present on the market for several years.

Before beginning scripting, it is strongly suggested that you have played Mount & Blade for quite some time and have at least an appreciable grasp on how it all works together. When you enter a city, you can see various buildings around town: the town is a scene and the buildings are scene props. When you enter a tavern, you see people standing around: it is much easier to understand that it is a tavern scene containing a standard tavern mission filled with NPC agents than it is to simply try to comprehend the concepts of scenes, missions, and agents without any frame of reference. You know that the places that the agents appear are fixed -- you remember to check upstairs in certain taverns because there are spawn points up there; each one of those is a position. And so on; once you comprehend how the game works "on the outside", reading the descriptions of what the things are "on the inside" will be much easier.

[edit] Hello, world

"Hello, world" is the traditional introduction to scripting by any programmer. The easiest way to implement hello world in Mount & Blade is with a simple trigger.

TODO

[edit] Other resources

Community resources on the TaleWorlds forums are available to make the effort of scripting a little less unbearable:

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox