A to Z Scripting: Setting up the .script files

From The DarkMod Wiki
Revision as of 22:54, 21 December 2020 by Dragofer (talk | contribs)
Jump to navigationJump to search

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 'reloadScripts' into the console
  • for map .scripts: restarting the map
  • restarting TDM


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

bind "p" "reloadScripts"

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


Next / previous article