Using eval
by gamer · in Torque Game Engine · 02/20/2007 (5:38 pm) · 8 replies
According to the documentation of eval, it Returns a string containing the results from the function that is built and called.
but when I tried this:
$b=new Array();
$a=eval("$b.count();");
echo($a);
prints nothing; //expected: 0
echo(eval("$b.count();"));
prints echo(eval("$b.count();")); //expected:0
why?
thanks
but when I tried this:
$b=new Array();
$a=eval("$b.count();");
echo($a);
prints nothing; //expected: 0
echo(eval("$b.count();"));
prints echo(eval("$b.count();")); //expected:0
why?
thanks
About the author
#2
eval( "$a = $b.count();" );
02/21/2007 (10:04 am)
I always put the assignment into the eval, as in:eval( "$a = $b.count();" );
#3
eval("%tmp=$b.count();");
thanks!
02/21/2007 (10:04 am)
Sweet, I also figured out an alternative:eval("%tmp=$b.count();");
thanks!
#4
%tmp = $b.count();
The exact same thing?
02/21/2007 (3:30 pm)
Am I missing somthing here or isn't %tmp = $b.count();
The exact same thing?
#5
@gamer - If this isn't a hypothetical, definately do what Gonzo says... it's much faster.
02/21/2007 (3:38 pm)
@Gonzo - It is... I assume he was talking hypothetically. I will sometimes name a couple of lists Card0Pile, Card1Pile, etc., and then I use eval to get counts, as in eval( "%count = Card" @ %i @ "Pile.getCount();");@gamer - If this isn't a hypothetical, definately do what Gonzo says... it's much faster.
#6
=
%count = Card[%i]Pile.getCount();
Should work just fine I believe
02/21/2007 (4:10 pm)
Eval( "%count = Card" @ %i @ "Pile.getCount();");=
%count = Card[%i]Pile.getCount();
Should work just fine I believe
#7
eval("$b.count()") will give you nothing but eval("%tmp=$b.count()"); gives you the right number.
02/21/2007 (4:18 pm)
Yes I was talking hypothetically to show how eval works.eval("$b.count()") will give you nothing but eval("%tmp=$b.count()"); gives you the right number.
#8
Using a function similar to this, I could send remote commands to a server st passing a few key parameters. Instead of having some complicated if/else or case/switch statements do the work, which would also need to be added to if ever expanded, I use the above to assemble my functions for me and I just send it preformatted data. For example, if I sent the following data...
%what = "Admin";
%who = "Player";
%how = "Ban";
%arg = "Frank";
The resulting eval would be the same as...
which would call the following function...
Maybe a ban was too rough, if I instead sent...
%what = "Admin";
%who = "Player";
%how = "Kick";
%arg = "Frank";
The resulting eval would be the same as...
which would call the following function...
You could also use something like...
%what = "Admin";
%who = "Server";
%how = "MissionChange";
%arg = "server/missions/myMission.mis";
resulting in...
which would call the following function...
The possibilities are endless. You could pass more arguements to more complex functions if you needed to, but the above can work a lot of things without needing to expand other code to activate them.
EDIT: One more example to demonstrate it's flexibility...
%what = "Game";
%who = "Reset";
%how = "";
%arg = "30";
leaving out the "how" simply results in a shorter function...
but the arguement still gets passed which would call the following function...
02/21/2007 (4:48 pm)
Most of the times I see people use eval, it is really not neccesary. Not neccesary means wasting time, yours and Torque's. For an idea of a good use of evals capabilities I rigged up these three functions to demonstrate how I made a very compact admin feature for servers that didn't require writing a ton of code. Now if you have ever seen admin code, you know it can take a lot of functions to make it possible to ban players or change missions. Using eval can solve a lot of hassles in this situation.function funkshunBuilder(%what, %who, %how, %arg)
{
eval(%what @ %who @ %how @ "(\"" @ %arg @ "\");");
}Using a function similar to this, I could send remote commands to a server st passing a few key parameters. Instead of having some complicated if/else or case/switch statements do the work, which would also need to be added to if ever expanded, I use the above to assemble my functions for me and I just send it preformatted data. For example, if I sent the following data...
%what = "Admin";
%who = "Player";
%how = "Ban";
%arg = "Frank";
The resulting eval would be the same as...
AdminPlayerBan("Frank");which would call the following function...
function AdminPlayerBan(%player)
{
ban(%player);
}Maybe a ban was too rough, if I instead sent...
%what = "Admin";
%who = "Player";
%how = "Kick";
%arg = "Frank";
The resulting eval would be the same as...
AdminPlayerKick("Frank");which would call the following function...
function AdminPlayerKick(%player)
{
kick(%player);
}You could also use something like...
%what = "Admin";
%who = "Server";
%how = "MissionChange";
%arg = "server/missions/myMission.mis";
resulting in...
AdminServerMissionChange("server/missions/myMission.mis");which would call the following function...
function AdminServerMissionChange(%mission)
{
load(%mission);
}The possibilities are endless. You could pass more arguements to more complex functions if you needed to, but the above can work a lot of things without needing to expand other code to activate them.
EDIT: One more example to demonstrate it's flexibility...
%what = "Game";
%who = "Reset";
%how = "";
%arg = "30";
leaving out the "how" simply results in a shorter function...
GameReset("30");but the arguement still gets passed which would call the following function...
function GameReset(%seconds)
{
schedule(1000 * %seconds, 0 "resetGame");
}
Torque Owner Brian Hill
$a=eval("return $b.count();");
echo($a);
echo(eval("return $b.count();"));