Author Topic: An Introduction to Module System Syntax and Usage  (Read 14511 times)

0 Members and 1 Guest are viewing this topic.

Caba`drin

  • Administrator
  • *
  • It's time to toss the dice.
    • View Profile
  • Faction: Nord
  • MP nick: Caba_drin
  • M&BWBWF&SNW
An Introduction to Module System Syntax and Usage
« on: October 22, 2010, 05:42:55 AM »
There are some very useful threads and guides that provide an introduction to the Module System and how to begin using it to mod every bit of Mount&Blade (Warband), but I have yet to come upon one that addresses the very basics of the operations and syntax used by the code structure of the Module System. Rather, short single question threads abound and exchanges in the Q&A Thread serve to educate aspiring modders/coders in the workings of the system. While I am by no means the most qualified member of this community to intelligently, comprehensively and effectively explain the syntax used in the Module System, I thought it couldn't do much harm to make an attempt.



ModuleSystem Downloads
Updated header_operations file for Warband by Lav (get it!)



The Basics
A listing of all of the valid commands and operations that can be used in the module system is found in the file header_operations.py. It includes comments (following the # sign) that may give a hint as to each operations use, but also identify what arguments need to be included with each operation. Every operation used in the module system takes the following form:
1. It is enclosed in parentheses: (operation)
2. It is immediately followed by a comma: (operation),
3. Arguments within the operation parentheses are separated by a comma: (operation, argument, argument),

Additionally, the header_operations file assigns a numerical code to each of the operations listed there. For instance, call_script = 1. When the game gives errors, they are in the form of SCRIPT ERROR ON OPCODE ##:....OPCODE number provided refers to the number assigned to the operation in header_operations. So, if the error was with OPCODE1, the line of code in question used the operation call_script.

Items contained within the various module files are indexed, that is assigned an ordinal numeric value based on the order they are found in the file. These indexes begin with 0 and count by 1 to each next value. Thus, troops, items, triggers, scenes, etc, once compiled into .txt files, are referred to by their index value. So, if you see an error code in "trigger 0", that is the first trigger in the indicated file, and if the error is in line 4, you are looking for the fifth operation within that trigger.

(click to show/hide)
(click to show/hide)
(click to show/hide)



Terminology
(click to show/hide)
(click to show/hide)
(click to show/hide)



Control and Conditional Operations and Syntax
In general, it is imperitive to note that the game engine treats every conditional operation as an "IF" and all of the code that follows after a conditional operation as the "THEN" in an If-Then statement. As the engine goes through lines of code, it will stop any time a conditional operation fails, and it will not read the remainder of the code.

To isolate If-Then(-Else) statements to allow failure but continue processing the remainder of the code, one must use a "try block" with (try_begin), (else_try), and (try_end), This is discussed in more detail below.
(click to show/hide)
(click to show/hide)
(click to show/hide)
(click to show/hide)
(click to show/hide)
(click to show/hide)



Other Topics
(click to show/hide)
(click to show/hide)



Other Reference Threads
Scattered across the sub-forum are numerous threads that address these topics. While I didn't take the time to isolate each of these discussions and create yet another thread of links, the following are the crucial pre-existing collections of information:



This is a work in progress. Addenda, suggestions, corrections, and similar are welcome.
« Last Edit: March 11, 2012, 03:03:29 AM by Caba`drin »



Somebody

  • Grandmaster Knight
  • *
    • View Profile
  • Faction: Bandit
  • WBWF&S
Re: An Introduction to Module System Syntax and Usage
« Reply #1 on: October 22, 2010, 06:31:51 AM »
Very well written - it will probably be helpful to many people.
Also, you may also want to include a short section on party templates - their relation to parties is analogous to those of troops to agents. Expanding on strings (instead of stating look in header_operations) would also be nice - you could also lead into dialogs if you are so inclined.

Vornne

  • Grandmaster Knight
  • *
    • View Profile
  • Faction: Neutral
  • MP nick: Vornne
  • M&BWBNW
