A to Z Scripting: Scripts that use input variables

From The DarkMod Wiki
Jump to navigationJump to search

Taken out of series as redundant.

A good way to avoid writing multiple very similar scripts is to create variables inside the script's input brackets. This allows other scripts to call this script and pass it values for these variables as instructions. For example, you could write a script to damage various ai's by various amounts, with an entity and a float input. Other scripts can call that script and pass it the name of an AI to be damaged and a number for how much. This is basically how utility scripts work, you can read more about their possibilities in A to Z Scripting: Utility scripts.

Another frequent scenario is that an entity calls a script and passes its own name and the name of the entity that triggered it. As a mapper you can place custom spawnargs on these entities whose values should be used in the script. This way multiple entities can call the same script with results that are unique to each entity (i.e. a script to make the calling entity disappear). This has been discussed in A to Z Scripting: Ways of calling a script.

Scripts that receive instructions from other scripts

Script to apply a variable amount of damage to an ai

void damage_guard(float damage_amount, ai guard)
	float health_current	= guard.getHealth();
	float health_new	= health_current – damage_amount; 


Other scripts can call the above script. In this example, the scripts below were made solely to call the above script and give it instructions, though you may also call from a longer script i.e. a cutscene.

void hurt_guard1_severe()
	thread damage_guard($guard1, 50);		//reduce guard1's health by 50

void hurt_guard2_light()
	thread damage_guard($guard2, 20);		//reduce guard2's health by 20

(Note: the more correct way to damage an AI or the player is by applying a damageDef. These cause the entity on the receiving end to emit appropriate sounds, get blasted away etc.)

Scripts that receive instructions from the entities that called them

Example: multiple func_movers and buttons, movement in multiple directions

This is a script for a room full of movers and buttons. Each button targets a unique callscriptfunction entity. that make the movers move in various directions

This is the main script that does the heavy lifting in applying the movements. It accepts input variables for a mover entity ("box"), movement duration ("move_time") and the movement vector ("move_vector").

void move_mover(entity box, float move_time, vector move_vector)

Main script that performs the movements.
Gets called by and receives input parameters from move_forward() or move_backward().
Checks if the box is stationary.
If the box is stationary, calculates new target position, sets time taken to move and then initiates movement.

	if(!box.isMoving())		//check if box has stopped moving
		//define new target position based on current position + input move_vector
		vector target_position	= box.getOrigin() + move_vector;

		box.time(move_time);		//set time taken to move
		box.moveToPos(target_position);	//initiate movement to the target position


These are the smaller scripts that pass instructions to the main script. Each one is called from a separate button in the map.

void move_forward_box1()		//called by button1 in the map
	thread move_mover($box1, 2, '50 0 0');		//box1, 2 seconds move_time, 50 units along the x axis

void move_backward_box1()		//called by button2 in the map
	thread move_mover($box1, 3, '-50 0 0');

void move_forward_box2()		//called by button3 in the map
	thread move_mover($box2, 2, '50 0 0');

void move_backward_box2()		//called by button4 in the map
	thread move_mover($box2, 3, '-50 0 0');

Next / previous article