Legacy:EvaluateScript

From Spherical
Revision as of 02:29, 2 June 2013 by Apollolux (talk | contribs) (filled)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The EvaluateScript(filename) command will execute the code in the file called "filename" in the scripts directory of the current game. Common usage is to separate code into multiple files, making it easier for the programmer to go through the game's code.

Usage

EvaluateScript(filename);


  • filename string. The name of the file containing the code to be executed.

Examples

EvaluateScript("intro.js");
EvaluateScript("game_menu.js");
 
function game()
{
  //A function contained in "intro.js"
  Intro();
  //A function contained in "game_menu.js"
  GameMenu();
}

Notes

As said, the main usage of the EvaluateScript command is to be able to separate the game's code into multiple files, making it easier to find particular bits of code. As such, the files being evaluated by this command usually contains definitions of functions, objects, etc. such as

//in "show_name.js"
function ShowName()
{
  font.drawText(0,0, "Your name is "+player.name);
  FlipScreen();
  GetKey();
}

However, for things like cutscenes, execution commands may wish to be used:

//in "chap1_cutscene1.js"
 
ShowTalkWindow("It was a cold day...");
MovePerson("up");
ShowTalkWindow("...And I wanted a cup of tea.");

So in this case, when the EvaluateScript("chap1_cutscene1.js") command is used, it will give the same result as using the above three commands.

The eval() command

The Javascript eval(string) command is similar to that of the EvaluateScript() command, except it executes the string itself rather than the contents of a file. For example:

function RunGame()
{
  var my_number = 1;
 
  //Does: LoadGame(1);
  eval("LoadGame(1);");
}

Caution

Both the EvaluateScript() and eval() commands take considerable computer resources to execute, and as such, should only be used for loading or running commands that are not used multiple times a second. This for example, will run very slow, and consume a lot of memory:

//DoScript is as defined in the previous example
 
function ShowText()
{
  while(true)
  {
    DoScript("RenderMap();");
    DoScript("font.drawText(0,0, 'hello');");
    DoScript("FlipScreen();");
  }
}

As a general rule, never use the eval() or EvaluateScript(), or their commands where they could be used every frame or update. In such cases, try a different approach.

See also

API:Script/navbox