Re: An Introduction to Module System Syntax and Usage
« Reply #2 on: October 22, 2010, 12:14:14 PM »
Very nice. I looked for this sort of thread when learning how it worked and didn't find much, so mostly learnt by reading the native code... I've wondered about posting a thread like this, but have been plenty busy enough as it is; so thanks :)


A few more things that occur to me you might want to mention (maybe you just haven't got to them, and it's a bit jumbled sorry):
While you are correct that global variables ("$var") seem to be initialized to 0, in my experience local variables (":var") are not; you must assign them values in some way before reading from them, or they could contain some random number.
When code refers to entries from another file using a prefixed string (like "itm_saddle_horse" or "trp_looter") the module system converts it to a number at build time, which can be found in the corresponding ID_*.py file (like ID_items.py). So when the game gives an error message, a few things will be represented by strings, like the name of the script, but most things will be represented by their id number. You can add debugging messages to show the id of a troop or whatever, like:
Code: [Select]
(assign, reg10, ":troop_id_being_checked"),
(display_message, "@troop being checked is {reg10}"),
Then if the output was "troop being checked is 30" you can look in ID_troops.py and find the line "trp_hired_blade = 30". In this particular example you could also use:
Code: [Select]
(str_store_troop_name, s10, ":troop_id_being_checked"),
(display_message, "@troop being checked is {s10}"),
if you wanted it to show the name, though for debugging I mostly don't bother. When sending things over the network with a multiplayer mod, you can use this knowledge to avoid adding a new event type for every little thing you want to do (there are only 128 types available): by making the events generic, and sending the involved troops, items, strings (preset only, of the type "str_something"), sounds, or whatever else as int parameters.
Additional to the comments at the top of the module_* files, there are sometimes comments in the corresponding header_* file, or sometimes in another header; in addition to header_operations, header_triggers and header_common are examples of files with information in them relevant to a lot of different files.

Sinisterius

  • Guest
Re: An Introduction to Module System Syntax and Usage
« Reply #3 on: October 22, 2010, 12:19:00 PM »
Yeah, nice. I'm afraid it might be a bit too complicated to a beginner modder, but then again, it can't get any simpler...

Idibil

  • Grandmaster Knight
  • *
    • View Profile
    • Nova Regula
  • Faction: Nord
  • MP nick: CeltíberoIdibil
