Difference between revisions of "Scripting Tasks"
(Scripting Task: drain energy while equipment is being used (simulate energy usage)) |
(changed names to make example more generic) |
||
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 | + | 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.$ | + | 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.$ | + | this.$energyDrainTimer = new Timer(this, this.$drainEnergy, 5, 0.5); |
− | this.$ | + | 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 | + | 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.$ | + | <pre>this.$drainEnergy = function () |
{ | { | ||
if (player.ship.energy > 64) | if (player.ship.energy > 64) | ||
{ | { | ||
− | |||
player.ship.energy -= 6.4; | player.ship.energy -= 6.4; | ||
+ | // do something with the used energy here | ||
} | } | ||
else | else | ||
{ | { | ||
− | player.consoleMessage("Energy failure | + | player.consoleMessage("Energy failure!!"); |
− | this.$ | + | 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 | + | 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.$ | + | <pre>this.$activateEnergyDrain = function () |
{ | { | ||
− | this.$ | + | this.$energyDrainTimer.start(); |
}</pre> | }</pre> | ||
− | <pre>this.$ | + | <pre>this.$deactivateEnergyDrain = function () |
{ | { | ||
− | this.$ | + | this.$energyDrainTimer.stop(); |
}</pre> | }</pre> | ||
[[Category:Oolite scripting]] | [[Category:Oolite scripting]] |
Revision as of 21:26, 24 February 2013
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(); }