Scripts and expressions.
ZGameEditor has a very simple expression parser. The general syntax is based upon "C"-dialects.
The following syntax is supported.
Comments:
//this is a single line comment
/*
this is a
multi line
comment
*/
Assignments:
[objectname].[propertyname].[propertyindex] = value ;
objectname - The name of a component is the value of its "Name"-property and is set in the editor. The special objectname "CurrentModel" can be used if the expression written has a Model-component as a parent.
propertyname - The name of the property to assign.
propertyindex - If the property is an indexed property, then the name of the index must be specified.
For Color-values, this is "R","G","B","A" for the Red/Green/Blue/Alpha components of the color in the range 0 to 1.
For position or vertex-values (such as Model.Position/Rotation/Velocity) the propertyindex is "X","Y","Z" for the value of its corresponding axis.
Example assignments:
PlayerModel.Position.X = 42;
PlayerModel.Position.X = PlayerModel.Position.Y / 2;
MyMaterial.Color.R = 0.5;
PlayerScore += 500;
Other C-style assignments are supported also:
- = - normal assignment
- += - plus-assign: x+=1 is the same as x=x+1
- -= - minus-assign: x-=1 is the same as x=x-1
- *= - multiply-assign: x*=1 is the same as x=x*1
- /= - divide-assign: x/=1 is the same as x=x/1
When assigning a property with several components (such as the x,y and z of Scale or Rotation) to the same value a shorter syntax is allowed.The following assigment:
CurrentModel.Scale=1;
Give the same result as:
CurrentModel.Scale.X=1;
CurrentModel.Scale.Y=1;
CurrentModel.Scale.Z=1;
IF-statements:
if( [condition] )
[one line expression] ;
if( [condition] ) {
//do something if condition is true
} else {
//do something else if condition is false
}
condition - An expression which is evaluated to true or false.
Operators that are valid in conditions:
- == - equal
- != - not equal
- > - greater than
- >= - greater than or equal
- < - less than
- <= - less than or equal
- || - OR, statement is true if any of the conditions are true
- && - AND, statement is true if both conditions are true
Example IF-statements:
if(PlayerScore>5000)
PlayerLives += 1;
if( (App.Time>5) && (PlayerLives<2) ) {
Difficulty *= 1.2;
Speed *= 1.2;
}
Built in functions:
- sin(x) - returns the sine of the angle x in radians
- sqrt(x) - square root of x
- cos(x) - cosine of the angle x in radians.
- tan(x) - tangent of x
- abs(x) - absolute (positive) value of x
- rnd() - returns a random number in the range 0 to 1.
- random(base,variance) - random number in the range base-variance to base+variance
- atan2(y,x) - arctangent angle and quadrant of a given number
- noise2(x,y)
- noise3(x,y,z) - 2 and 3 dimensional Perlin Noise. Return values in an approximate range of -0.3 to 0.3 so you need to scale it to get a -1 to 1 range. More info: Hugo Elias article about noise.
- frac(x) - fractional (decimal) part of x
- exp(x) - the exponential of x
- clamp(x,min,max) - clamps the value x to be in the range min to max
- pow(base,exponent) - raises base to any power
- centerMouse() - centers the mouse cursor
- setRandomSeed(x) - set the random seed value to x, returns the old seed.
- floor(x) - returns the floor of x (the highest integer less than or equal to x)
- ceil(x) - returns the ceiling of x (the smallest integer larger than x)
- acos(x) - returns the inverse cosine of x
- asin(x) - returns the inverse sine of x
Predefined constants: