Skip to content

Scripting

abc013 edited this page Apr 16, 2021 · 3 revisions

A script is a piece of code written in c# that can be attached to every map type. The script will be executed automatically when a game with that map type starts.

Creating a script

First, go to the maps\scripts\ folder. There, create a file with a unique name and the ending .cs. Please note that all scripts outside this folder and without the extension .cs will not be detected! Open the file with a text editor of your choice. A MissionScript has basic functions, which are listed below, and follows this structure:

using WarriorsSnuggery;
using WarriorsSnuggery.Scripting;

namespace Mission
{
	public class TutorialScript : MissionScriptBase
	{
		public TutorialScript(string file, Game game) : base(file, game)
		{
			// The constructor is called during the load process
		}

		public override void OnStart()
		{
			// This will be called when the game starts
		}

		public override void Tick()
		{
			// Script will be called every tick here
		}

		public override void OnFinish()
		{
			// The script will be called here when the game ends
		}

		public override void OnWin()
		{
			// The script will be called here when the player won
		}

		public override void OnLose()
		{
			// The script will be called here when the player lost
		}

		public override object[] GetState()
		{
			// Save the current progress of the script here. This function is used for gamesaves so, when loading, the script can continue where it was.
			// It is important to save all variables that are used in this script and that are vital for it
			return new object[0];
		}

		public override void LoadState(MiniTextNode[] nodes)
		{
			// Load the progress saved in a gamesave. Make sure to load it correctly so the script continues where it was when saved
			// Please note that OnStart() will not be executed when loading from a gamesave
		}
	}
}

Take a look at existing scripts to see how things are written. For more functions, feel free to browse in the source code of Warrior's Snuggery or enable autocompletion.

Autocompletion

Visual Studio

Autocompletion that works for all functions is active when using the source code of Warrior's Snuggery in Visual Studio. There is a project called WarriorsSnuggery.Scripts, in which the scripts of the maps\scripts\ folder are listed.

Visual Studio Code

Autocompletion can be enabled when loading the source code in the workspace and also loading the .cs script you want to edit. Please make sure to have installed Intellisense for c#.

Attaching the script to a map type

A script can be attached by adding the following line to a map type:

	MissionScript=<Filename>

Clone this wiki locally