Scripting basics: Difference between revisions

From The DarkMod Wiki
Jump to navigationJump to search
No edit summary
(Cleanup, fact correction, moved from coding category to scripting category)
Line 1: Line 1:
Doom 3 scripts are text files with a .script extension, typically located in the ''script'' directory. ('''FIXME:''' Are these requirements or conventions?)
Doom 3 scripts are text files containing script code in a proprietary syntax. By convention, they are given a <tt>.script</tt> extension. Game-wide script files are located in the <tt>script</tt> directory, while map-specific script files are located in the same directory as the corresponding map.


== Syntax ==
== Syntax ==
Line 5: Line 5:
The syntax of the scripts is vaguely C++-like, though the scripts are nowhere near as powerful. They have standard braces-and-semicolons syntax, and a notion of "classes". Class members are defined inside a class declaration and defined separately, rather like C++. Unlike C++, there are no separate header files; everything goes into one script file.
The syntax of the scripts is vaguely C++-like, though the scripts are nowhere near as powerful. They have standard braces-and-semicolons syntax, and a notion of "classes". Class members are defined inside a class declaration and defined separately, rather like C++. Unlike C++, there are no separate header files; everything goes into one script file.


Doom 3 scripts only have a few data types:
Doom 3 scripts have only a few data types:


* <tt>float</tt> - A number.
* <tt>float</tt> - A number.
Line 19: Line 19:
== Discovery (or, why is Doom 3 ignoring my script file?) ==
== Discovery (or, why is Doom 3 ignoring my script file?) ==


Doom 3 will automatically find and load some kinds of files, such as .def files. However, it does not do the same for script files; if you just create a .script file and dump it into the script directory, Doom 3 will blithely ignore it.
Doom 3 will automatically find and load some kinds of files, such as .def files. However, it does not do the same for script files; if you merely create a .script file and place it in the <tt>script</tt> directory, Doom 3 will blithely ignore it.


There are two ways to make Doom 3 use a script:
There are two ways to make Doom 3 use a script:


* Place it in a map script. Maps scripts are loaded when the corresponding map is loaded, but they must be named the same as the map file. For example, the map script for <tt>mymap.map</tt> must be called <tt>mymap.script</tt>. Map scripts are used to implement map-specific behaviours; as such, mappers will often use them but mod coders typically won't.
* Place it in a map script. Map scripts are loaded when the corresponding map is loaded. They must be given the same name and placed in the same location as the corresponding map file, but with a .script extension instead of a .map extension. For example, the map script for <tt>maps/mydir/mymap.map</tt> must be called <tt>maps/mydir/mymap.script</tt>. Map scripts are used to implement map-specific behaviours; as such, mappers will often use them but mod coders typically won't.


