I’ve been spending most of the night changing the event handling. Instead of calling event handlers called
STATUS_WHATEVER whenever the old script mechanism would run all the scripts, events are sent as and when noteworthy things happen. I won’t update the Wiki page now, what with it being four in the morning, but here’s my current test script. Almost all of it is known to actually work. Hopefully scripters will get some idea of how much more control this provides than the old "status_string equal STATUS_WHATEVER" approach. It’s also significantly more efficient. The “tickle” method is called at all the old polling opportunities, so if you
do need that behaviour, or just idle updates, that’s possible too. (Remember, though, about the efficiency thing.)
Handlers for events that have an instantaneous effect are called
didFoo(). For events that take time – mostly tunnel effects – there are
willFoo()/
didFoo() pairs.
Code: Select all
// This was originally written by dajt, but almost all the original script has been replaced.
this.name = "JavaScript-test";
this.author = "Jens Ayton";
this.copyright = "This work is hereby placed in the public domain.";
this.description = "A test OXP written using JavaScript. Based on a test script by dajt.";
this.version = "2.0";
// Called after all OXPs are launched, before the game has begun.
this.startUp = function()
{
LogWithClass("jstest.startUp", "Script startUp called.");
}
// Called when saved game is loaded, or player respawns after dying.
this.reset = function()
{
LogWithClass("jstest.reset", "Script reset called.");
}
// Called when alert status changes.
this.alertConditionChanged = function()
{
LogWithClass("jstest.alert", "Player alert condition changed to " + player.alertCondition + ", alert flags: " + player.alertFlags);
}
// Called before launch tunnel effect.
this.willDock = function()
{
LogWithClass("jstest.dock.begin", "Player is docking.");
}
// Called at end of docking tunnel effect.
this.didDock = function()
{
LogWithClass("jstest.dock.complete", "Player docked at" + stationName + ", which " + (dockedAtMainStation ? "is" : "is not") + " the system's main station.");
}
// Called before launch tunnel effect.
this.willLaunch = function()
{
LogWithClass("jstest.launch.begin", "Player is launching.");
}
// Called after launch tunnel effect.
this.didLaunch = function()
{
LogWithClass("jstest.launch.complete", "Player \"" + player.name + "\" has launched in a " + player.shipDescription + " into galaxy " + galaxyNumber + ", planet " + planetNumber + " (\"" + system.name + "\"), govt = " + system.governmentID + ", economy = " + system.economyID + ", tech level = " + system.techLevel);
}
// Called when player initiates witchspace countdown.
this.didBeginJumpCountDown = function(type) // Type is one of: "standard", "galactic". Others may be added in future.
{
LogWithClass("jstest.witchSpace.started", "Jump to " + type + " witchspace started.");
}
// Called if player cancels witchspace countdown.
this.didCancelJumpCountDown = function()
{
LogWithClass("jstest.witchSpace.cancelled", "Jump to witchspace cancelled.");
}
// Called at end of witchspace countdown, if unsuccessful.
this.didFailToJump = function(reason) // Reason is one of: "blocked", "too far", "insufficient fuel". Others may be added in future.
{
LogWithClass("jstest.witchSpace.failed", "Jump to witchspace failed. Reason: " + reason);
}
// Called at end of witchspace countdown, if successful, or on entring wormhole.
this.willEnterWitchSpace = function(cause) // cause is one of: "standard jump", "galactic jump", "wormhole". Others may be added in future.
{
LogWithClass("jstest.witchSpace.begin", "Player is entering witchspace. Cause: " + cause);
}
// Called at beginning of witchspace tunnel effect; the destination system is already set up at this time.
this.willExitWitchSpace = function()
{
LogWithClass("jstest.witchSpace.arrive", "Player is exiting witchspace.");
}
// Called after witchspace tunnel effect.
this.didExitWitchSpace = function()
{
LogWithClass("jstest.witchSpace.complete", "Player exited witchspace into galaxy " + galaxyNumber + ", planet " + planetNumber + " (\"" + system.name + "\"), govt = " + system.governmentID + ", economy = " + system.economyID + ", tech level = " + system.techLevel);
}
// Called when auto-docking sequence begins (but not for insta-dock).
this.didStartAutoPilot = function()
{
LogWithClass("jstest.autoPilot.on", "Player enabled autopilot.");
}
// Called when player cancels auto-docking sequence.
this.didAbortAutoPilot = function()
{
LogWithClass("jstest.autoPilot.cancelled", "Player cancelled autopilot.");
}
// Station says player can't dock on account of being naughty.
this.didRecieveDockingRefusal = function()
{
LogWithClass("jstest.autoPilot.refused", "Station refused autopilot docking.");
}
// Escape pod launched.
this.didLaunchEscapePod = function()
{
LogWithClass("jstest.escapePod", "Player ejected... the sissy.");
}
// Player died.
this.didBecomeDead = function()
{
LogWithClass("jstest.died", "Player died.");
}
// Called periodically, more or less.
this.tickle = function(status)
{
LogWithClass("jstest.idle", "Ping! Status is: " + status);
}
LogWithClass("jstest.loaded", "Script loaded.");
Currently broken:
willLaunch().
Known to be missing: enter/exit aegis; target changed; cloaking device events. What else?