A to Z Scripting: Getting map information: Difference between revisions
Created page with "== 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 sta..." |
No edit summary |
||
Line 22: | Line 22: | ||
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: | |||
* 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. | |||
Line 33: | Line 40: | ||
float health_current = $lord_marlow.getHealth(); //get current health | float health_current = $lord_marlow.getHealth(); //get current health | ||
if(health_current | 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... | sys.wait(0.5); //wait 0.5s... |
Revision as of 11:24, 23 December 2020
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.
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.
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
- Next article: A to Z Scripting: Practical exercise: subtle teleportation
- Previous article: A to Z Scripting: Ways of calling a script
- Table of contents: A to Z Scripting