Patch: Server Scripts

The "Server Scripts" patch allows you to program scripts with C# that interact with MachineCraft

Once enabled, a new text box is shown in the HOST menu. Here is where you enter the names of scripts to be executed, each separated by a semi-colon (;). Hosting a room (without JOIN) will run the server scripts.

//#requires

At the beginning of your script, if it requires other scripts to run, you will need to define them here.

For example, if your script needs commandLib.cs and nameLib.cs to run, the beginning of your script will be:

//#requires commandLib.cs
//#requires nameLib.cs

These required files will be searched for in the _scripts directory, so if the script is inside another directory, be sure to add it to your requires statement, such as

//#requires libs\commandLib.cs

which will look for the script in UserData\_scripts\libs\commandLib.cs.

Basic Server script

All server scripts must inherit from the HostScript class. They must also override the pluginName and pluginCreator properties. A simple template is shown below:

class templateScript : HostScript {
    public override string pluginName => "template";
    public override string pluginCreator => "Dylan";



}

MCNPlayer Object

The MCNPlayer object represents a player currently connected to the room.

Properties

playerName

The name of the player.

machineName

The name of the player's machine.

plrID

The ID of the player based on when they joined the room. The first player will have ID 0, the second will have ID 1, and so on.

functions

sendMessage(string)

Shows a message to the player in chat.

MCNServer class

The MCNServer class is a static class containing various functions to interact with the MachineCraft server.

Properties

players

an array of MCNPlayer objects repesenting the currently connected players.

Functions

broadcastMessage(string)

Broadcasts a message to all connected players

setSnow(int)

Acts like the >snow=? command, where the number can be from 0 to 9.

setRain(int)

Acts like the >rain=? command, where the number can be from 0 to 9.

setCloud(int)

Acts like the >cloud=? command, where the number can be from 0 to 9.

setCycle(int)

Acts like the >cycle=? command

setTime(int)

Acts like the >time=? command, where the number can be from 0 to 24.

playMovie(string)

Acts like the >movie=? command, where the first parameter is the URL of the file to play.

stopMovie()

Stops the currently playing movie

pauseMovie()

Pauses the currently playing movie

resumeMovie()

Resumes the currently playing movie

HostScript class

This is the main class you will use for creating your own scripts. It has functions you can override that get called when an event occurs.

Overridable Methods

onInit()

Called when the script is first compiled and run. Similar to unity's Start() method.

onDestroy()

Called when the script is destroyed. Should be used to clean up any resources created by the script

onPlayerJoin(MCNPlayer)

Called when a player joins the room.

onPlayerSwitchedMachine(MCNPlayer)

Called when a player switches their machine (using machine switch patch)

onPlayerLeave(MCNPlayer)

Called when a player leaves the room

onChatMessage(MCNPlayer, string)

Called when a player sends a chat message.

onDeath(MCNPlayer)

Called when a player's machine explodes.