A to Z Scripting: Setting up the .script files: Difference between revisions

From The DarkMod Wiki
Jump to navigationJump to search
(Created page with "== Setting up the .script files == A .script file is simply a .txt file whose .txt extension has been renamed to .script. If you can't see or change the .txt extension, make s...")
 
(changed 'reloadScripts' to 'reloadscript'. 'reloadScripts' with a trailing 's' returns 'Unknown command 'reloadScripts')
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
== Setting up the .script files ==
A .script file is simply a .txt file whose .txt extension has been renamed to .script. If you can't see or change the .txt extension, make sure your operating system isn't set to hide known file extensions.
A .script file is simply a .txt file whose .txt extension has been renamed to .script. If you can't see or change the .txt extension, make sure your operating system isn't set to hide known file extensions.


Any simple text editor, such as Notepad or Wordpad, can be used to work with scripts, though you may want to download a more elaborate text editor to profit from features such as conditional highlighting to help you keep track of which brackets go together.
Any simple text editor, such as Notepad or Wordpad, can be used to work with scripts, though you may want to download something more elaborate like Notepad++. The main advantage would be automatic highlighting i.e. of brackets that go together.


If you're opening a .script file for the first time, you will be asked to set your preferred text editor as the standard program for opening this kind of file.
If you're opening a .script file for the first time, you will be asked to set your preferred text editor as the standard program for opening this kind of file.




=== Map scripts ===
== Map .scripts ==


The easiest way to set up scripts is as a map script. Any scripts in this file will only be available in that map, which is often all you need. Create a new .txt file in your maps folder with the same name as your map and change the .txt extension to .script.
The easiest way to set up scripts is in a map .script. Any scripts in this file will only be available in that map, which is often all you need. Create a new .txt file in your maps folder with the same name as your map and change the .txt extension to .script.


A map .script file must always have a main() script. This script must always stay at the bottom of your .script file:
 
It's a good habit to put a main() script into the bottom of your map .script. This gets called automatically at map start, which makes it very convenient in case you want to quickly test a script or run some script events at the beginning of the map.


  void main()
  void main()
Line 18: Line 18:
  }
  }


The main() script is automatically called at map start. This makes it convenient for testing scripts and initialising scripts that should run from the beginning of the mission.
The main() script should begin with sys.waitFrame(). This is because all entities are spawned in the first frame of the mission, so waiting a single frame makes sure they're ready for script events.
 
 
== General .scripts ==
 
General scripts are available in all missions, which is useful if you're making a campaign or a new type of entity. The .script file can have any name that's not already taken by a core TDM script (they all begin with tdm_) and must be placed within the "script" folder (without an s) instead of the "maps" folder. Unlike map .scripts, general .scripts should not contain a main() function.
 
Your script folder will need to contain an additional file called tdm_custom_scripts.script. This instructs the game to #include your custom .script files. As an example, see below what line is needed in tdm_custom_scripts.script to include a file called general_script1.script:
 
#include "script/general_script1.script"
 


It's good practice to let the main() script begin with sys.waitFrame();. This is because all entities are spawned in the first frame of the mission, so waiting a single frame makes sure they're ready for script events.
Note that if there are mistakes in your general .scripts, you won't be able to start TDM until you've solved all of them (map .scripts with errors would let you start the game, but not the map). You'll see a window telling you which line the mistake was on. Press the 'copy' button and paste into i.e. Notepad or Word to read the complete error message. Some people may find it more convenient to develop scripts in a map .script first before moving them into a general .script.




=== General scripts ===
== Reloading scripts after making changes ==


General scripts are available in all missions, which is useful if you're making a campaign or some kind of scripting addon. The downside is that you have to restart TDM (rather than just the map) before changes take effect. Also, any mistakes stop TDM from starting up with a blue screen, and you will need to press "copy" and paste the error message somewhere like Notepad or Word to read it. Therefore it's recommended to develop your script as a map script first.
After you've saved changes in your .script file, you need to tell the game to reload scripts. This can be done in one of the following ways:
* typing 'reloadscript' into the console
* for map .scripts: restarting the map
* restarting TDM


The .script file can have any name and must be placed within the "script" folder (without an s) instead of the "maps" folder. Unlike map scripts, general scripts should not contain a main() function.


Your script folder always needs to contain a tdm_custom_scripts.script file. This instructs the game to #include your custom .script files into the engine's inclusion chain. As an example, see below what line is needed in tdm_custom_scripts.script to #include a .script file called "my_custom_script":
The fastest way is to bind the console command 'reloadscript' to a hotkey. Do this by typing the following into the console (if you want to bind to p):
bind "p" "reloadscript"


#include "script/my_custom_script.script"
Note that you have to be ingame for hotkeys to work.




Line 39: Line 52:
*Previous article: [[A to Z Scripting: More scripting basics]]
*Previous article: [[A to Z Scripting: More scripting basics]]
*Table of contents: [[A to Z Scripting]]
*Table of contents: [[A to Z Scripting]]
[[Category:Scripting]]

Latest revision as of 14:31, 25 June 2022

A .script file is simply a .txt file whose .txt extension has been renamed to .script. If you can't see or change the .txt extension, make sure your operating system isn't set to hide known file extensions.

Any simple text editor, such as Notepad or Wordpad, can be used to work with scripts, though you may want to download something more elaborate like Notepad++. The main advantage would be automatic highlighting i.e. of brackets that go together.

If you're opening a .script file for the first time, you will be asked to set your preferred text editor as the standard program for opening this kind of file.


Map .scripts

The easiest way to set up scripts is in a map .script. Any scripts in this file will only be available in that map, which is often all you need. Create a new .txt file in your maps folder with the same name as your map and change the .txt extension to .script.


It's a good habit to put a main() script into the bottom of your map .script. This gets called automatically at map start, which makes it very convenient in case you want to quickly test a script or run some script events at the beginning of the map.

void main()
{
	sys.waitFrame();
}

The main() script should begin with sys.waitFrame(). This is because all entities are spawned in the first frame of the mission, so waiting a single frame makes sure they're ready for script events.


General .scripts

General scripts are available in all missions, which is useful if you're making a campaign or a new type of entity. The .script file can have any name that's not already taken by a core TDM script (they all begin with tdm_) and must be placed within the "script" folder (without an s) instead of the "maps" folder. Unlike map .scripts, general .scripts should not contain a main() function.

Your script folder will need to contain an additional file called tdm_custom_scripts.script. This instructs the game to #include your custom .script files. As an example, see below what line is needed in tdm_custom_scripts.script to include a file called general_script1.script:

#include "script/general_script1.script"


Note that if there are mistakes in your general .scripts, you won't be able to start TDM until you've solved all of them (map .scripts with errors would let you start the game, but not the map). You'll see a window telling you which line the mistake was on. Press the 'copy' button and paste into i.e. Notepad or Word to read the complete error message. Some people may find it more convenient to develop scripts in a map .script first before moving them into a general .script.


Reloading scripts after making changes

After you've saved changes in your .script file, you need to tell the game to reload scripts. This can be done in one of the following ways:

  • typing 'reloadscript' into the console
  • for map .scripts: restarting the map
  • restarting TDM


The fastest way is to bind the console command 'reloadscript' to a hotkey. Do this by typing the following into the console (if you want to bind to p):

bind "p" "reloadscript"

Note that you have to be ingame for hotkeys to work.


Next / previous article