Game Engine API Documentation

This documentation outlines the available API calls and custom classes within the EECS498.007 game engine. There may be inaccuracies present, so if you spot any, please add an issue at the github page with proof of the inaccuracy (spec screenshot). If you're intereseted in contributing, whether that be adding more documentation or improving the style, reach out on discord to @gnufoo!

Table of Contents

Custom Lua Classes

This section provides documentation on C++ classes that have been made accessible in Lua through LuaBridge, detailing available methods and their usage.

Actor Class

The Actor class represents an entity in the game world, with functions to manipulate its components and properties.

Methods

GetName()

Description:

Returns the name of the actor.

Example: local actorName = actor:GetName()

GetID()

Description:

Returns the ID of the actor.

Example: local actorID = actor:GetID()

GetComponent(type_name)

Description:

Returns a reference to the first component of type type_name. Returns nil if a component of type type_name doesn't exist.

Example: local transform = actor:GetComponent("Transform")

GetComponents(type_name)

Description:

Returns a reference to all components of type type_name in the form of an indexed table. Returns nil if a component of type type_name doesn't exist.

Example: local sprite_renderers = actor:GetComponent("SpriteRenderer") sprite_renderers[1].sprite = "sprite_1"

GetComponentByKey(key)

Description:

Returns a reference to the component with the key key. Returns nil if a component with the key key doesn't exist.

Example: local first_component = actor:GetComponentByKey("1")

AddComponent(type_name)

Description:

Add component of type type_name to actor and return reference to it. The new component should begin executing lifecycle functions on the next frame. The new comopnent's key will be rn, where r denotes a component created at runtime and n is the number of times that AddComponent(type_name) has been called in the entire program.

Example: local sprite_renderer = actor:AddComponent("SpriteRenderer") sprite_renderer.sprite = "new_sprite"

RemoveComponent(component_ref)

Description:

Removes component component_reffrom the actor.

Example: local sprite_renderer = actor:GetComponent("SpriteRenderer") actor:RemoveComponent(sprite_renderer)

Vector2 Class

The Vector2 class represents an 2D mathematical vector. Supports adding and subtracting of vetors as well as scalar multiplication.

Members:

x - The x component of the vector.

y - The y component of the vector.

Methods

Normalize()

Description:

Converts this vector into a unit vector. Returns the length.

Example: local vector = Vector2(2, 5)
vector.Normalize()

Length()

Description:

Returns the length of the vector.

Example: local vector = Vector2(2, 5)
Debug.Log(vector.Length())

Vector2.Distance(a, b)

Description:

A static Vector2 function. Returns Length(a - b).

Example: local a = Vector2(2, 5)
local b = Vector2(-1, -3)
Debug.Log(Vector2.Distance(a, b))

Vector2.Dot(a, b)

Description:

A static Vector2 function. Returns the dot product of the two vectors.

Example: local a = Vector2(2, 5)
local b = Vector2(-1, -3)
local dot_product = Vector2.Dot(a, b)

Application API

Application.Quit()

Description:

Exits the application immediately with error code 0.

Example: Application.Quit()

Application.Sleep(milliseconds)

Description:

Puts the application to sleep for the specified number of milliseconds.

Parameters:

milliseconds - Integer. The number of milliseconds the app will sleep for.

Example: Application.Sleep(1000)

Application.GetFrame()

Description:

Returns the current frame number.

Example: Application.GetFrame()

Application.OpenURL(url)

Description:

Opens the specified web page in the user’s default browser.

Parameters:

url - String. The URL of the web page to open.

Example: Application.OpenURL("http://www.example.com")

Input API

Input.GetKey(keycode)

Description:

Returns true if the specified keycode is held down on the current frame.

Parameters:

keycode - The keycode to check.

Example: Input.GetKey("space")

Input.GetKeyDown(keycode)

Description:

Returns true if the keycode became pressed on the current frame.

Parameters:

keycode - The keycode to check for a press event.

Example: Input.GetKeyDown("a")

