Difference between revisions of "Legacy:EvaluateScript"
(created from http://web.archive.org/web/20110803092824/http://www.spheredev.org/wiki/EvaluateScript) |
(filled) |
||
Line 1: | Line 1: | ||
− | |||
− | |||
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. | 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. | ||
Line 6: | Line 4: | ||
==Usage== | ==Usage== | ||
− | {{Usage | + | {{Usage|func=EvaluateScript|params=filename}} |
− | * ''' | + | * '''filename''' string. The name of the file containing the code to be executed. |
− | |||
− | |||
==Examples== | ==Examples== | ||
− | ( | + | <syntaxhighlight> |
+ | EvaluateScript("intro.js"); | ||
+ | EvaluateScript("game_menu.js"); | ||
+ | |||
+ | function game() | ||
+ | { | ||
+ | //A function contained in "intro.js" | ||
+ | Intro(); | ||
+ | //A function contained in "game_menu.js" | ||
+ | GameMenu(); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
==Notes== | ==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 |
+ | <syntaxhighlight> | ||
+ | //in "show_name.js" | ||
+ | function ShowName() | ||
+ | { | ||
+ | font.drawText(0,0, "Your name is "+player.name); | ||
+ | FlipScreen(); | ||
+ | GetKey(); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | However, for things like cutscenes, execution commands may wish to be used: | ||
+ | <syntaxhighlight> | ||
+ | //in "chap1_cutscene1.js" | ||
+ | |||
+ | ShowTalkWindow("It was a cold day..."); | ||
+ | MovePerson("up"); | ||
+ | ShowTalkWindow("...And I wanted a cup of tea."); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | So in this case, when the <code>EvaluateScript("chap1_cutscene1.js")</code> command is used, it will give the same result as using the above three commands. | ||
+ | |||
+ | ==The eval() command== | ||
+ | The Javascript <code>eval(string)</code> command is similar to that of the EvaluateScript() command, except it executes the string itself rather than the contents of a file. For example: | ||
+ | <syntaxhighlight> | ||
+ | function RunGame() | ||
+ | { | ||
+ | var my_number = 1; | ||
+ | |||
+ | //Does: LoadGame(1); | ||
+ | eval("LoadGame(1);"); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==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: | ||
+ | <syntaxhighlight> | ||
+ | //DoScript is as defined in the previous example | ||
+ | |||
+ | function ShowText() | ||
+ | { | ||
+ | while(true) | ||
+ | { | ||
+ | DoScript("RenderMap();"); | ||
+ | DoScript("font.drawText(0,0, 'hello');"); | ||
+ | DoScript("FlipScreen();"); | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 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== | ==See also== | ||
− | * | + | * [[API:EvaluateSystemScript|EvaluateSystemScript]] |
− | * | + | * [[API:RequireScript|RequireScript]] |
− | * | + | * [[API:RequireSystemScript|RequireSystemScript]] |
− | + | ||
+ | {{API:Script/navbox}} |
Latest revision as of 02:29, 2 June 2013
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
- 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.