Structure of a script

From ZDoom Wiki
Jump to: navigation, search

How to write a script? Let's start with a simple example:

// My first ACS script

#include "zcommon.acs"
 
script 1 ENTER
{
  print (s:"Hello world!");
}


What does this script do? We'll look at each line and explain:

// My first ACS script

This is a comment line. Comments doesn't affect the behavior of the script, they serve only as notes or short explanations what the script does. See the comments section below for more info.

#include "zcommon.acs"

The #include directive instructs the compiler to open the given file and process its contents before continuing processing the current file. The file zcommon.acs is a standard file which contains all basic definitions. Basically every script file you make must include that file. (Note: vanilla Hexen scripts will include common.acs instead; and some other ports such as Vavoom may provide a different file such as vcommon.acs.)

script 1 enter

This is a beginning of a script definition. The script's identification number is 1 and the enter keyword means that the script is activated by every player on start of a level. Script numbers can be 1-32767. Alternatively, scripts can be named, so the following line would also work:
script "Example script" enter

print (s:"Hello world!");

This line is an ACS statement. Print is the name of the built-in function that prints a message on screen - this one prints "Hello world!".

Comments

Comments are parts of the source code ignored by the compiler. They simply do nothing. Their purpose is to allow programmers to insert notes or descriptions in the source code.


ACS supports 2 styles of comments:

// line comment
/* block comment */

The first one, known as line comment begins where the pair of slash signs (//) begin and end at the end of the same line. The second one, known as block comment starts at /* characters and ends at the first appearance of */ characters.


/* Wow, my second ACS script, now with more comments.
   As you can see this is a block comment so it can go
   through
   more
   lines */

#include "zcommon.acs"

// This is executed when player enters the level 
script "Welcome Player" ENTER
{
  print (s: "Hello world!");  // Print the sentence "Hello world!" on screen
}