Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the rules.

Scripting 01 - Getting Started

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.
Post Reply
User avatar
Gurt
Lead Programmer
Lead Programmer
Posts: 1884
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Gender:
Age: 33

Scripting 01 - Getting Started

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

Scripting 01 - Getting Started

Scripting in SFD allows you to create unique events and actions in your maps. We will expand the SFD Script API as time goes on to allow for even more possibilities.

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.

All objects in SFD implements the IBase interface which represents an object in the game that can be used in scripts. When working with IBase objects in the script you must cast it as required to either an IObject or IPlayer. The IObject implements a set of base functions for all tiles in the game - for example SetWorldPosition() or Destroy(). If you know that the IObject you're working with is an IObjectElevatorAttachmentJoint you must cast it in order to be able to call the implemented functions in the IObjectElevatorAttachmentJoint - for example SetMotorSpeed().


Scripting requires only triggers to get started:
Triggers are tiles that can be activated to call a method in your script, perform a specific set of actions and then activate other triggers (which in turn can call methods, perform specific actions and activate other triggers). The most basic triggers are the StartupTrigger (activates as soon as the game starts), Button00 (activates when a player push the button) or AreaTrigger (activates when an object or player touches the area).

All triggers have a property which can be filled with the name of the method to run when the trigger is activated (can be left blank) (for example the StartupTrigger have the property "Script Method"). The method in your script must have the following signature:
public void MyMethodName(TriggerArgs args)
{ 
	// code
}

where MyMethodName must match the method typed in the property on the trigger.

The TriggerArgs is a class with the following properties:
BaseObject Caller
Gets the trigger object. Can be null.

BaseObject Sender
Gets the sender object that activated the trigger. Can be null.

bool Handled
A flag indicating if the event has been handled by user code and if any following events (activating other triggers) should be canceled.

Cast the Caller and/or Sender as required.

Try it yourself:
1: Create a new map with a player spawn and some ground to walk on.

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.ShowPopupMessage("I pushed the button");
}
5: Compile the script in the script editor using F5 and correct any errors.

6: Test run your map and push the button. You should now see the text "I pushed the button".

Let's show the user's name in the text instead of "I".
7: Open the script editor again and type the following instead:
public void MyButtonPressed(TriggerArgs args)
{
   string text = "I pushed the button";
   if ((args.Sender != null) && (args.Sender is IPlayer)) // failsafe
   {
      IPlayer pushingButtonPlayer = (IPlayer)args.Sender;
      text = string.Format("{0} pushed the button", pushingButtonPlayer.Name);
   }
   Game.ShowPopupMessage(text);
}

8: Test run your map and push the button.
0 x
Gurt

Post Reply