Re: An Introduction to Module System Syntax and Usage
« Reply #4 on: October 22, 2010, 02:16:21 PM »
Nice iniciative Caba`drin!

Nate

  • Knight
  • *
  • A time for war and a time for peace.
    • View Profile
  • Faction: Rhodok
  • MP nick: Wappaw_Pawn
  • M&BWB
Re: An Introduction to Module System Syntax and Usage
« Reply #5 on: October 31, 2010, 07:54:43 PM »
As a rookie programmer, I'm finding this guide to be very helpful. I have no doubt that I'll be coming back to it many times in the near and far future.

Yeah, nice. I'm afraid it might be a bit too complicated to a beginner modder, but then again, it can't get any simpler...

Something that may help people with little to no initial programming knowledge could be adding a basic terms and explanations section in spoiler form at the end (e.g. explaining that '!=' means 'not equal to'). In this way excessive explanation that would only hinder knowledgeable programmers wouldn't clog up the main part of the guide, but beginners could have a ready reference, allowing them smoother progress.
"Drawing on my fine command of the English language, I said nothing." - Robert Benchley

Davee

  • Squire
  • *
  • For every time you say LOW-PORN ORC, a viking dies
    • View Profile
    • Vikingr Module
  • Faction: Nord
  • MP nick: Davee
  • M&BWB
Re: An Introduction to Module System Syntax and Usage
« Reply #6 on: November 08, 2010, 09:57:59 AM »
Extremely helpful! I vote sticky (or at least a reference in the stickies)!

Now if I could just find a list of what the different operations do...
"its like having a hero on our side :D" -pez

Laszlo

  • Sergeant Knight at Arms
  • *
    • View Profile
    • Knights of the South
  • Faction: Swadian
  • MP nick: KoS_Laszlo
  • M&BWBNW
Re: An Introduction to Module System Syntax and Usage
« Reply #7 on: November 10, 2010, 06:50:48 AM »
I really need to read this several times over :)

Thanks for the guide!

COGlory

  • Recruit
  • *
    • View Profile
  • Faction: Neutral
Re: An Introduction to Module System Syntax and Usage
« Reply #8 on: January 08, 2011, 06:33:32 PM »
This thread should be stickied.  It's helped me greatly in mentally transforming the MS into C#.  Thank you very much! 

Cromcrom

  • Guest
Re: An Introduction to Module System Syntax and Usage
« Reply #9 on: January 17, 2011, 04:18:16 PM »
Very very helpful, thanks a LOT

Sayd Ûthman

  • Language Moderator
  • *
  • Battle of Europe Developer
    • View Profile
  • Faction: Neutral
  • MP nick: Sayd_uthman
  • WB
Re: An Introduction to Module System Syntax and Usage
« Reply #10 on: April 09, 2011, 01:36:24 AM »
Learned a lot from this , thank you

13exa

  • Knight at Arms
  • *
  • B23A, RoB, and MC Modder
    • View Profile
  • Faction: Nord
Re: An Introduction to Module System Syntax and Usage
« Reply #11 on: May 09, 2011, 02:21:31 PM »
So what is the function of that Global Variables??
(click to show/hide)
(click to show/hide)

Caba`drin

  • Administrator
  • *
  • It's time to toss the dice.
    • View Profile
  • Faction: Nord
  • MP nick: Caba_drin
  • M&BWBWF&SNW
Re: An Introduction to Module System Syntax and Usage
« Reply #12 on: May 09, 2011, 02:25:05 PM »
So what is the function of that Global Variables??
The function of global variables depends on the context. Often they are used to track player-specific values across the game, cf. "$player_honor" throughout the mod sys, or some other single-dimension value that needs to be used in many places, cf. "$g_battle_won" in mission templates.

If you are trying to track a value for only a short period between code blocks, then a register is fine (so long as you pick one that you know won't be over-written). If the value is multi-dimensional, or needs to be tracked for many troops/parties/items, then a slot is a far better choice.



13exa

  • Knight at Arms
  • *
  • B23A, RoB, and MC Modder
    • View Profile
  • Faction: Nord
Re: An Introduction to Module System Syntax and Usage
« Reply #13 on: May 09, 2011, 02:37:44 PM »
So what is the function of that Global Variables??
The function of global variables depends on the context. Often they are used to track player-specific values across the game, cf. "$player_honor" throughout the mod sys, or some other single-dimension value that needs to be used in many places, cf. "$g_battle_won" in mission templates.

If you are trying to track a value for only a short period between code blocks, then a register is fine (so long as you pick one that you know won't be over-written). If the value is multi-dimensional, or needs to be tracked for many troops/parties/items, then a slot is a far better choice.
Well. I want to make a description of my problem.
Examples, I want to make a Game Menus that will give player an Item, but they are must be waiting for some hours. So i take Trade Assessment as Basic.

The Concept:
First, The Confirmation Menu about Waiting for 3 Hours
                                          V

            Rest for Hours = Waiting for Getting Item
                                          V
Last, The Confirmation that Player have the item on the Inventory


My problem is at the Red word. I can't make from Rest Hour to back to "Last, The Confirmation that Player have the item on the Inventory"

Have a solution, Caba' drin?
(click to show/hide)
(click to show/hide)

Toredain

  • Recruit
  • *
    • View Profile
  • Faction: Neutral
  • WB
Re: An Introduction to Module System Syntax and Usage
« Reply #14 on: June 13, 2011, 07:01:11 PM »
Looking in some module py files I see slots skip some numbers so there may be slot 21 and slot 23 but nothing slot 22.

Are unused slots "open" or is it okay to add slots?