Difference between revisions of "Scripting Tasks"

From Elite Wiki
(Scripting Task: drain energy while equipment is being used (simulate energy usage))
 
(Added link to "script tool: Exception traceback")
 
(2 intermediate revisions by 2 users not shown)
Line 6: Line 6:
 
===using a Timer and it's callback function (modified example taken from [[Ore Processor]]):===
 
===using a Timer and it's callback function (modified example taken from [[Ore Processor]]):===
 
====Overview====
 
====Overview====
This solution uses a [[Oolite JavaScript Reference: Timer|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).
+
This solution uses a [[Oolite JavaScript Reference: Timer|Timer]] and it's callback function. When the equipment is activated, the timer is being started. The timer is configured to fire every 0.5 seconds. Every time it fires, the callback function ''this.$drainEnergy'' 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 of the [[Ore Processor|Ore Processor OXP]] for more info (e.g. howto play a sound while energy is drained).
   
 
=====1. Timer setup=====
 
=====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 ''[[Oolite JavaScript Reference: world script event handlers#startUp|this.startUp]]'' (which is part of a world script):
+
The timer ''this.$energyDrainTimer'' that periodically calls the energy reducing method has to be configured. Here this is done in the method ''[[Oolite JavaScript Reference: World script event handlers#startUp|this.startUp]]'' (which is part of a world script):
   
 
<pre>this.startUp = function ()
 
<pre>this.startUp = function ()
 
{
 
{
this.$oreTimer = new Timer(this, this.$processOre, 5, 0.5);
+
this.$energyDrainTimer = new Timer(this, this.$drainEnergy, 5, 0.5);
this.$oreTimer.stop();
+
this.$energyDrainTimer.stop();
 
}</pre>
 
}</pre>
   
 
=====2. prepare energy reducing function=====
 
=====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:
+
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 energy drain will stop (for safety):
   
<pre>this.$processOre = function ()
+
<pre>this.$drainEnergy = function ()
 
{
 
{
 
if (player.ship.energy > 64)
 
if (player.ship.energy > 64)
 
{
 
{
// busy processing splinter
 
 
player.ship.energy -= 6.4;
 
player.ship.energy -= 6.4;
  +
// do something with the used energy here
 
}
 
}
 
else
 
else
 
{
 
{
player.consoleMessage("Energy failure, no extraction of ore!!");
+
player.consoleMessage("Energy failure!!");
this.$oreTimer.stop();
+
this.$energyDrainTimer.stop();
 
}
 
}
 
}</pre>
 
}</pre>
   
 
=====3. start/stop the timer when equipment is activated/deactivated=====
 
=====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:
+
In the actual OreProcessor.oxp, the function energy draining function is called when a splinter is scooped. To make things simpler, that has been changed for this example. Now there exist functions ''this.$activateEnergyDrain'' and ''this.$deactivateEnergyDrain'' which simply start or stop the timer:
   
<pre>this.$activateOreProcessor = function ()
+
<pre>this.$activateEnergyDrain = function ()
 
{
 
{
this.$oreTimer.start();
+
this.$energyDrainTimer.start();
 
}</pre>
 
}</pre>
   
<pre>this.$activateOreProcessor = function ()
+
<pre>this.$deactivateEnergyDrain = function ()
 
{
 
{
this.$oreTimer.stop();
+
this.$energyDrainTimer.stop();
 
}</pre>
 
}</pre>
  +
  +
== Links ==
  +
*[http://www.aegidian.org/bb/viewtopic.php?f=4&t=20611 script tool: Exception traceback] (cag, 2020)
  +
::Using JavaScript's try ... catch statement, a primitive form of call stack traceback is possible.
   
 
[[Category:Oolite scripting]]
 
[[Category:Oolite scripting]]

Latest revision as of 15:06, 26 November 2021

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, the timer is being started. The timer is configured to fire every 0.5 seconds. Every time it fires, the callback function this.$drainEnergy 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 of the Ore Processor OXP for more info (e.g. howto play a sound while energy is drained).

1. Timer setup

The timer this.$energyDrainTimer 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.$energyDrainTimer = new Timer(this, this.$drainEnergy, 5, 0.5);
   this.$energyDrainTimer.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 energy drain will stop (for safety):

this.$drainEnergy = function ()
{
  if (player.ship.energy > 64)
  {
    player.ship.energy -= 6.4;
    // do something with the used energy here
  }
  else
  {
    player.consoleMessage("Energy failure!!");
    this.$energyDrainTimer.stop();
  }
}
3. start/stop the timer when equipment is activated/deactivated

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

this.$activateEnergyDrain = function ()
{
  this.$energyDrainTimer.start();                                                    
}
this.$deactivateEnergyDrain = function ()
{
  this.$energyDrainTimer.stop();                                                    
}

Links

Using JavaScript's try ... catch statement, a primitive form of call stack traceback is possible.