ZGameEditor Documentation
Community |
Main /
WritingExpressionsNote: Scripting has been significantly enhanced with features not documented below, see these link for what's been added: - http://www.emix8.org/forum/viewtopic.php?t=704 and - http://www.emix8.org/forum/viewtopic.php?t=868. Scripts and expressions. ZGameEditor has a simple scripting language. 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 assignmentsPlayerModel.Position.X = 42; PlayerScore += 500; Other C-style assignments are supported also:
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 assignment: IF-statementsif( [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:
Example IF-statements: if(PlayerScore>5000) PlayerLives += 1; if( (App.Time>5) && (PlayerLives<2) ) { Difficulty *= 1.2; Speed *= 1.2; } LoopsFor-loopsC-style for-loops can be used: for ( (defVar optional) <EXPR1> ; <EXPR2> ; <EXPR3> ) { //do something; } The expression <EXPR1> is evaluated, then <EXPR2> is checked, if it's true, then the loop body is executed. After the first iteration, the <EXPR3> is evaluated, <EXPR2> is checked again, if it's true, the loop body is executed, etc... When you want to use a global variable that is already defined by the DefineVariable Component, you can simply write .. for(i = 0; i<4; ++i){expression;} In case you want to use a local variable (only exists within the scope that it is defined in), initializing the variable as you need it is more convenient for(int i = 0; i<4; ++i){expression;} Change "int" into "float" when you want to use a floating point variables. While-loopswhile(i<42) { //do something; i++; } While statements can also be defined using the Repeat component. Break and continueUse break to break out of a loop before it has finished execution: while(i<42) { if(i==10) break; //exit loop i++; } Use continue to jump directly to the next loop iteration: for(int i=0; i<10; i++) { if(i>=5) continue; //keep looping and skip the rest of the loop body sum+=i; //this line only executes when i<5 } Switch statement//Switch-statement example switch(i) { case 0 : case 1 : case 2 : //When i is 0, 1 or 2. doSomething(); break; case 3 : //When i is 3. doSomethingElse(); break; default : //The default case when i is none of the above } Conditional statement//Conditional statement example //Give 1000 in bonus when time<100, otherwise bonus will be zero int bonus= time<100 ? 1000 : 0; Bit manipulation operators
Built in functions
User defined functionsDefine functions using the ZLibrary component. Call external modules (dll:s) using the ZExternalLibrary component Predefined constants
VariablesGlobal variables can be defined with the DefineVariable component. Arrays can be defined with the DefineArray component. Local variables can be defined with the following syntax:
Note that local variables and arrays are not initialized, you need to assign a value before using them: //S is uninitialized, this will generate a runtime error string s; s=s + "i"; //Ok initialize S before use string s=""; s=s + "i"; StringsThe scripting language also handles strings. Example - RenderText component named ScoreJimmy There are a number of built-in functions for strings:
Special characters in string literals
Advanced scripting
Syntax: @ <component> ( [ <property> : <value> ], ... ); This will declare and execute a component with the properties set to the desired values. Examples: //A component that does not need any properties @RemoveModel(); //Component with a single property @SetAppState(State : AppState_TitleScreen); //Several properties @RenderText(Text : "Hello" + intToStr(i), X : -0.5, Y : -1 + (0.1*i)); It makes much more functionality available from scripting so it's up to the user to choose between Component, code-based, or a combination approach, based on personal preference.
Previously you could only use expressions such as "[component].[property]". Now you can use more advanced expressions, so for instance this is possible: (Um1 is a UseMaterial component name) //Set the red component of the material Um1.Material.Color.R=1; Notice that "Material" is a property referring to another component. You can also assign component references to another component: //Use different materials if(something) Um1.Material=Material1; else Um1.Material=Material2;
Use the "ref" syntax. "Ref" stands for "reference". This allows you to return more than one value from a function. Examples: //notice the "ref" word in front of the arguments that are passed by reference void splitColor(int color,ref int r, ref int g, ref int b) { //..separate r,g,b from color and return back to caller } //call this function int r,g,b; splitColor(0xff8020,r,g,b); This also allows to call dll-functions that expect a pointer to an integer as an argument. //OpenGL-function, generate a texture id and return it into the second argument void glGenTextures(int n, ref int textureid) { } //Delete a texture, pass the id using a reference void glDeleteTextures(int n, ref int textureid) { }
Example: //OpenGL external library definitions, notice the last parameter "xptr pixels" void glTexImage2D(int target, int level, int internalformat, int width, int height, int border, int format, int atype, xptr pixels) {} void glGetTexImage(int target, int level, int format, int atype, xptr pixels) {} These functions can now be called with a DefineArray (only, so far) component instance as the last parameter. |