VoidExpanse/Consumable scripting

From AtomicTorchWiki
Revision as of 03:16, 25 July 2014 by Tjmonk15 (talk | contribs) (Grammar, punctuation, phrasing edits.)

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!