Script

Introduction

Clonk supports its own scripting language: C4Script.
Object definitions and scenarios can contain a script. Function calls made from scenario scripts are considered "global" calls. Function calls made within an object script are considered "local" calls.
For further information on the two types of script see object scripts and scenario scripts.
C4Script uses a C-style syntax. It includes operators, variable declarations, compound statements, conditional statements (if) and loops (for and while).

Debugging

Activate the debug mode in the developer section of the game options to have additional error messages displayed. Anyone writing scripts should do this.

Functions

As in other programming languages you can define functions in C4Script:
func CreateRock()
{
  [Erzeugt einen Stein|Image=ROCK]
  CreateObject(ROCK,50,50);
  return(1);
}
Directly following the function name you can include a short descriptive text, an icon reference (object ID of the object whose image is to be used), and a conditional statement for when to display the function in the context menu, all within a set of [ ] brackets, separated by a | symbol.

Variables/Parameters

Variables can hold values of type int, bool, id, string, or object. Any parameter not directly specified in a function call will hold the default value 0. Functions can have a maximum of ten parameters.

Comments

Scripts may contain code comments in C-style. Comment test will be completely ignored when the script is comipiled for execution.
func MeineFunktion() // Ein Kommentar bis zum Ende der Zeile
{
  Message("Dieser Code wird ausgeführt"); /* Ein Kommentar in einem Block */ Message("Dieser auch");
  return(1);
}

Additional Information

#appendto: for appending code to existing scripts
#strict: the new coding standard
Loop Control
Calling Script Functions
Effects
Querying Game Data
Named variables and their scope
Operators: +, -, = etc. in C4Script
Data type checking at runtime
Script Player (i.e. AI player)
Sven2, April 2002
matthes, Juni 2004