Scripted 'new' event handlers.
Posted: Tue Apr 03, 2012 4:21 am
In post https://bb.oolite.space/viewtopic.ph ... 10#p167807 in the Scripting Requests thread in response to
I said.
I think there is probably scope for more of this kind of thing. However it would be inefficient for this to be replicated through many scripts, and have multiple identical framecallbacks running. So I'm thinking there may be scope for Cabal_Common_eventHandlers?
A script that wanted to use Cabal_Common_eventHandlers would call a function in Cabal_Common_eventHandlers to register it's
Svengali, what do you think? It's doable, but would it be a resource killer? Have I missed anything?
I'll have a trawl through scripting requests to see if there is anything else outstanding that could be done in a similar way, in the meantime anyone with any bright ideas, or code snippets like that above please post.
Commander McLane wrote:Seconded, as it seems quite useful.Phasted wrote:An event-handler (well, two actually) that triggers when the player buys or sells cargo (passing commoditiy name, quantity, and price).
I said.
Well here is some sample code that does just thatIn the meantime you could do something clever with thethis.guiScreenChanged
handler triggering a framecallback including code to keep an eye on the players manifest and credit level whilst on the market screen and react appropriately. In effect using framecallbacks to make your own event handlers.
Code: Select all
// script needed to set up custom event handlers.
this.cargoType = ["food", "textiles", "radioactives", "slaves", "liquorWines", "luxuries", "narcotics", "computers", "machinery", "alloys", "firearms", "furs", "minerals", "gold", "platinum", "gemStones", "alienItems"];
this.guiScreenChanged = function(to,from)
{
if (to !== "GUI_SCREEN_MARKET" || guiScreen !== "GUI_SCREEN_MARKET"){return;}
this.originalManifest = this.getManifest();
this.originalCredits = player.credits;
this.monitorManifestCallBack = addFrameCallback(this.monitorManifest.bind(this));
}
this.monitorManifest = function(delta)
{
if (guiScreen !== "GUI_SCREEN_MARKET")
{
removeFrameCallback(this.monitorManifestCallBack);
delete this.monitorManifestCallBack;
return;
}
this.currentManifest = this.getManifest();
var counter;
var cargoDiff;
var creditDiff;
for (counter = 0; counter < 17;counter++)
{
cargoDiff = this.currentManifest[counter] - this.originalManifest[counter];
creditDiff = this.originalCredits - player.credits;
if (cargoDiff < 0) {this.playerSoldCargo(this.cargoType[counter],Math.abs(cargoDiff),Math.abs(creditDiff));}
if (cargoDiff > 0) {this.playerBoughtCargo(this.cargoType[counter],Math.abs(cargoDiff),Math.abs(creditDiff));}
if (cargoDiff !== 0) {this.originalManifest = this.currentManifest; this.originalCredits = player.credits;break;}
}
}
this.getManifest = function()
{
var counter;
var tempArray = new Array(17);
for (counter = 0; counter < 17;counter++)
{
tempArray[counter] = manifest[this.cargoType[counter]];
}
return tempArray;
}
// and here we have the custom event handlers...
this.playerBoughtCargo = function(cargoName,quantity,price)
{
log(this.name,"Player bought "+quantity+" of "+cargoName+" for "+price+" credits.");
}
this.playerSoldCargo = function(cargoName,quantity,price)
{
log(this.name,"Player sold "+quantity+" of "+cargoName+" for "+price+" credits.");
}
A script that wanted to use Cabal_Common_eventHandlers would call a function in Cabal_Common_eventHandlers to register it's
this.name
. Cabal_Common_eventHandlers would contain an adapted version of the code above which would look up it's register of worldScripts, check that the eventHandler existed in a aprticular worldScript and if so call the eventHandler in that worldScript.Svengali, what do you think? It's doable, but would it be a resource killer? Have I missed anything?
I'll have a trawl through scripting requests to see if there is anything else outstanding that could be done in a similar way, in the meantime anyone with any bright ideas, or code snippets like that above please post.