Constants

From ZDoom Wiki
Jump to: navigation, search

Constants are values that are always the same when executed. An example of a constant would be 1, "Hello", etc. Constants can also be #define'd.

The syntax of defining a constant is as follows

#define NAME VALUE

Numbers

These constants can be used as values and also as script numbers.

For example:

#define SNUM	1
#define VAL	3131

script SNUM (void)
{
    int x = VAL;
}

Here SNUM would be replaced with 1 and VAL would be replaced with 3131.

This is useful for a constant that is used a lot. If something like the spawn numbers were to be changed, you'd only need to change the constant for the defined spawn numbers.

Use of operators

Constants can be used to define other constants.

For example:

#define WEAPON_FIST	0
#define WEAPON_CHAINSAW	WEAPON_FIST + 1

script "Test" ENTER
{
    Log(i:WEAPON_FIST);
    Log(i:WEAPON_CHAINSAW);
}

This logs the values 0 and 1 to the console.

You can also make more complex operations.

#define HUD_WIDTH      1024
#define HUD_HEIGHT     768
#define HUD_FWIDTH     HUD_WIDTH * 1.0
#define HUD_FCENTERX   HUD_FWIDTH / 2
#define HUD_FHEIGHT    HUD_HEIGHT * 1.0
#define HUD_FCENTERY   HUD_FHEIGHT / 2

script "Test2" ENTER
{
    Log(f:HUD_FWIDTH);
    Log(i:HUD_HEIGHT);
}

This logs the values 1024.0 and 768 to the console.

String Constants

You can define string constants as well.

First example:

script 1 OPEN
{
    print(s:"err");
    print(s:"err");
}

Second example:

#define STR_err "err"

script 1 OPEN
{
    print(s:STR_ERR);
    print(s:STR_ERR);
}

Both examples produce identical output.

Library Constants

You can also place constants in your Libraries. This can be especially useful when, say, applying a TID to the player, or numbers you may want handy for custom functions.

The syntax is much the same as a normal constant, with one change.

#libdefine NAME VALUE

Here is an example of some constants I personally use. These constants provide quick shortcuts to cardinal directions for PolyObjects and SetActorAngle.

#libdefine POLY_NORTH       64
#libdefine POLY_SOUTH       192
#libdefine POLY_WEST        128
#libdefine POLY_EAST        0

#libdefine ACTOR_FACE_NORTH 0.25
#libdefine ACTOR_FACE_WEST  0.5
#libdefine ACTOR_FACE_EAST  1.0
#libdefine ACTOR_FACE_SOUTH 0.75

DECORATE usage

Actors are also capable of storing individual constant integers. Unlike user variables, these cannot be changed.

These may be used in parameters of almost any kind, except for strings or actor names. Constants may also be declared outside of an actor for global usage.

const int MyGlobalInt = 2;
Actor TestDummy
{
	const int MyConst = 1;
	const int FLAGS = CLOFF_JUMPOBJECT|CLOFF_SKIPTARGET; //Saves on space for defining flags!
	const float Floaty = 1.234;
}

Enums can also be used for integer values only. All others (i.e. floats) must be defined as above outside an enum. Assigning a number to specific values means they will always be that number, rather than being auto-assigned numbers. This may be vital to ensuring compatibility with saved games or demos, since adding anything to the enum list may cause unintended side effects or prevent proper function. A semicolon is required on the closing bracket of an enum. Like constants, they can be inside of an actor.

Examples:

Actor Test
{
	enum //Non-initialized numbers
	{
	 	StateNum_Punch, //Autoassigned 0
		StateNum_Kick, //Autoassigned 1
	};
	//...States and stuff go here.
}
enum //Initialized numbers
{
	Mons_MaxCount = 128, 
	Mons_MinCount = 4,
};

Notes

DECORATE:

  • Constants can only be used with decorate functions.
  • Constants cannot be used for defining actor parameters. For example Health must have an actual number, not a constant.
  • Inheritance from parents only overwrites constants if the state itself is overwritten. It is recommended to have a state dedicated to the setup of user variables instead.