Object Scripts

Object scripts control the complex behaviour of an object. For details on scripting see the C4Script documentation.

Creation

For every object, the engine calls the function Initialize in the object script when it is created and completed.
func Initialize()
{
  CreateContents(Rock);
}  
An object with this script will be given a rock right after it has been created. The Initialize function is called only when the object has reached full size (a building only when its construction has been completed and a living being only when it is fully grown).

ActMap

An active object can also define activity script calls in its ActMap. The defined StartCall is made whenever an action begins (or repeats), an EndCall is made at the end of each activity. PhaseCall is called at each animation phase step and should only be used for very short animations. The call frequency of PhaseCalls is determined by the speed of the animation.

#include

An object script can also include the functionality of another script.
#include Clonk
At this position the complete script of the specified object definition (that of the clonk, in this case) is inserted, including all scripts that that script includes or got via #appendto. The only exception is that every script is included only once, so including both the Clonk script and a script that is included by the Clonk script doesn't include that script twice. Other Obviously, the included definition must be valid and loaded. Declared functions can be overloaded by functions of the same name that occur later in the script. Also see inherited().

Interaction from other scripts

Other scripts can call functions of an object obj with the -> and ->~ operators.
var obj = CreateObject(Flint); obj->Hit();

Object calls made by the engine

The engine calls the following functions is objects at the given time.
Function Parameter Description
Initialize When the object is completed (Con >= 100).
Construction object by_object When the object is created. The parameter is a pointer to the object the script of which has created this object. Also see Construction
Destruction When the object is removed.
Hit When the object collides with the landscape or is collected at high velocity (>=15).
Hit2 Like Hit, with speeds >= 20 (see OCF_HitSpeed2).
Hit3 Like Hit, with speeds >= 60 (see OCF_HitSpeed3).
Grab object target, bool grab When the object grabs or lets go of another object.
Grabbed object by_object, bool grab When the object is grabbed or let go by another object.
Get object target When the object takes another object from a container.
Put When the object puts another object into a container.
Damage int change, int cause, int by_player When the object is damaged. See Fx*Damagefor cause values.
DeepBreath When a living being surfaces after having used up more than half of its breath.
Incineration int by_player When the object is incinerated. Notice: with objects changing their definition via BurnTo, this call is made to the burned version!
IncinerationEx int by_player When the object is incinerated in and immediately extinguished by a surrounding liquid. Otherwise as Incineration.
Death When a living being dies.
Activate object by_object Activation by double dig. Only applies to collected items or directly controlled crew objects. Called after internal handling of the double dig command has been completed (e.g. chopping of trees etc.)
Contact_ When the object collides with the landscape. See CNAT - Contact Attachment.
Control_ object by_object When the object is controlled from the outside. See Control Functions.
Contained_ object by_object When the object is controlled from the inside. See Control Functions.
ControlCommand string command, object target, int x, int y, object target2, int data, object command_object When the object has received a command to be independently executed. See Control functions.
ControlCommandFinished string command, object target, int x, int y, object target2, any Data When the object has completed a command or execution of a command has failed.
ControlTransfer object obj, int x, int y When an object (obj) using the internal pathfinding algorithm is trying to pass the transfer zone of this object on its way to point x/y. The transfer function can then help the object along by giving special script commands and returning true. Also see SetTransferZone().
UpdateTransferZone When an object is loaded from a savegame or network synchronization is performed. Objects with a transfer zone should reset the zone in this call. Also see SetTransferZone().
IsFulfilled Only in game goal objects. A return value true indicates that this goal is fulfilled.
ControlContents id target When a new inventory object is selected. See Control Functions.
Selection object container When the object is selected in an inventory change. If you are processing this event, the function should play its own selection sound.
CatchBlow int level, object by When the object is hit or punched by another object.
QueryCatchBlow object by Called before the object is hit or punched by another object. By returning true, QueryCatchBlow can reject physical blows.
LineBreak int cause When a line object is broken. cause: 0 by movement, 1 because of a missing or incomplete target object.
BuildNeedsMaterial id material_definition, int amount When the object is building another object and building material is required. The parameters are the type and amount of the first needed material. If this function returns true, no material message is displayed above the object.
AttachTargetLost When the object is in an ATTACH action and has lost its action target. At this time, the object's action has already been reset.
CrewSelection bool deselect, bool cursor_only When crew selection is changed. cursor_only specifies whether only that crew member has been selected which is also the cursor.
GetObject2Drop object for_collection_of_object Called to determine the least needed inventory object when a clonk tries to collect a new object and his inventory is full. The function should return the object to be dropped to gain space, or nil if none.
OnMenuSelection int index, object menu_object When an object menu entry is selected.
CalcValue object in_base, int for_player Calculates the value of an object. Also see GetValue().
CalcDefValue object in_base, int for_player Calculates the value of an object type available to buy. Also see GetValue().
CalcBuyValue id item, int value Returns the buying price of the object type.
CalcSellValue object obj, int object_value Returns the selling price of the object type.
LiftTop When an object with LIFT action lifts its action target to the height specified in its DefCore or above.
Stuck When the action target of the object's PUSH or LIFT action is stuck.
GrabLost When the action target of the object's PUSH or PULL action is lost.
Collection object obj, bool put When the object has collected another object (obj) (by ingame collection or grabbing and getting).
Collection2 object obj When the object has collected another object (obj) (in all cases, even in script controlled collection or entering).
ContentsDestruction object destroyed When an object contained in the object has been destroyed/removed. The object still exists when the callback is called, but will be destroyed afterwards.
Departure object container When this object has left another object (container).
Ejection object obj When another object (obj) has left the contents of this object (also see script command Exit).
Entrance object container When the object has entered another object (container).
ActivateEntrance object by_object When another object is trying to enter this object through the entrance.
RejectCollect id def, object obj Called before Collection. If RejectCollect returns true, the collection of the other object is prevented.
RejectEntrance object into_object Called before Entrance. If RejectEntrance returns true, then entrance of the other object is prevented.
InitializePlayer int player Called in game goals, rules, or environment objects after the joining of a new player and before the corresponding call in the scenario script.
SellTo int by_player When the object is sold. Should return nil or the id of the object type which is actually added to the player's homebase material.
Sale int by_player When the object is sold.
Purchase int by_player, object buy_object When the object is bought.
Recruitment int player When the objet is added to the crew of a player.
RejectTeamSwitch int player, int new_team Callback in game goal, rule, and environment objects and in the scenario script. If RejectTeamSwitch returns true, the team switch of a player can be prevented (see SetPlayerTeam).
OnTeamSwitch int player, int new_team, int old_team Callback in game goal, rule, and environment objects and in the scenario script. Called when a player has successfully switch from old_team to new_team (see SetPlayerTeam).
sulai, 2003-11
Sven2, 2004-02
matthes, 2004-07
Clonkonaut, 2008-04