SetDefaultMapScript
From Spheriki
Sets the script to run for a map event, if there is no already specified script.
Contents |
Usage
- script_name A Constant type from list given below:
- SCRIPT_ON_ENTER_MAP
- SCRIPT_ON_LEAVE_MAP
- SCRIPT_ON_LEAVE_MAP_NORTH
- SCRIPT_ON_LEAVE_MAP_EAST
- SCRIPT_ON_LEAVE_MAP_SOUTH
- SCRIPT_ON_LEAVE_MAP_WEST
- script A string consisting of javascript code that will be executed.
Examples
This command is mainly used for either repetitous code that needs to be executed for an event on every map, or as a way of regaining control of the script execution after the mapengine has been called. One useful example for an RPG would be to display the name of the current map for a while after you have entered a new area, given below:
SetDefaultMapScript(SCRIPT_ON_ENTER_MAP, "DisplayMapName();");
function DisplayMapName()
{
g_count = 50;
SetUpdateScript("DrawName();");
}
var g_count = 0;
function DrawName()
{
g_count--;
font.drawText(0,0, current_map_name);
if (g_count <= 0)
{
g_count = 0;
SetUpdateScript("");//reset the updatescript to nothing;
}
}
And an example for how to regain control of code execution after calling the map engine WITHOUT having to explicitly add code into the map file:
function game()
{
SetDefaultMapScript(SCRIPT_ON_ENTER_MAP, "startcode()");
MapEngine(mapname, 60);
}
function startcode()
{
SetDefaultMapScript(SCRIPT_ON_ENTER_MAP, "");//reset default code
//run whatever code here you want (eg: game loops etc.)
ExitMapEngine();
}
Notes
- The map engine does not have to be running to use this command!
- The main purpose of this command is to be able to set the map event scripts without having to change them inside the map files (which can be tedious at times). As well, it allows for dynamic changing of these event scripts (so the script can be changed in game).