Skip to content

Lua scripting

Nicolas Hiot edited this page Feb 8, 2017 · 8 revisions

To start you need to know how to code in Lua(5.3). Read the official manual here.

LuaScript component

Like all GameObject extension, Lua scripts is a component attach to it. To start you need to add a LuaScript component to your GameObject and set the script file.

LuaScript& lua = myGameObject.addComponent<LuaScript>();
lua.setScript("<path_to_the_script>/script.lua");

Now you have a game object with script attach to it.

Called functions

When you call setScript to LuaScript component, the script was execute to set up the global variables and functions. For making your script more usefull, some functions are automatically called if they exist :

  • awake, called when the scene is load;
  • start, called when the scene is load after all awake;
  • update, called at every frame;
  • lateUpdate, called at every frame after update.

Example :

a = 10 --Global variable

function awake()
    print "awake"
end

function start()
    print "start"
end

function update()
    print "update"
end

function lateUpdate()
    print "lateUpdate"
end

Bind between C++ & Lua

To interact with the engine and the game objects, some functions or objects are binding from C++ to Lua.

Gloabal variables

  • this, the game object on which the script is attached.

Functions

  • print(...) function was override to print in the Lua debug console;
  • getButtonPressed(name:string) function from InputManger, return true if the button name was pressed during the current frame, else return false;
  • getDeltaTime() function from Time, return the time elapse between two frames;
  • getSpriteSheet(name:string) function from AssetsManager, return the sprite sheet associated with the name name;
  • getDefaultSpriteSheet() function from AssetsManager, return the default sprite sheet;
  • getAnimation(name:string) function from AssetsManager, return the animation associated with the name name;
  • getDefaultAnimation() function from AssetsManager, return the default empty animation.

Objects / Types

  • SpriteSheet :
    • SpriteSheet(name:string, uri:string, tile_width:integer, tile_height:integer), return a new sprite sheet.
  • Animation :
    • Animation(name:string, spriteSheet:SpriteSheet), return a new animation;
    • addFrame(i:integer, j:integer, duration:number), add the sprite at the coordinates i, j in the sprite sheet as a frame with a duration.
  • SpriteRenderer :
    • setSprite(spriteSheet:SpriteSheet, i:integer, j:integer), set the sprite at the coordinates i, j in the sprite sheet.
  • AnimationRenderer :
    • setAnimation(animation:Animation), set the animation;
    • loop(value:boolean), choose if animation is a loop or not;
    • loop(), set animation as a loop;
    • play(), play the animation;
    • pause(), pause the animation;
    • stop(), stop the animation.
  • Camera :
    • zoom(scale:number), set the zoom value.
  • GameObject :
    • GameObject(), return a new game object;
    • getName(), return the name;
    • getPosition(), return two number, the current position x, y;
    • setPosition(x:number, y:number), move object to the position x, y;
    • getRotation(), return the current rotation;
    • setRotation(angle:number), set the rotation to angle;
    • getComponent(type:string), get or add the component of type name.
Clone this wiki locally