Module gameobject

The GameObject module.

Info:

  • Author: Sharper Dev

Functions

GameObject:new (name) Creates a GameObject with a transform and no components.
GameObject:addComponent (component) Creates and attaches a component by its registered component name.
GameObject:getComponent (componentName) Returns the first attached component whose name matches the argument.
GameObject:callUpdate () Calls update on each enabled updateable component if this object is enabled.
GameObject.instantiate (gameObjectPath, isUniversal) Adds a GameObject to the active scene, or to the universal scene when requested.
GameObject.findByName (name) Finds the first GameObject with this name in the first active scene.
GameObject.findByNameUniversal (name) Finds the first GameObject with this name in the universal scene.
GameObject:destroy () Disables the object, invokes its onDestroy hook, and destroys its components.


Functions

GameObject:new (name)
Creates a GameObject with a transform and no components.

Parameters:

  • name string Name assigned to the new object.

Returns:

    GameObject object The created object.

Usage:

    local GameObject = require("gameobject.m2d_gameobject")
    local obj = GameObject:new("MyObject")
GameObject:addComponent (component)
Creates and attaches a component by its registered component name.

Parameters:

  • component string Registered component name, such as "Script" or "Sprite".

Returns:

    Component component The created component.

Usage:

    local obj = GameObject:new("MyObject")
    obj:addComponent("Script")
GameObject:getComponent (componentName)
Returns the first attached component whose name matches the argument.

Parameters:

  • componentName string Component name to look up.

Returns:

    Component|nil component Matching component, or nil if none is found.

Usage:

    local obj = GameObject:new("MyObject")
    obj:addComponent("Script")
    local script = obj:getComponent("Script")
GameObject:callUpdate ()
Calls update on each enabled updateable component if this object is enabled.

Usage:

    gameObject:callUpdate()
GameObject.instantiate (gameObjectPath, isUniversal)
Adds a GameObject to the active scene, or to the universal scene when requested. Accepts either a Lua file path to load with dofile or an existing GameObject.

Parameters:

  • gameObjectPath string|GameObject File path or GameObject instance to add.
  • isUniversal boolean|nil If true, add to the universal scene.

Returns:

    GameObject object The added object.

Usage:

    local obj = GameObject.instantiate("path/to/GameObject.lua")
GameObject.findByName (name)
Finds the first GameObject with this name in the first active scene.

Parameters:

  • name string Name to search for.

Returns:

    GameObject|nil object Matching object, or nil if none is found.

Usage:

    local obj = GameObject.findByName("MyObject")
GameObject.findByNameUniversal (name)
Finds the first GameObject with this name in the universal scene.

Parameters:

  • name string Name to search for.

Returns:

    GameObject|nil object Matching object, or nil if none is found.

Usage:

    local obj = GameObject.findByNameUniversal("MyObject")
GameObject:destroy ()
Disables the object, invokes its onDestroy hook, and destroys its components. Clears the object's transform and updateable-component references.

Usage:

    gameObject:destroy()
generated by LDoc 1.5.0 Last updated 2026-10-04 12:53:32