Page 1 of 1

Scripting 02 - Spawning objects

Posted: Thu Mar 17, 2016 9:05 pm
by Gurt
Scripting 02 - Spawning objects

Scripting in SFD assumes you have a fair knowledge of C#. You should be able to declare variables, write your own functions and classes, you should know about inheritance and derived classes and know how to cast objects.

In this part I will illustrate how to create and manipulate objects through code.


Try it yourself:
Let's create a button that will spawn a barrel in the centre of the map when pressed.

1: Create a new map with a player spawn and some ground to walk on below the centre of the map.

2: Place a Button00 in the map.

3: For the "Script Method" property for Button00 type in "MyButtonPressed".

4: Open the script editor and type the following:
public void MyButtonPressed(TriggerArgs args)
{
	Game.CreateObject("Barrel00", Vector2.Zero, 0f);
}
Note: Barrel00 is the tile's name as seen in the tile-list in the editor.

5: Test run the map. A barrel will now be spawned in the centre of the map each time you press the button.

Let's change the code so a crate will spawn 80 world units above the button instead.
6: Open the script editor and type the following instead (comments not mandatory to include):
public void MyButtonPressed(TriggerArgs args)
{
	Vector2 worldPosition = Vector2.Zero;
	if ((args.Caller != null) && (args.Caller is IObject)) // failsafe
	{
		IObject myButton = (IObject)args.Caller; // caller is the button in this case
		worldPosition = myButton.GetWorldPosition() + new Vector2(0f, 80f);
	}
	Game.CreateObject("Crate00", worldPosition, 0f);
}
7: Test run the map. Crate00 will spawn 80 world units above the button each time it's pressed.

Let's change the code so each spawned crate will spawn a little further away from the button and let's destroy all spawned creates each fifth time pressed.

8: Open the script editor and type the following instead (comments not mandatory to include):
int buttonPressCount = 0;
public void MyButtonPressed(TriggerArgs args)
{
	buttonPressCount ++;
	if (buttonPressCount <= 4)
	{
		// keep spawning crates
		Vector2 worldPosition = Vector2.Zero;
		if ((args.Caller != null) && (args.Caller is IObject)) // failsafe
		{
			IObject myButton = (IObject)args.Caller; // caller is the button in this case
			worldPosition = myButton.GetWorldPosition() + new Vector2(buttonPressCount * 20, 80f);
		}
		IObject createdObject = Game.CreateObject("Crate00", worldPosition, 0f);
		createdObject.CustomId = "MyCrate"; // the customId will be used to be able to keep track on the crates
	} 
	else 
	{
		// Destroy all spawned objects with CustomId "MyCrate" and reset buttonPressCount 
		IObject[] myCrates = Game.GetObjectsByCustomId("MyCrate"); // returns an IObject array containing all objects with CustomId "MyCrate"
		for (int i = 0; i < myCrates.Length; i ++) // iterate over all objects and destroy them
		{
			myCrates.Destroy();
		}
		buttonPressCount = 0;
	}
}


9: Test run the map. Press the button 4 times and 4 crates will spawn. Press the button a fifth time and all crates will be destroyed.

Re: Scripting 02 - Spawning objects

Posted: Fri Apr 27, 2018 2:13 am
by FRU1TSALAD
is there a way to give all players a specific item? making a map that requires this

Re: Scripting 02 - Spawning objects

Posted: Fri Apr 27, 2018 9:46 pm
by JakSparro98
FRU1TSALAD wrote:
Fri Apr 27, 2018 2:13 am
is there a way to give all players a specific item? making a map that requires this
If you need help regarding something related with scripts please spend a little of your time exposing the problem in the general section of ScriptAPI, start a new thread and avoid asking something in a non-relevant way if you want to join a discussion in an existing thread.