Scripting Tasks

From Elite Wiki
Revision as of 21:16, 24 February 2013 by GGShinobi (talk | contribs) (Scripting Task: drain energy while equipment is being used (simulate energy usage))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This page lists examples for solutions of often recurring scripting tasks. If you know a better or other way of doing something or have any suggestions or ideas, feel free to edit / add to this page!

drain energy while equipment is being used (simulate energy usage)

using a Timer and it's callback function (modified example taken from Ore Processor):

Overview

This solution uses a Timer and it's callback function. When the equipment is activated (here: by collecting some processable ore), the timer is being started. The timer is configured to fire every 0.5 seconds. Every time it fires, the callback function this.$processOre is being called, which reduces the ships' energy by a certain value (here: by 6.4). Note: for simplicity, the code shown here is reduced to only those parts that simulate the energy usage. Have a look at the original source code for more info (e.g. howto play a sound while energy is drained).

1. Timer setup

The timer this.$oreTimer that periodically calls the energy reducing method has to be configured. Here this is done in the method this.startUp (which is part of a world script):

this.startUp = function ()
{
   this.$oreTimer = new Timer(this, this.$processOre, 5, 0.5);
   this.$oreTimer.stop();
}
2. prepare energy reducing function

Together with the timer, this method simulates the energy draining by reducing the ships' energy every time that it is called. If the ships' energy drops to 64, the ore processor will deactivate itself for safety:

this.$processOre = function ()
{
  if (player.ship.energy > 64)
  {
    // busy processing splinter
    player.ship.energy -= 6.4;
  }
  else
  {
    player.consoleMessage("Energy failure, no extraction of ore!!");
    this.$oreTimer.stop();
  }
}
3. start/stop the timer when equipment is activated/deactivated

In the actual OreProcessor.oxp, the function this.$processOre is called when a splinter is scooped. To make things simpler, that has been changed for this example. Now there exist functions this.$activateOreProcessor and this.$deactivateOreProcessor which simply start or stop the timer:

this.$activateOreProcessor = function ()
{
  this.$oreTimer.start();                                                    
}
this.$activateOreProcessor = function ()
{
  this.$oreTimer.stop();                                                    
}