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.

