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 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.
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 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
	CreateGround();
	
	// 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);
	ground.SetSizeFactor(sizeFactor);
}
1 x
Gurt

Locked