A to Z Scripting: Basic maths: Difference between revisions

From The DarkMod Wiki
Jump to navigationJump to search
(Created page with "== Basic maths == === Basic maths symbols === You can use basic maths symbols, as well as brackets: {| class="wikitable" |- ! symbol !! effect !! can be used with |- | + || ad...")
 
(→‎Increasing/decreasing: Warn against prefix forms)
 
(21 intermediate revisions by one other user not shown)
Line 1: Line 1:
== Basic maths ==
== Basic maths symbols ==
=== Basic maths symbols ===
You can use basic maths symbols, as well as brackets (parentheses):
You can use basic maths symbols, as well as brackets:
{| class="wikitable"
{| class="wikitable"
|-
|-
Line 15: Line 14:
|}
|}


=== The = sign ===
== The = sign ==
An important difference is what the = sign does. In scripting, = means "set", while == means "equals".
An important difference is what the = sign does. In scripting, = means "set", while == means "equals".
  teleport_destination = '20 20 20'; //set teleport_destination to '20 20 20'
  teleport_destination = '20 20 20'; //set teleport_destination to '20 20 20'
Line 21: Line 20:
  if ( float1 == float2 ) //check if float1 is equal to float2
  if ( float1 == float2 ) //check if float1 is equal to float2


=== Combinations ===
== is used in conditionals to check whether a condition is true (2 values are equal to each other) before carrying out an associated set of instructions. That's described in detail in a later article, [[A to Z Scripting: Conditionals]].
You can freely combine script events, variables and numbers in one line like this:
 
teleport_destination = $player1.getOrigin() + teleportation_offset + '0 0 16';


Strings can be cobbled together from strings, floats and vectors with the + sign. This is quite useful for sending helpful messages to the console:
The = sign can be combined with a maths symbol. Both the lines below do the exact same thing:
  sys.println(entity1 + " is at the location " + position1);
float1 = float1 + float2; //set float1 to: current value + float2
  float1 += float2; //set float1 to: current value + float2




=== Increasing/decreasing ===
== Increasing/decreasing ==
Increasing (incrementing) a variable's value by 1 would work like this:
Increasing (incrementing) a variable's value by 1 would work like this:
  counter = counter + 1; //set "counter" to: the current value of "counter" + 1
  counter = counter + 1; //set "counter" to: the current value of "counter" + 1
counter += 1; //set "counter" to: the current value of "counter" + 1


An alternative method to increase a variable by 1 is:
An alternative method to increase or decrease a variable by 1 is:
  counter++; //increase "counter" by 1
  counter++; //increase "counter" by 1
  counter--; //decrease "counter" by 1
  counter--; //decrease "counter" by 1


Be aware that the alternative "prefix" forms of these - found in the C language - are not supported:
++counter; // not legal
--counter; // not legal
== Combining ==
You can freely combine script events, variables and absolute values in one line like this:
teleport_destination = $player1.getOrigin() + teleportation_offset + '0 0 16';
Strings can be cobbled together from strings, floats and vectors with the + sign. If you also want to include entities you need to turn them into strings with getName(). This is quite useful for sending helpful messages to the console while testing the script:
sys.println(entity1.getName() + " is at the location " + entity1.getOrigin()); //+ doesn't work with entities, so you need to use getName() to turn it into a string
What you can't do is perform maths operations with both floats and vectors. You'll need to i.e. use a script event to convert the vector into a float (see "Vectors").
== Integers ==
There's no data type for integers in DoomScript, but you can make an integer out of a float like this:
float integer1 = int(5.8); //returns 5
int() is a utility script, you can read more about them here: [[A to Z Scripting: Utility scripts]].


=== Vectors ===
You can access individual components of a vector variable by appending _x, _y or _z to the name, i.e. teleport_destination_z. These will be floats.


Vector multiplication requires special script events: DotProduct() and CrossProduct().
== Vectors ==
You can add or subtract vectors from each other. Things like multiplication or turning a vector into a distance require script events such as DotProduct(), CrossProduct() and vecLength().


More on vectors and what you can do with them in [[A to Z Scripting: Special methods]].
You can access & change individual components of a vector variable by appending _x, _y or _z to the name, i.e. teleport_destination_z. These will be floats.  


More detail on vector maths and what you can do with it in [[A to Z Scripting: Special methods]].


== Next / previous article ==
== Next / previous article ==
Line 51: Line 72:
*Previous article: [[A to Z Scripting: More scripting basics]]
*Previous article: [[A to Z Scripting: More scripting basics]]
*Table of contents: [[A to Z Scripting]]
*Table of contents: [[A to Z Scripting]]
[[Category:Scripting]]

Latest revision as of 19:35, 1 February 2023

Basic maths symbols

You can use basic maths symbols, as well as brackets (parentheses):

symbol effect can be used with
+ add/combine floats, vectors, strings
- subtract floats, vectors
* multiply floats
/ divide floats

The = sign

An important difference is what the = sign does. In scripting, = means "set", while == means "equals".

teleport_destination	= '20 20 20';		//set teleport_destination to '20 20 20'
float1 			= float2 + float3;	//set float1 to the sum of float2 and float3
if ( float1 == float2 )				//check if float1 is equal to float2

== is used in conditionals to check whether a condition is true (2 values are equal to each other) before carrying out an associated set of instructions. That's described in detail in a later article, A to Z Scripting: Conditionals.


The = sign can be combined with a maths symbol. Both the lines below do the exact same thing:

float1	= float1 + float2;	//set float1 to: current value + float2
float1	+= float2;		//set float1 to: current value + float2


Increasing/decreasing

Increasing (incrementing) a variable's value by 1 would work like this:

counter = counter + 1;		//set "counter" to: the current value of "counter" + 1
counter += 1;			//set "counter" to: the current value of "counter" + 1

An alternative method to increase or decrease a variable by 1 is:

counter++;			//increase "counter" by 1
counter--;			//decrease "counter" by 1

Be aware that the alternative "prefix" forms of these - found in the C language - are not supported:

++counter;			// not legal
--counter;			// not legal

Combining

You can freely combine script events, variables and absolute values in one line like this:

teleport_destination = $player1.getOrigin() + teleportation_offset + '0 0 16';


Strings can be cobbled together from strings, floats and vectors with the + sign. If you also want to include entities you need to turn them into strings with getName(). This is quite useful for sending helpful messages to the console while testing the script:

sys.println(entity1.getName() + " is at the location " + entity1.getOrigin());	//+ doesn't work with entities, so you need to use getName() to turn it into a string

What you can't do is perform maths operations with both floats and vectors. You'll need to i.e. use a script event to convert the vector into a float (see "Vectors").


Integers

There's no data type for integers in DoomScript, but you can make an integer out of a float like this:

float integer1 = int(5.8);	//returns 5

int() is a utility script, you can read more about them here: A to Z Scripting: Utility scripts.


Vectors

You can add or subtract vectors from each other. Things like multiplication or turning a vector into a distance require script events such as DotProduct(), CrossProduct() and vecLength().

You can access & change individual components of a vector variable by appending _x, _y or _z to the name, i.e. teleport_destination_z. These will be floats.


More detail on vector maths and what you can do with it in A to Z Scripting: Special methods.

Next / previous article