Results 1 to 12 of 12

Thread: Beginner's Scripting Guide

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Beginner's Scripting Guide

    Hello all . With this post , i hope to get a lot more players/modders interested in scripting . Most people get lost in scipting immediatly , and then say that it's way to hard . I used to do this too , until i kept trying again , and now i really like scripting ..

    It's all about the way you approach it . Some tutorials for scripting just ask to copy-paste a certain script in the appropriate folders and teach you almost nothing . Others start off good , but become to complicated at the end .

    I'll try to come off as gentle as possible with this tutorial . We'll take it step-by-step , and i'll try to make it easy to understand ..

    But at first , we need a background script . I won't give any further comments about this , since HouseOfHam already has an excellent post about it . Check it out here :

    http://www.twcenter.net/forums/showthread.php?t=169689

    Step 1 : How to make a script

    Step 2 : Basic conditions and commands

    Step 3 : Making more complicated scripts

    Step 4 : Some other stuff

    Step 5 : What if's and counters ?



    Last edited by Killerbee; May 23, 2009 at 09:33 AM.

  2. #2

    Default Re: Beginner's Scripting Guide

    Step 1 : How to make a script

    Spoiler Alert, click show to read: 


    Once you have a background script , open the file in your data/scripts/show_me folder called background_script (so the folder in wich you made your background script) . If you took it from HoH's tutorial (HoH = HouseOfHam) , then it should look like this :

    Spoiler Alert, click show to read: 
    Code:
    script
    
    ; Anything following a semicolon is a comment.
    
    ; Remove the adviser portrait from screen.
    select_ui_element advisor_dismiss_button
    simulate_mouse_click lclick_up
    
    ; Wait for it to go away.
    while I_AdvisorVisible
    end_while
    
    suspend_unscripted_advice true
    
    ; Open the adviser message bubble automatically whenever advance_advice_thread is called.
    ; I recommend using this method instead of the select_ui_element + simulate_mouse_click approach.
    ; Do NOT mix both methods, though, or the advisor will show and then immediately close before
    ; you get a chance to read the text.
    declare_show_me
    
    ; Very useful for debugging - uncomment to use
    ;console_command toggle_perfect_spy
    
    ;;;
    ;;; --- Forced shutdown ---
    ;;;
    ;;; Press 'Esc' on the campaign map, then click on the '?' button in the
    ;;; menu scroll to terminate the script.
    ;;;
    ;;; When would this be useful? -- When you are already in a game and
    ;;; exit back to the main menu to restart the campaign, or reload a saved
    ;;; game, RTW does not automatically terminate the script, so you have
    ;;; to do it yourself. If you leave the old script running, you'll have all
    ;;; sorts of weird problems with the script in the new game.
    ;;;
    monitor_event ScrollAdviceRequested ScrollAdviceRequested end_game_scroll
        terminate_script
    end_monitor
    
    ; Handle saved game reloads
    monitor_event GameReloaded TrueCondition
      terminate_script
    end_monitor
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;; Start script
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    while I_TurnNumber < 99999
      suspend_unscripted_advice true
    end_while
    end_script


    Alright , let's get started with scripting .. If your mod uses some small scripts , you can put all scripts in the 'background_script' folder . If you try to get a heavy scripted mod however (like LOTR-TW) , it might be better to make multiple files , to sort everyting out . For instance , Bardo (the LOTR-TW scripter) made for his 'palantir script' 3 new entries in 3 different folders . the palantir script is based on the 'Palantir' in the LOTR-trilogy , and shows the world for 10 seconds .. I'll show you where he pu all entries , so you could understand :

    Spoiler Alert, click show to read: 


    In the background_script.txt file :
    Spoiler Alert, click show to read: 
    Code:
    ;**********************************************
    ;PALANTIR trigger:
    ;**********************************************
    
    monitor_event CharacterTurnStart CharacterIsLocal
       and not FactionType romans_julii
       and Attribute Electability = 10
       and RandomPercent < 10
    advance_advice_thread UsePalantir_Thread
    end_monitor
    monitor_event FactionTurnEnd I_FactionNearTile slave 0 248,211
       console_command move_character SpecialGuy, 249,212
    end_monitor


    Take a look at the underlined line . It targets to another line , that's inside the 'export_descr_advice'.txt file . Now , if we take a look at that file , and search for 'Palantir' , we see the following :

    Inside the export_descr_advice.txt' file :
    Spoiler Alert, click show to read: 
    Code:
    ;------------------------------------------ LOTR-TW
    AdviceThread UsePalantir_Thread
        GameArea Campaign
    
        Item UsePalantir_Text_01
        Uninhibitable
        Verbosity  0
        Threshold  1
            MaxRepeats  0
            RepeatInterval  1
            Attitude Excited
        Presentation Default
        Title UsePalantir_Text_01_Title
        Script scripts\show_me\palantir.txt
        Text UsePalantir_Text_01_Text1


    And here again , take a look at the underlined line . it targets us to the data/scripts/show_me/palantir file . And if we take a look there ... :

    In the palantir.txt file :
    Spoiler Alert, click show to read: 
    Code:
    script
    
       zoom_strat_camera 1
       inhibit_camera_input true
       console_command toggle_fow
       console_command toggle_perfect_spy
       wait 20
       console_command toggle_perfect_spy
       console_command toggle_fow
       inhibit_camera_input false
    
       console_command move_character SpecialGuy, 248,211
    
    select_ui_element advisor_dismiss_button
    simulate_mouse_click lclick_down
    simulate_mouse_click lclick_up
    
    end_script


    ... we see the actual script , or what happens when all previous "triggers" are activated


    So , this was just to show you , that when you make lots of scripts , you better organize it . here , we saw a 'loop' , that goes from the background_script.txt file , to the 'export_descr_advice.txt' file , and ends in the 'palantir.txt' file .

    Alright , now , lets take a look at how most (or the basic) scripts are made :

    Code:
    script
    
    monitor_event 'conditions'
    
    'commands given to the game'
    end_monitor
    
    end_script
    Some experienced modders may say that that's not totally true , but as i mentioned , we'll start of as easy and gentle as possible .. now , you can put this in your backround script , but it must be between the 'loop' :

    Spoiler Alert, click show to read: 
    Code:
    while I_TurnNumber < 99999
      suspend_unscripted_advice true
    end_while
    end_script


    and the previous stuff :

    Spoiler Alert, click show to read: 
    Code:
    script
    
    ; Anything following a semicolon is a comment.
    
    ; Remove the adviser portrait from screen.
    select_ui_element advisor_dismiss_button
    simulate_mouse_click lclick_up
    
    ; Wait for it to go away.
    while I_AdvisorVisible
    end_while
    
    suspend_unscripted_advice true
    
    ; Open the adviser message bubble automatically whenever advance_advice_thread is called.
    ; I recommend using this method instead of the select_ui_element + simulate_mouse_click approach.
    ; Do NOT mix both methods, though, or the advisor will show and then immediately close before
    ; you get a chance to read the text.
    declare_show_me
    
    ; Very useful for debugging - uncomment to use
    ;console_command toggle_perfect_spy
    
    ;;;
    ;;; --- Forced shutdown ---
    ;;;
    ;;; Press 'Esc' on the campaign map, then click on the '?' button in the
    ;;; menu scroll to terminate the script.
    ;;;
    ;;; When would this be useful? -- When you are already in a game and
    ;;; exit back to the main menu to restart the campaign, or reload a saved
    ;;; game, RTW does not automatically terminate the script, so you have
    ;;; to do it yourself. If you leave the old script running, you'll have all
    ;;; sorts of weird problems with the script in the new game.
    ;;;
    monitor_event ScrollAdviceRequested ScrollAdviceRequested end_game_scroll
        terminate_script
    end_monitor
    
    ; Handle saved game reloads
    monitor_event GameReloaded TrueCondition
      terminate_script
    end_monitor


    So in the end , it looks like this :

    Spoiler Alert, click show to read: 
    Code:
    script
    
    ; Anything following a semicolon is a comment.
    
    ; Remove the adviser portrait from screen.
    select_ui_element advisor_dismiss_button
    simulate_mouse_click lclick_up
    
    ; Wait for it to go away.
    while I_AdvisorVisible
    end_while
    
    suspend_unscripted_advice true
    
    ; Open the adviser message bubble automatically whenever advance_advice_thread is called.
    ; I recommend using this method instead of the select_ui_element + simulate_mouse_click approach.
    ; Do NOT mix both methods, though, or the advisor will show and then immediately close before
    ; you get a chance to read the text.
    declare_show_me
    
    ; Very useful for debugging - uncomment to use
    ;console_command toggle_perfect_spy
    
    ;;;
    ;;; --- Forced shutdown ---
    ;;;
    ;;; Press 'Esc' on the campaign map, then click on the '?' button in the
    ;;; menu scroll to terminate the script.
    ;;;
    ;;; When would this be useful? -- When you are already in a game and
    ;;; exit back to the main menu to restart the campaign, or reload a saved
    ;;; game, RTW does not automatically terminate the script, so you have
    ;;; to do it yourself. If you leave the old script running, you'll have all
    ;;; sorts of weird problems with the script in the new game.
    ;;;
    monitor_event ScrollAdviceRequested ScrollAdviceRequested end_game_scroll
        terminate_script
    end_monitor
    
    ; Handle saved game reloads
    monitor_event GameReloaded TrueCondition
      terminate_script
    end_monitor
    
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ;;; Your script
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    
    ;script Note that the 'script' lines , are already listed in this file . if you take a look at the top of the file , you'll see the word 'script there , meaning that it's already listed in this script . Scripts must have only 1 'script' , and 'end_script' "word" .
    
    monitor_event 'conditions'
    
    'commands given to the game'
    end_monitor
    
    ;end_script Same as above
    
    while I_TurnNumber < 99999
      suspend_unscripted_advice true
    end_while
    end_script


    Alright , now we've put up the base , we'll start with some basic commands and stuff
    Last edited by Killerbee; May 09, 2009 at 07:03 AM.

  3. #3

    Default Re: Beginner's Scripting Guide

    Step 2 : Basic conditions and commands

    Spoiler Alert, click show to read: 
    Alright , we'll start of with the probably most basic condition : The 'turn start' condition . This condition will be activated whenever it's your turn again . So when you press 'end turn' , it shows all other factions , and then it's your turn again . The "correct description" for this condition is :

    Code:
     FactionTurnStart FactionIsLocal
    Unfortunately , we can't type what we want , so for instance :

    Code:
    monitor_event If i'm at the start of my turn
    
    end_monitor
    Now , if we put this in our little 'basic script' , we'll get the following :

    Code:
    monitor_event FactionTurnStart FactionIsLocal
    
     'commands given to the game'
     end_monitor
    Alas , we need the correct description , so double check spelling , so that you don't make silly typing errors like this:

    'FactionTurnTsart FactionIsLocal'

    Now , the script will activate as soon as it's your turn . only 1 problem : We don't have any commands listed .

    So for now , we'll use a basic command : Giving money to a faction ..
    Code:
    console_command add_money 'faction' 'amount'
    Now , this won't work , as we need to specify the faction that gets the money , and the amount of cash it gets .. So we get this :

    Code:
    console_command add_money greek_cities 1000
    Thus , this will add 1000 money to the greeks .. but when ? That is why we use conditions . Previously , you set up a basic condition , that activates at each start of your turn .. So , if we mix both conditions and commands , we get this :

    Code:
    monitor_event FactionTurnStart FactionIsLocal
    
    console_command add_money greek_cities 1000
    
    end_monitor
    This will add 1000 denarii to the greeks , no matetr wich faction you're playin as .

    Congratz , you put up a basic script ! You can test it out now if you want .

    If you don't know what the 'console_command' is , i'll explain it . Most "cheaters" will already now it ! So cheaters actually have an advantage in scripting ..

    The 'console panel' , is a "program" that you can activate during your campaign , by pressing '~' (without quotes) , '²' , or something else . When you opened it , you can type some 'commands' in it , like :

    add_money greek_cities 1000

    So , then the greek_cities get 1000 denarii .. Already saw it ? In our script , we "opened" the console panel whenever it's your turn , and gave a command to it , just like above . (add_money greek_cities 1000)
    Last edited by Killerbee; May 09, 2009 at 07:03 AM.

  4. #4

    Default Re: Beginner's Scripting Guide

    Step 3 : Making more complicated scripts

    Spoiler Alert, click show to read: 
    Part 1 :

    Alright , you made a script , but with the previous commands and conditions , you won't be able to get some nice results . That's why we'll dig a little deeper .

    First of all , I'll show you a new condition . Also a pretty basic one , and it also involves 'Factions' . This is how you should put it in your script :

    Code:
    I_FactionType greek_cities
    As you could probably see , this condition looks if you're the stated faction , in this case , if you're playing as the greeks



    Part 2 :
    However , you can't just use it like our previous condition .. So you can't use it this way :
    Code:
    monitor_event I_LocalFaction greek_cities
    However , there are 2 other ways to do it :

    Either you use this :
    Code:
    monitor_event FactionTurnStart FactionIsLocal
     and I_LocalFaction greek_cities
    
    -your commands-
    
    end_monitor
    or this :

    Code:
    if I_LocalFaction greek_cities
    
    -yourcommands-
    
    end_if
    i suggest you use the first options , as you probably don't know what an 'if' is , although you can probably guess it So , if we use our new script :

    Code:
    monitor_event FactionTurnStart FactionIsLocal
     and I_LocalFaction greek_cities
    
    console_command add_money greek_cities 1000
    
    end_monitor
    Instead of our previous script , this won't occur each time it's your turn . You also need to be playing as the greeks . So , you need to be playing as the greeks , and it needs to be your turn , in order to activate the command .

    Now , we also need a new command . We'll take a very popular , and probably the most used one : The spawn_army command ..

    this is how it should be listed in your script :

    Code:
    spawn_army
       faction 'faction' ; (so for instance , greek_cities)
       character "character name" ,'type of character' (for instance , general , named_character , ...) ,age ,coörds
       'list of units' , for instance : 
       unit greek general's guard cavalry early, 'amount of soldiers' 'exp' 'armour' 'weapon_lvl'
       end
    Now , this won't work ofcourse .. We need to get some working names , coörds , factions , etc .. So , if we do everyting correctly , it should look like this :

    Code:
    spawn_army
       faction greek_cities ; So this will spawn an army for the greek faction
       character Polydoros, general, age 25, x 123, y 65
       unit greek general's guard cavalry early, 30 exp 5 armour 2 weapon_lvl 3
       end
    This will spawn a general for the greeks called Polydoros , who'll be of age 25 , and he'll be spawned on the listed coördinates . As for the units , there'll be a greek early general , with 30 units , 5 experience bonus , 2 armour bonus , and 3 wep_lvl bonus .

    But i won't go any deeper here , as there are already lots of tutorials about this command :

    HouseOfHam's tutorial :
    Tutorial

    Now , if we use this command in a script , combined with our new conditions , it should look like this :

    Code:
    monitor_event FactionTurnStart FactionsIslocal
     and I_LocalFaction greek_cities
    Code:
    spawn_army
       faction greek_cities ; So this will spawn an army for the greek faction
       character Polydoros, general, age 25, x 123, y 65
        unit greek general's guard cavalry early, 30 exp 5 armour 2 weapon_lvl 3
        end
    end_monitor
    Alright , we now have a more complicated script than our previous one .


    There are much more conditions and commands , and they are listed in the docudemons .. Download them here :

    http://www.totalwar.org/Downloads/Rt...ad/BI_docs.zip

    Just unzip it wherever you want . there should be 4 text files in them .. Just check the appropriate folder to find conitions or commands , or whatever you like .
    Last edited by Squid; December 16, 2010 at 11:28 AM.

  5. #5

    Default Re: Beginner's Scripting Guide

    Step 4 : Some other stuff

    There are some other basic thingies you can use in scripts .. For instance , the 'not' word .. this can be used in conditions , to trigger when something isn't activated , but for instance :

    Code:
    monitor_event FactionTurnStart FactionIsLocal
       and not I_LocalFaction romans_julii
    So , if you're playing as the Julii , this script will not trigger ! If you're playing as another faction , however , it should trigger perfectly This can be used for some scripts (e.g.scripts who add units to weaker factions faction , so they die less quickly) .. For instance :

    Code:
    monitor_event FactionTurnStart FactionIsLocal
      and not I_LocalFaction romans_julii
      and not I_LocalFaction romans_brutii
      and not I_LocalFaction romans_scipii
      and not I_LocalFaction romans_senate
      and not I_LocalFaction egypt
      and RandomPercent < 10 ; This declares a random percent , so that the script only activated 1/10 of ;the time
    
    spawn_army 'blah blah'
       character 'blah blah'
       units 'blah blah'
       end
    end_monitor
    

    This script will neither activate for all roman factions , nor for the egyptians , as these are the strongest factions in vanilla ..

    Last edited by Killerbee; May 09, 2009 at 04:19 PM.

  6. #6

    Default Re: Beginner's Scripting Guide

    Step 5 : What if's and counters ?

    Spoiler Alert, click show to read: 
    In one of my previous examples , i showed you something new .. The usage of 'if' , instead of 'monitor_event' .. Some conditions require an 'if' instead of 'monitor event' , like this one :

    Code:
    if I_SettlementOwner Sparta = greek_cities
    
    - yourcommands  -
    
    end_if
    with this example , i showed you how to use the 'if' , and i showed you a new condition .. This condition can be used if you want a certain faction to own a settlement , in order to activate the script .. This is a very usefull condition , and is one of my most-used ones (although i'm not so long into scripting )

    This condition can also be used in combination with 'counters' .. What are counters , you say ? Take a look here

    this combination can give you some nice results :

    Code:
    if I_SettlementOwner Sparta = greek_cities
    set_counter SettlementOwner_Sparta 1
    
    end_if
    
    If I_CompareCounter SettlementOwner_Sparta = 1
    
    console_command add_money romans_julii -1000
    
    end_if
    1 Note about this script is that i used a negative digit to add money .. So what will it do ? CTD ? nope , it'll just decrease the amount of denarii the romans_julii get , by 1000

    -More will be added later -

    Last edited by Killerbee; May 23, 2009 at 09:34 AM.

  7. #7

    Default Re: Beginner's Scripting Guide

    Very nice guide, will help me create a nice script. However, I had a question though:

    The file that we could download, containing the command line codes. Most of them doesn't seem to be working. I am new to modding/scripting RTW and I have no idea why it won't work. Just get a message that the command is not recognized.

  8. #8
    Squid's Avatar Opifex
    Patrician Artifex Technical Staff

    Join Date
    Feb 2007
    Location
    Frozen waste lands of the north
    Posts
    17,751
    Blog Entries
    3

    Default Re: Beginner's Scripting Guide

    There are many console commands that can't actually be run in the console and can only be run through a script.
    Under the patronage of Roman_Man#3, Patron of Ishan
    Click for my tools and tutorials
    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe." -----Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •