9.8.4 evaluate sting - eval( string )

The evaluation function lets you write expressions as string that can be evaluated during the execution of the script. In the example we use a string that reads "x = tempval * sin(time*100)". Note that this string contains a usual expression. The difference to a normal expression is, that we define this expression to be a string:

string a = "x = tempval * sin(time*100)"; // defining string variable "a"

From now on we can use "a" as a usual string variable, or we can evaluate, what the string says. The function

eval (a); // evaluate string "a"

will look at the string and try to execute it. In this case it calculates a value for x with a temporary variable "tempval". Both x and tempval must be defined as variable

double x; // defining the varibale "x" as a floating point value
double tempval; // defining "tempval" as a float point value

During runtime we can give tempval different values to calculate different x values while evaluating the string.

// Evaluate "a" for Cubes X-Position

tempval = 100; // defining "tempval" to have a value of 100

eval( a ); // evaluate string "a"

Cube.Position.X = x;

// Evaluate "a" with a different temporary value for Cubes Y-Position

tempval = -100;

eval( a );

Cube.Position.Y = x;

get demo project evaluate.prj

 

Xpressionist 3.5