Using user variables in ZScript

From ZDoom Wiki
Jump to navigation Jump to search
Note: This feature is for ZScript only.
Note: User variables in DECORATE DO NOT appear in the editor when defined, to make a configurable DECORATE actor, you need to use custom args.


Guide written by inkoalawetrust

User variables in ZScript are normal variables, but unlike standard variables, user variables can be changed from a level editor like UDB or SLADE, allowing mappers to change the behaviour of actors without needing to ever touch the source code of the actor, or having to make multiple inheriting subclasses with minor differences between each other.

The example that will be used throughout this guide is a customizable Zombieman variant.

Before starting

Keep in mind that this guide will not cover how to use user variables in code, as they are identical to normal variables in usage, the guide only covers user variable-specific knowledge. Such as how to define them and give them default values.

Creating a user variable

The process of creating a user variable in ZScript is the same as creating normal variables. To make a user variable, you simply have to give it the User_ prefix like shown below:

Int BulletAmount; //Normal variable.
Int User_BulletAmount; //User variable.

Supported user variable types

  • Integer (Int)
  • Floating point (Float)
  • Double-precision floating point (Double)
  • Boolean (Bool)
  • String (String)

You can find more about all the supported ZScript variable types here.

Note: Floats are the same as Doubles, and both show up as decimal types in the editor.

Setting a default value to a user variable

To give a user variable a default value, in case none is set in the editor. You can set a default value to it in your actors' BeginPlay() or PostBeginPlay() virtuals. The values given inside of the virtuals can be overwritten by the editor. Below is an example of how to do this:

	Override Void PostBeginPlay()
	{
		Super.PostBeginPlay();
		If (User_Firerate <= 0) {User_FireRate = 5;}
		If (User_BulletAmount <= 0) {BulletAmount = 5;}
	}

Using user variables in the editor

So, you have made your mega awesome actor that has user variables to be able to modify its behaviour, but where are the user variables even at ? Well, the user variables of an actor, if they have any, can be found in the "Custom" tab of the actors' "Edit Thing" window.

The custom tab, with several user variables available.

Once you find them, you can change their values around to whatever you want, as long as it is the variables' data type, for example, you can't assign text/strings to an integer variable.

Removing/Resetting user variable values

To reset the value of a user variable back to nothing in the editor, you need to click on top of the variables' name or stated type, and press the Delete key, NOT backspace.

If you are not sure where that is, it's the area marked in red.

Tips

  • The contents of the Custom tab are updated only every time the Edit Thing window is opened again, so if you immediately place an actor with user variables in your map, or change the actor you've already placed to one which has some, and they don't appear in the Custom tab, just reopen the window and check again.
  • If you are using GZDoom Builder Bugfix, Boolean user variables will appear as integers due to a bug, you can still change them, to set it to true, just add any numerical value above 0, to set it back to false just change the value back to 0. This is not an issue in Ultimate Doom Builder, which has already fixed this bug.

Full example

Below is an example of a Zombieman-derived actor with new custom behaviour, that uses multiple user variables to be able to modify said behaviour on a per-actor basis. It uses most of the information covered on this guide, and can be used a reference on how to use user variables and what they can be used for.

Class CustomizableZombieman : Zombieman
{
	Default
	{
		//$Title Former Human (Customizable)
		//$Category Monsters
	}
	Bool User_EnableBurstFire; //If true, the zombie will shoot in bursts.
	Bool User_AvoidMelee; //Makes the zombie avoid melee, as he has no melee attack.
	Int User_BulletAmount; //How many bullets to fire at every burst.
	Int User_Firerate; //How fast each round is fired, in tics.
	Int BulletAmount; //Counts down how many bullets have been fired.
	Override Void PostBeginPlay()
	{
		Super.PostBeginPlay();
		If (User_Firerate <= 0) {User_FireRate = 5;}
		If (User_BulletAmount <= 0) {BulletAmount = 5;}
	}
	Override Void Tick()
	{
		Super.Tick();
		If (User_AvoidMelee) {bAvoidMelee = True;} //Turn the eponymous flag on.
		Else {bAvoidMelee = False;} //Or off if the user variable is false.
	}
	States
	{
		Missile:
			TNT1 A 0 A_JumpIf (User_EnableBurstFire,"CustomMissile");
			POSS E 10 A_FaceTarget;
			POSS F 8 A_PosAttack;
			POSS E 8;
			Goto See;
		CustomMissile:
			POSS E 10
			{
				Tics = User_Firerate; //Yes, you can directly change how many tics a frame state lasts, but A_SetTics works too.
				A_FaceTarget();
			}
			POSS F 8
			{
				Tics = User_Firerate;
				A_PosAttack();
			}
			POSS E 8 A_SetTics (User_Firerate);
			TNT1 A 0
			{
				//If the amount of rounds to fire is left to 0 or less, and the eponymous user variable is set to 1 or above.
				If (BulletAmount <= 0 && User_BulletAmount >= 1)
				{
					BulletAmount = User_BulletAmount; //Then reset the burst fire counter.
					Return ResolveState(Null); //And leave this state frame.
				}
				If (BulletAmount <= 0 && User_BulletAmount <= 0) //Default counter amount if no value is given.
				{
					BulletAmount = 5;
					Return ResolveState(Null);
				}
				//If BulletAmount is still above 0.
				BulletAmount--; //Decrement
				Return A_Jump (256,"CustomMissile"); //And jump back to the start of the state.
			}
			Goto See;
	}
}