Scripts

The mono scripting module uses C# scripts and compiles these to assemblies that are loaded and reloaded at runtime.

In order to fully understand how to write scripts you may refer to this document.

Base classes

The base class for writing scripts is EntityBehaviour.

Example:

public class MyScript : EntityBehaviour
{
    ...
}

Namespaces

It is recommended to add your scripts to a namespace, name the namespace the same as your project name.

Exmaple:

namespace MyProject
{
    public class MyScript : EntityBehaviour
    {
        ...
    }
}

Main Events

In order for the engine to communicate with these scripts, a few predefined functions can be used to receive events from the engine.

Start()

This methos is called during the first frame when play mode is activated.

Example:

public void Start()
{
    Debug.LogInfo("Start was called!");
}

Update()

Update is called at the start of every frame, but only during play mode.

Example:

public void Update()
{
    Debug.LogInfo(string.Format("Update() was called with a delta time {0}", Time.DeltaTime));
}

Draw()

Draw is called every frame, regardless of being in play mode or edit mode.

Example:

public void Draw()
{
    Debug.LogInfo(string.Format("Draw() was called with a delta time {0}", Time.DeltaTime));
}

Physics Events

The physics simulation will also trigger events, the engine will forward these the the following functions.

OnBodyActivated()

When a body awakes, the engine will trigger the OnBodyActivated() event on any scripts the object holds.

Example:

public void OnBodyActivated()
{
    Debug.LogInfo("Body activated");
}

OnBodyDeactivated()

When a body enters a sleeping state, the engine will trigger the OnBodyDeactivated() event on any scripts the object holds.

public void OnBodyDeactivated()
{
    Debug.LogInfo("Body deactivated");
}

OnContactAdded()

OnContactAdded is called when another body collides with the current body.

public void OnContactAdded(SceneObject other)
{
    Debug.LogInfo(Object.Name + " collided with " + other.Name);
}

OnContactPersisted()

As long as a collision persists, this event will be triggered every frame.

public void OnContactPersisted(SceneObject other)
{
    Debug.LogInfo("Body collisions persists between " + Object.Name + " and " + other.Name);
}

OnContactRemoved()

As soon as a collision ends, this event will be triggered.

public void OnContactRemoved(SceneObject other)
{
    Debug.LogInfo("Collision ended between " + Object.Name + " and " + other.Name);
}

Properties

Properties are variables that are exposed in the editor. In order to expose a field as a property it must be public.

Example:

public class MyClass
{
    // Will be exposed in the editor
    public float _speed = 5.0f;

    // Will be hidden in the editor
    private float _myVar = 1.0f;
}

Along with any basic type, the following types can also be used as properties:

public SceneObject _sceneObject;
public Mesh _texture;
public Model _texture;
public Material _material;
public Image _texture;

Referencing other objects

You can reference other objects in your scene by exposing a SceneObject variable as a property. Keep in mind that in order to make use of the entities API, you must cast it to an EntitySceneObject first!