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.