* There is only one script that Doom 3 loads for all maps. It's called <tt>doom_include.script</tt> ('''FIXME:''' is this correct?). Since this script would be extremely long if it contained all the general-purpose script code in the ''entire mod'', we don't put any script code in <tt>doom_include.script</tt> directly. Instead, we use <tt>#include</tt> directives to pull in scripts with different names. For example, <tt>darkmod_include.script</tt> is <tt>#include</tt>d from <tt>doom_include.script</tt>:
* There is only one script that Doom 3 loads for all maps. It's called <tt>script/doom_main.script</tt>, and is loaded before the map-specific script. Since this script would be extremely long if it contained all the general-purpose script code in the ''entire mod'', we don't put any script code in <tt>doom_main.script</tt> directly. Instead, we use <tt>#include</tt> directives to pull in scripts with different names. For example, <tt>darkmod_include.script</tt> is <tt>#include</tt>d from <tt>doom_main.script</tt>.


Most of the TDM-specific scripts, including <tt>ai_darkmod_base.script</tt>, are <tt>#include</tt>d from <tt>darkmod_include.script</tt>. We could also put further <tt>#include</tt> directives into <tt>ai_darkmod_base.script</tt>, and so on. As long as a script gets <tt>#include</tt>d at least once, it will get loaded in all maps. Note that you should be careful not to <tt>#include</tt> the same script twice, or Doom 3 will complain about multiple definitions.
Most of the TDM-specific scripts, including <tt>ai_darkmod_base.script</tt>, are <tt>#include</tt>d from <tt>darkmod_include.script</tt>. We could also put further <tt>#include</tt> directives into <tt>ai_darkmod_base.script</tt>, and so on. As long as a script gets <tt>#include</tt>d at least once, it will get loaded in all maps. Note that you should be careful not to <tt>#include</tt> the same script twice, or Doom 3 will complain about multiple definitions.
Line 32: Line 32:


'''TODO:''' Describe the various ways in which functions in script files can get called.
'''TODO:''' Describe the various ways in which functions in script files can get called.
Note the doom_main function in doom_main.script; presumably this is called on level load???


== Interfacing with the SDK ==
== Interfacing with the SDK ==
Line 43: Line 45:
Also need refs for Dark Mod's scriptevents.
Also need refs for Dark Mod's scriptevents.


[[Category:Coding]]
[[Category:Scripting]]

Revision as of 12:43, 27 September 2007

Doom 3 scripts are text files containing script code in a proprietary syntax. By convention, they are given a .script extension. Game-wide script files are located in the script directory, while map-specific script files are located in the same directory as the corresponding map.

Syntax

The syntax of the scripts is vaguely C++-like, though the scripts are nowhere near as powerful. They have standard braces-and-semicolons syntax, and a notion of "classes". Class members are defined inside a class declaration and defined separately, rather like C++. Unlike C++, there are no separate header files; everything goes into one script file.

Doom 3 scripts have only a few data types:

  • float - A number.
  • vector - Three numbers in one variable; useful for representing locations, velocities, etc.
  • entity - A reference to an entity (a pointer, in C++ parlance).

Note the complete lack of a dedicated integer type, or any ability to define complex data structures! If data structures are needed, they are typically created in the SDK and scriptevents (see below) are provided to allow scripts to manipulate them.

For a more detailed listing of the syntax, see Modwiki's SCRIPT file reference.

TODO: More detail.

Discovery (or, why is Doom 3 ignoring my script file?)

Doom 3 will automatically find and load some kinds of files, such as .def files. However, it does not do the same for script files; if you merely create a .script file and place it in the script directory, Doom 3 will blithely ignore it.

There are two ways to make Doom 3 use a script:

  • Place it in a map script. Map scripts are loaded when the corresponding map is loaded. They must be given the same name and placed in the same location as the corresponding map file, but with a .script extension instead of a .map extension. For example, the map script for maps/mydir/mymap.map must be called maps/mydir/mymap.script. Map scripts are used to implement map-specific behaviours; as such, mappers will often use them but mod coders typically won't.
  • There is only one script that Doom 3 loads for all maps. It's called script/doom_main.script, and is loaded before the map-specific script. Since this script would be extremely long if it contained all the general-purpose script code in the entire mod, we don't put any script code in doom_main.script directly. Instead, we use #include directives to pull in scripts with different names. For example, darkmod_include.script is #included from doom_main.script.

Most of the TDM-specific scripts, including ai_darkmod_base.script, are #included from darkmod_include.script. We could also put further #include directives into ai_darkmod_base.script, and so on. As long as a script gets #included at least once, it will get loaded in all maps. Note that you should be careful not to #include the same script twice, or Doom 3 will complain about multiple definitions.

Script invocation

TODO: Describe the various ways in which functions in script files can get called.

Note the doom_main function in doom_main.script; presumably this is called on level load???

Interfacing with the SDK

No language can operate usefully in a vacuum - at some point it needs to go out and do stuff.

TODO: Describe the various ways in which the SDK can interface with Doom 3 scripts; scriptevents, scriptvars (instances of idScriptVar), etc.

Vanilla Doom 3 script reference: http://www.modwiki.net/wiki/Script_events_%28Doom_3%29

Also need refs for Dark Mod's scriptevents.