Input.GetKeyUp(keycode)

Description:

Returns true if the keycode became lifted on the current frame.

Parameters:

keycode - The keycode to check for a release event.

Example: Input.GetKeyUp("up")

Input.GetMousePosition()

Description:

Returns the current mouse position as a table with x and y coordinates.

Example: local position = Input.GetMousePosition()
print("X: " .. position.x .. ", Y: " .. position.y)

Input.GetMouseButton(button_num)

Description:

Returns true if the specified mouse button is currently pressed.

Parameters:

button_num - The mouse button number (1 for left, 2 for middle, 3 for right).

Example: Input.GetMouseButton(1)

Input.GetMouseButtonDown(button_num)

Description:

Returns true if the specified mouse button was pressed down on this frame.

Parameters:

button_num - The mouse button number to check.

Example: Input.GetMouseButtonDown(3)

Input.GetMouseButtonUp(button_num)

Description:

Returns true if the specified mouse button was lifted on this frame.

Parameters:

button_num - The mouse button number to check for a release event.

Example: Input.GetMouseButtonUp(2)

Input.GetMouseScrollDelta()

Description:

Returns the mouse scroll wheel delta as a float. Returns 0 if there was no scroll event on this frame.

Example: local delta = Input.GetMouseScrollDelta()
print("Mouse scroll delta: " .. delta)

Actor API

Actor.Instantiate(actor_template_name)

Description:

Creates a new actor based on the specified actor template and returns a reference to it. The actor's components will not execute until the next frame, but the actor is discoverable via Actor.Find() or Actor.FindAll() immediately.

Parameters:

actor_template_name - The name of the actor template to instantiate.

Example: local newActor = Actor.Instantiate("Enemy")

Actor.Destroy(actor)

Description:

Destroys an actor, removing it and all of its components from the scene. No lifecycle functions of the actor's components will run after this call. The actual destruction of the actor occurs at the end of the current frame, after all OnLateUpdate calls.

Parameters:

actor - The actor to destroy.

Example: Actor.Destroy(someActor)

Audio API

Audio.Play(channel, clip_name, does_loop)

Description:

Begins playing the specified audio clip on the specified channel, with an option to loop the audio.

Parameters:

channel - The audio channel on which to play the clip.

clip_name - The name of the audio clip to play.

does_loop - Boolean value indicating whether the audio clip should loop (true) or play once (false).

Example: Audio.Play(1, "background_music", true)

Audio.Halt(channel)

Description:

Halts audio playback on the specified channel.

Parameters:

channel - The audio channel whose playback will be halted.

Example: Audio.Halt(1)

Audio.SetVolume(channel, volume)

Description:

Alters the volume of an audio channel.

Parameters:

channel - The channel whose volume will be adjusted.

volume - The new volume level as an integer, ranging from 0 (silent) to 128 (maximum volume).

Example: Audio.SetVolume(1, 64)

Text API

Text.Draw(str_content, x, y, font_name, font_size, r, g, b, a)

Description:

Draws text on the screen at the specified position and with the given appearance settings.

Parameters:

str_content - The string content to draw.

x, y - The x and y screen coordinates where the text will begin.

font_name - The name of the font to use.

font_size - The size of the font.

r, g, b, a - The red, green, blue, and alpha values for the text color. Each component ranges from 0 to 255.

Example: Text.Draw("Hello, World!", 100, 50, "Arial", 24, 255, 255, 255, 255)

Image API

Image.DrawUI(image_name, x, y)

Description:

Draws an image to the UI layer using screen coordinates, unaffected by the camera. Images must be redrawn every frame to remain visible. Defaults to white color and full opacity. UI elements render behind text elements by default, with a default sorting order of 0.

Parameters:

image_name - The name of the image to draw.

x, y - Screen coordinates for where to draw the image.

Example: Image.DrawUI("logo", 100, 200)

Image.DrawUIEx(image_name, x, y, r, g, b, a, sorting_order)

Description:

Extended version of DrawUI, allowing for tint and alpha modifications as well as a specified sorting order. Numeric parameters are treated as floats but are downcasted to integers in C++.

Parameters:

image_name - The name of the image to draw.

x, y - Screen coordinates for where to draw the image.

r, g, b, a - The red, green, blue, and alpha values for the image color. Each component ranges from 0 to 255.

sorting_order - The relative order in which to draw images on the UI.

Example: Image.DrawUIEx("hero_icon", 400, 300, 255, 0, 0, 128, 1)

Image API

Image.Draw(image_name, x, y)

Description:

Draws an image in scene coordinates, affected by camera position and zoom. Assumes the pivot point is at the center of the image. This call must be made every frame to maintain visibility, rendering behind UI elements.

Parameters:

image_name - The name of the image to draw.

x, y - Screen coordinates for where to draw the image.

Example: Image.Draw("background", 0, 0)

Image.DrawEx(image_name, x, y, rotation_degrees, scale_x, scale_y, pivot_x, pivot_y, r, g, b, a, sorting_order)

Description:

Draws an image with extended options for rotation, scaling, tint, alpha, and sorting order. Parameters for rotation, colors, and sorting order are treated as floats but downcasted to integers in C++.

Parameters:

image_name - The name of the image to draw.

x, y - Screen coordinates for where to draw the image.

rotation_degrees - An angle in degrees that indicates the rotation that will be applied to the image, rotating it in a clockwise direction.

scale_x, scale_y - Alters the size of an image on the x and y axis, where 1 is the normal size.

r, g, b, a - The red, green, blue, and alpha values for the image color. Each component ranges from 0 to 255.

sorting_order - The relative order in which to draw images on the UI.

Image name, position, rotation, scale, pivot point, color, alpha, and sorting order details.

Example: Image.DrawEx("enemy_sprite", 500, 500, 45, 1.0, 1.0, 0.5, 0.5, 255, 255, 255, 255, 2)

Image.DrawPixel(x, y, r, g, b, a)

Description:

Draws a single pixel at specified screen coordinates with the given color and alpha.

Parameters:

x, y - Screen coordinates for where to draw the pixel.

r, g, b, a - The red, green, blue, and alpha values for the pixel color. Each component ranges from 0 to 255.

Example: Image.DrawPixel(500, 500, 255, 255, 255, 255)

Camera API

Camera.SetPosition(x, y)

Description:

Sets the camera's position in the scene using floating-point coordinates.

Parameters:

x, y - The x and y coordinates to set the camera's position to.

Example: Camera.SetPosition(100.0, 200.0)

Camera.GetPositionX()

Description:

Returns the x-coordinate of the camera's position as a float.

Example: local x = Camera.GetPositionX()

Camera.GetPositionY()

Description:

Returns the y-coordinate of the camera's position as a float.

Example: local y = Camera.GetPositionY()

Camera.SetZoom(zoom_factor)

Description:

Sets the camera's zoom level in the scene. The zoom factor is a float, adjusting the render scale for Image.Draw() and Image.DrawEx() calls accordingly.

Parameters:

zoom_factor - The zoom factor to set, represented as a float.

Example: Camera.SetZoom(1.5)

Camera.GetZoom()

Description:

Returns the current zoom factor of the camera as a float.

Example: local zoom = Camera.GetZoom()

Scene API

Scene.Load(scene_name)

Description:

Loads a new scene at the beginning of the next frame. If called multiple times within a single frame, only the most recent scene_name will be loaded.

Parameters:

scene_name - The name of the scene to load.

Example: Scene.Load("basic")

Scene.GetCurrent()

Description:

Returns the name of the current scene, excluding the “.scene” file extension.

Example: local currentScene = Scene.GetCurrent()

Scene.DontDestroy(actor)

Description:

Marks an actor to persist and not be unloaded or destroyed when a new scene is loaded. This is particularly useful for maintaining global “manager” actors across different scenes.

Parameters:

actor - A reference to the actor that should not be destroyed.

Example: Scene.DontDestroy(self.actor)