A to Z Scripting: Getting map information: Difference between revisions

From The DarkMod Wiki
Jump to navigationJump to search
Dragofer (talk | contribs)
No edit summary
Dragofer (talk | contribs)
mNo edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Getting map information ==
Your scripts unlock much potential if they can see and react to what's happening in the map. The way to get up-to-date information about the state and properties of entities is with "get" script events. There are a lot of them, and as said they have a ton of potential.
Your scripts unlock much potential if they can see and react to what's happening in the map. The way to get up-to-date information about the state and properties of entities is with "get" script events. There are a lot of them, and as said they have a ton of potential.


Line 13: Line 11:




=== Getting spawnargs ===
== Getting spawnargs ==


Spawnargs are a special case. All spawnargs are strings, so if you want to store them as a variable you need to choose a "get" event which converts them into the matching data type:
Spawnargs are a special case. All spawnargs are strings, so if you want to store them as a variable you need to choose a "get" event which converts them into the matching data type:
Line 22: Line 20:
  boolean open = $mover.getBoolKey("open"); //for booleans
  boolean open = $mover.getBoolKey("open"); //for booleans
  entity frob_master = $mover.getEntityKey("frob_master"); //for entities
  entity frob_master = $mover.getEntityKey("frob_master"); //for entities


A possible question might be, why store something in a variable if it's already stored in a spawnarg? In most cases this might be a matter of preference. These points can be seen as advantages:
A possible question might be, why store something in a variable if it's already stored in a spawnarg? In most cases this might be a matter of preference. These points can be seen as advantages:
Line 31: Line 30:




=== Example: checking an AI's health for a "No harm" objective ===
== Setting spawnargs ==
A handy way of storing something without creating new variables is to set custom spawnargs on entities. This can be especially useful if you want different entities to have different values for the same "variable". Simply use setKey() and always give it 2 strings as input. Example:
 
$func_static_1.setKey("teleport_destination", "40 40 948");
 
 
== Example: checking an AI's health for a "No harm" objective ==


This script is for an objective to let no harm come to an AI. It would check the AI's "health" spawnarg to find what his max health is, then compare that with the AI's current health. If there's a difference, the objective fails.
This script is for an objective to let no harm come to an AI. It would check the AI's "health" spawnarg to find what his max health is, then compare that with the AI's current health. If there's a difference, the objective fails.
Line 52: Line 57:
*Previous article: [[A to Z Scripting: Ways of calling a script]]
*Previous article: [[A to Z Scripting: Ways of calling a script]]
*Table of contents: [[A to Z Scripting]]
*Table of contents: [[A to Z Scripting]]
[[Category:Scripting]]

Latest revision as of 22:49, 26 December 2020

Your scripts unlock much potential if they can see and react to what's happening in the map. The way to get up-to-date information about the state and properties of entities is with "get" script events. There are a lot of them, and as said they have a ton of potential.


Often you'll store their results as a variable, like this:

float health_current = $lord_marlow.getHealth();	//Lord Marlow's current health

Or use them directly in a conditional:

if ($lord_marlow.getHealth() < 100)			//If Lord Marlow has been hurt...


Getting spawnargs

Spawnargs are a special case. All spawnargs are strings, so if you want to store them as a variable you need to choose a "get" event which converts them into the matching data type:

string name		= $mover.getKey("name");		//for strings
float move_time		= $mover.getFloatKey("move_time");	//for floats
vector rotate		= $mover.getVectorKey("rotate");	//for vectors
boolean open		= $mover.getBoolKey("open");		//for booleans
entity frob_master	= $mover.getEntityKey("frob_master");	//for entities


A possible question might be, why store something in a variable if it's already stored in a spawnarg? In most cases this might be a matter of preference. These points can be seen as advantages:

  • variable names are shorter than get() events for fetching the spawnarg, i.e. max_health vs $lord_marlow.getFloatKey("health").
  • storing in a variable allows you to spread out any calculations over several lines
  • no need to put get() events into the input brackets of other events, i.e. $lord_marlow.setHealth($lord_marlow.getFloatKey("health")) to fully restore his health to max.

The downside is that they make your script longer. As you get more confident in scripting you might begin to lean more towards fetching spawnargs when and where you need them in order to get more compact scripts.


Setting spawnargs

A handy way of storing something without creating new variables is to set custom spawnargs on entities. This can be especially useful if you want different entities to have different values for the same "variable". Simply use setKey() and always give it 2 strings as input. Example:

$func_static_1.setKey("teleport_destination", "40 40 948");


Example: checking an AI's health for a "No harm" objective

This script is for an objective to let no harm come to an AI. It would check the AI's "health" spawnarg to find what his max health is, then compare that with the AI's current health. If there's a difference, the objective fails.

void check_health()
{
	float health_max	= $lord_marlow.getFloatKey("health"); 			//find max health by checking the spawnarg
	float health_current	= $lord_marlow.getHealth();				//get current health

	if(health_current < health_max) $player1.setObjectiveState(5, OBJ_FAILED);	//if health is no longer max, set objective 5 to failed

	sys.wait(0.5);			//wait 0.5s...
	thread check_health();		//... before repeating this whole script
}


Next / previous article