Skip to content

Scripting

Shiba edited this page Feb 24, 2023 · 1 revision

Scripting

This is going to be a pretty quick overview and documentation of all of the scripting elements of the ShibaEngine.

Console

LogMessage(string)

LogMessage displays a message in the engine's console, these messages won't be visible when called in a compiled executable.

Example:

ShibaEngineCore.Console.LogMessage("Hello World!");

LogError(string)

Displays an error message in the engine's console, similar to LogMessage.

Instance

uint entity

This field is the entity id of the instance, this is used mostly by the engine itself.

string name

This is the name of the instance, assigned in the scene.

T GetCoreComponent()

T in this method should derive from CoreComponent, this method obtains the core component object from the instance it's called from.

Example:
public class Example : Component
{
    public Instance instance;
    public void Start()
    {
        var transform = instance.GetCoreComponent<Transform>();
        transform.position = new Vector3(0.0f, 5.0f, 0.0f);
    }
}

T GetComponent()

T in this method should derive from Component, this method obtains the object of the attached script to an instance.

Example:
public class Example : Component
{
    public int otherField;
    public Instance instance;
    public void Start()
    {
        var otherExample = instance.GetComponent<Example>();
        ShibaEngineCore.Console.LogMessage(otherExample.otherField.ToString());
    }
}

void AddComponent()

T in this method should derive from Component, this method adds a new component of a script to the instance.

Example:
public class Example : Component
{
    public Instance instance;
    public void Start()
    {
        instance.AddComponent<Example>();
    }
}

T[] FindComponentsInChildren()

T in this method should derive from Component, this method finds all of the specified components in the children of the instance.

Example:
public class Example : Component
{
    public int otherField;
    public Instance instance;
    public void Start()
    {
        foreach(var example : instance.FindComponentsInChildren<Example>())
        {
            ShibaEngineCore.Console.LogMessage(example.otherField.ToString());
        }
    }
}

Input

enum KeyCode

This is directly copied from the GLFW keycodes, this is just used to determine which key to check.

bool GetKeyDown(KeyCode key)

Checks whether the selected key had been pressed.

Time

float deltaTime

Time since the last frame, mostly used to make things framerate-independent.

float currentTime

The current frame count of the program, the same as glfwGetTime().

Behaviour

A behaviour is a static class that isn't attached to any instance, it runs an updates method at either a set interval or each frame. It is created by using the Behaviour attribute on any static class.

Example:
[Behaviour(3.5f)]
public static class ExampleBehaviour 
{
    public static void Update()
    {
        // gets called every 3.5 seconds
        ShibaEngineCore.Console.LogMessage("Behaviour Update");
    }
} 

Scene

uint CreateEntity()

Creates a new entity and returns it's id.

uint[] FindComponentsInScene()

The T in this method needs to derive from Component, this returns all the ids of entities that have the specified component attached.

Example:
public class Example : Component
{
    public int otherField;
    public Instance instance;
    public void Start()
    {
        foreach(var example : Scene.FindComponentsInScene<Example>())
        {
            var instance = GetInstance(example);
            ShibaEngineCore.Console.LogMessage(instance.name);
        }
    }
}

Components

Instance GetInstance(uint entity)

Gets the instance object for the specified entity id.

Example:
public class Example : Component
{
    public int otherField;
    public Instance instance;
    public void Start()
    {
        foreach(var example : Scene.FindComponentsInScene<Example>())
        {
            var instance = GetInstance(example);
            ShibaEngineCore.Console.LogMessage(instance.name);
        }
    }
}

Scripting Example

Vertical movement when holding space

public class VerticalMovement : Component 
{
    public float speed;
    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.KEY_SPACE))
        {
            transform.position += Vector3.UnitY * speed * Time.deltaTime;
        }
    }
}

An object spinning around another object

public class SpinningObject : Component
{
    public Instance otherObject;
    private Transform otherTransform;
    
    public float radius;
    public float speed;

    public void Start()
    {
        otherTransform = otherObject.GetCoreComponent<Transform>();
    }

    public void Update()
    {
        var circlePosition = new Vector3(MathF.Sin(Time.currentTime * speed) * radius, transform.position.y, MathF.Cos(Time.currentTime * speed) * radius);
        transform.position = circlePosition + otherTransform.position;
    }
}