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

Scripting 04 - Button through script

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
Lead Programmer
Lead Programmer
Posts: 1003
Joined: Sun Feb 28, 2016 3:22 pm
Title: Lead programmer
Started SFD: Made it!
Location: Sweden
Age: 28

Scripting 04 - Button through script

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

Scripting 04 - Button through script

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 script I will show how to create a button at the start of the round and when pressed will display a popup message displaying the player's name and current time.

The script:

You can try this script in an empty map. Just copy-paste it into the script window and test run the map.
// OnStartup is run when the script starts running.
public void OnStartup()
	// Create ground
	// Create button pole
	Game.CreateObject("ButtonPole00", new Vector2(8f*(-2.5f), 8f*(-0.5f)), 0f);

        // Create the actual button - Button00 implements IObjectButtonTrigger and IObjectTrigger.
	IObjectButtonTrigger customButton = (IObjectButtonTrigger)Game.CreateObject("Button00", new Vector2(8f*(-2.5f), 8f*(0.5f)), 0f); 
	// Hook up method to run when the button is pressed
	customButton.SetScriptMethod("ButtonPressed"); // ! ScriptMethod must be a public void method with param TriggerArgs !

// Will be run when the button is pressed
public void ButtonPressed(TriggerArgs args)
	if (args.Sender is IPlayer)
		string playerName = ((IPlayer)args.Sender).Name;
		Game.ShowPopupMessage(string.Format("Button pressed by {0} at {1}", playerName, DateTime.Now.ToString()));

// Create some ground under the 0,0 coordinate of the world
private void CreateGround()
	IObject ground = Game.CreateObject("Concrete07B", new Vector2(8f*(-4.5f), 8f*(-1.5f)), 0f);
	Point sizeFactor = new Point(10, 1);
0 x

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest