Game Development Community

Is it possible to execute arbitrary string?

by pin (#0001) · in Technical Issues · 05/01/2005 (1:00 am) · 5 replies

Hello,

Is it possible somehow in TorqueScript to execute an arbitrary string?

E.g eval('myFunction()');

This way you can concatenate/edit any string in runtime to have a very dynamic program, modyfying itself.

This is a powerful feature I'm used to in other scripting languages (e.g Macromedia Director Lingo and PHP)

For a PHP example, look here
http://se.php.net/manual/sv/function.eval.php

Is it possible, and if yes, how?

Kind regards,
Thomas

#1
05/01/2005 (1:05 am)
Yeah the eval statement does just that it executes an arbitrary string, concatenation is done via the @ operator.

For an example look at this code...
function LoadItems(){
	//This code creates a datablock dynamically from an array and a couple of vars
	//Almost functional, just need to add query result and reconfigure loop a little.
	%query = "SELECT * FROM Items";
	%result = $SqLite.query(%query,0);
	$SQLite.dump();
	if(%result){
		while(!$SqLite.endOfResult(%result)){
			%BaseType = $SqLite.getColumn(%result,1);
			%Name = $SqLite.getColumn(%result,2);
			%dbstring ="datablock "@%BaseType@"("@%Name@"){";
			for(%x = 2; %x <= $SqLite.numColumns(%result) - 1; %x++){
				%varName = $SqLite.getColumnName(%result, %x);
				if(%varName !$= "Dynamics"){
					if($SqLite.getColumn(%result,%varName) !$="" ){
						%dbstring = %dbstring@ "\n"@ %varName@" = \""@$SqLite.getColumn(%result,%varName)@"\";";
					}
				}else{
					%Dynamics = $SqLite.getColumn(%result,%varName);
					//echo("To see this means that Dynamics is an invalid Column");
					%i = 0;
					while (getWord(%Dynamics, %i) !$= ""){
						%varName = getWord(%Dynamics, %i);
						%varValue = getWord(%Dynamics, %i++);
						%dbstring = %dbstring @ "\n"@ %varName @ " = \"" @ %varValue @ "\";";
						%i ++;
					}
				}
				
			}
		%dbstring = %dbstring@"\n};";
		echo("Pulling new datablock from db");
		echo(%dbstring);
		eval(%dbstring);
		$SqLite.nextRow(%result);
		}
		$SqLite.clearResult(%result);
	}else{
		
	}
}

The above creates a datablock string by pulling the relevant key/value pairs from a database and concatenating them into a string call %dbstring and then we eval the string right near the end, to load the datablock into memory.
#2
05/01/2005 (1:55 am)
Hi Dreamer, thanks, I'm new to TS so please bear with me :)

I tried a very simple thing in the console now, e.g

%astring = "echo(test);";

eval(%astring);

I don't get any error but also no output in the console.

What am I missing?

Thanks,
Thomas
#3
05/01/2005 (2:17 am)
Erm, I might be wrong, but a local variable in the console won't go anywhere, use global vars instead.
#4
05/01/2005 (2:26 am)
Hes right... try

$astring = "echo(test);";

the scope of a console command is within that command, so the local variable would be "destroyed" at the end of the command
#5
05/01/2005 (2:32 am)
Thanks Matthew, correct, now it works.
/Thomas