This forum is locked and will eventually go offline. If you have feedback to share you can find us in our Discord channel "MythoLogic Interactive" https://discord.gg/nECKnbT7gk

Forum rules

Scripting 02 - Spawning objects

A smaller forum with a few tutorials how to get started with the ScriptAPI.
Forum rules
By using the forum you agree to the following rules.
Locked
User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1887
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 36

Scripting 02 - Spawning objects

Post by Gurt » Thu Mar 17, 2016 9:05 pm

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.
1 x
Gurt

User avatar
FRU1TSALAD
Meatbag
Posts: 3
Joined: Wed Apr 18, 2018 10:53 pm
Title: The Great Bamboozler
SFD Account: Lol9er9er
SFD Alias: FRU17 $ALAD
Started SFD: October 2016
Location: Severn,MD
Age: 23
Contact:

Post by FRU1TSALAD » Fri Apr 27, 2018 2:13 am

is there a way to give all players a specific item? making a map that requires this
0 x
"You can catch flies with honey, but you'll catch more honeys bein' fly"

User avatar
JakSparro98
Superfighter
Superfighter
Posts: 530
Joined: Fri Jul 15, 2016 7:56 pm
Started SFD: PreAlpha 1.0.5
Location: Rome, Italy
Gender:
Age: 27

Post by JakSparro98 » Fri Apr 27, 2018 9:46 pm

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.
0 x

Locked