A to Z Scripting: Setting up the .script files

From The DarkMod Wiki
Revision as of 10:03, 21 December 2020 by Dragofer (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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.

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.

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 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.

A map .script file must always have a main() script. This script must always stay at the bottom of your .script file:

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

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.

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.


General scripts

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.

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":

#include "script/my_custom_script.script"


Next / previous article