VoidExpanse/Consumable scripting: Difference between revisions

From AtomicTorchWiki
(Created page with "== Consumables Scripting == In most cases, standard xml-structures are more then enough to implement consumable effect. However, if we need to do something special, for examp...")
 
m (Grammar, punctuation, phrasing edits.)
Line 1: Line 1:
== Consumables Scripting ==
== Consumables Scripting ==


In most cases, standard xml-structures are more then enough to implement consumable effect. However, if we need
In most cases the standard xml-structures are more than enough to implement consumable effects. However, if we need
to do something special, for example, restock ammo, self-destruct, teleport or kill all ships in front of ship, you'll need scripts.
to do something special, for example, restock ammo, self-destruct, teleport or kill all ships in front of the player, you will need scripts.


There are no hard rules about consumables scripting. Here are just a couple of tips.
There are no hard rules for scripting consumables, here are just a couple of tips.
For example, we need to create a consumable, that refills ammo in all ballistic weapons.
For example, if we need to create a consumable that refills ammo in all of the players ballistic weapons.
First of all, we need to create an empty consumable xml. Under "empty" I mean with no effects.
First of all, we need to create a basic consumable xml, with an empty effects node.
<pre>
<pre>
<effects>
<effects>
Line 13: Line 13:
</pre>
</pre>


Then, you create a new global script in folder /scripts/globals/, in which you hooks some function to a special trigger:
Then, you create a new global script in folder /scripts/globals/, in which you hook some function to a special trigger:
<pre>
<pre>
function OnInit()
function OnInit()
Line 21: Line 21:
}
}
</pre>
</pre>
After then, every time you use consumable with xml-id "consumable_ballistic_ammo", function "onBallisticAmmoConsumableUsed" will be called.
After that, every time a player uses a consumable with the xml-id "consumable_ballistic_ammo", function "onBallisticAmmoConsumableUsed" will be called.


Now, all that's left is to implement ammo refill:
Now, all that's left is to implement the actual ammo refill:
<pre>
<pre>
function onBallisticAmmoConsumableUsed( args )
function onBallisticAmmoConsumableUsed( args )
Line 37: Line 37:
</pre>
</pre>


That's all, now we have consumable, that refills ammo of ballistic weapons!
That's all, now we have a consumable that refills ammo the of all ballistic weapons!

Revision as of 03:16, 25 July 2014

Consumables Scripting

In most cases the standard xml-structures are more than enough to implement consumable effects. However, if we need to do something special, for example, restock ammo, self-destruct, teleport or kill all ships in front of the player, you will need scripts.

There are no hard rules for scripting consumables, here are just a couple of tips. For example, if we need to create a consumable that refills ammo in all of the players ballistic weapons. First of all, we need to create a basic consumable xml, with an empty effects node.

	<effects>
		<!-- no effects here -->
	</effects>

Then, you create a new global script in folder /scripts/globals/, in which you hook some function to a special trigger:

function OnInit()
{
	actions.BindToTrigger("onConsumableUsed", "onBallisticAmmoConsumableUsed",
		{id: "consumable_ballistic_ammo"});
}

After that, every time a player uses a consumable with the xml-id "consumable_ballistic_ammo", function "onBallisticAmmoConsumableUsed" will be called.

Now, all that's left is to implement the actual ammo refill:

function onBallisticAmmoConsumableUsed( args )
{
	var ballisticWeapons = ship.GetWeaponsOfType( args.ship_id, 2 );
	if(ballisticWeapons == null) return;
	
	for(var i =0; i < ballisticWeapons.length; i++)
	{
		ship.RefillAmmoForWeapon( args.ship_id, ballisticWeapons[i] );
	}
}

That's all, now we have a consumable that refills ammo the of all ballistic weapons!