Skip to content
Werner Stoop edited this page Dec 26, 2013 · 3 revisions

Lua is a popular scripting language for games. Through Rengine's Lua State you get access to a Lua interpreter, with most of Rengine's functionality exposed.

Links:

Global functions

These functions have global scope in the Lua script.

log(message)

Writes a message to Rengine's log file.

setTimeout(func, millis)

Waits for millis milliseconds, then calls func

onUpdate(func)

Registers the function func to be called every frame

Game object

Functions in the Game scope

Game.changeState(newstate)

Changes the game's state to the state identified by newstate

Game.createParticle(x,y, dx,dy, life, color)

Creates a particle at position (x,y), with color color moving in direction (dx,dy) every frame. The particle life lasts for frames (So if the game's FPS is 33, then a life value of 66 will mean the the particle will last for 2 seconds)

Game.getStyle(style)

Retrieves a specific Style from the game.ini file.

BmpObj

The Bitmap object encapsulates a bitmap in the engine. Instances of BmpObj are drawn to the screen with the G.blit() function

Bmp(filename)

Loads the bitmap file specified by filename from the Resources and returns it encapsulated within a BmpObj instance.

BmpObj:__tostring()

Returns a string representation of the BmpObj instance.

BmpObj:__gc()

Garbage collects the BmpObj instance.

BmpObj:setMask(color)

Sets the color used as a mask when the bitmap is drawn to the screen.

CellObj

The Cell object. The CellObj encapsulates a collection of cells on the map. You then use the methods of the CellObj to manipulate the cells.

You use the C(selector) function to create a CellObj instance that collects all the cells on the maps that match the specific selector.

C(selector)

Returns a CellObj instance that encapsulates all the cells on a map that matches the particular selector.

CellObj:__tostring()

Returns a string representation of the CellObj

CellObj:__gc()

Frees the CellObj when it is garbage collected.

CellObj:set(layer, si, ti)

Sets the specific layer of the cells encapsulated in the CellObj to the si,ti value, where

  • si is the Set Index
  • ti is the Tile Index

G

G is the Graphics object that allows you to draw primitives on the screen.

You can only call these functions when the screen is being drawn. That's to say, you can only call then inside functions registered through onUpdate() If you call them elsewhere, you will get the error "Call to graphics function outside of a screen update"

These fields are also available:

  • G.FPS - The configured frames per second of the game (see game.ini).
  • G.SCREEN_WIDTH - The configured width of the screen.
  • G.SCREEN_HEIGHT - The configured height of the screen.

G.setColor(color)

Sets the color used to draw the graphics primitives.

G.pixel(x,y)

Plots a pixel at x,y on the screen.

G.line(x0, y0, x1, y1)

Draws a line from x0,y0 to x1,y1

G.rect(x0, y0, x1, y1)

Draws a rectangle from x0,y0 to x1,y1

G.fillRect(x0, y0, x1, y1)

Draws a filled rectangle from x0,y0 to x1,y1

G.circle(x, y, r)

Draws a circle centered at x,y with radius r

G.fillCircle(x, y, r)

Draws a filled circle centered at x,y with radius r

G.ellipse(x0, y0, x1, y1)

Draws an ellipse from x0,y0 to x1,y1

G.roundRect(x0, y0, x1, y1, r)

Draws a rectangle from x0,y0 to x1,y1 with rounded corners of radius r

G.fillRoundRect(x0, y0, x1, y1, r)

Draws a rectangle from x0,y0 to x1,y1 with rounded corners of radius r

G.curve(x0, y0, x1, y1, x2, y2)

Draws a Bezier curve from x0,y0 to x2,y2 with x1,y1 as the control point. Note that it doesn't pass through x1,y1

G.lerp(col1, col2, frac)

Returns a color that is some fraction frac along the line from col1 to col2. For example, G.lerp("red", "blue", 0.33) will return a color that is 1/3rd of the way from red to blue.

G.setFont(font)

Sets the font used for the G.print() function.

G.print(x,y,text)

Prints the text to the screen, with its top left position at x,y.

G.textDims(text)

Returns the width,height in pixels that the text will occupy on the screen.

Example:

 local w,h = G.textDims(message);

G.blit(bmp, dx, dy, [sx], [sy], [w], [h])

Draws an instance bmp of BmpObj to the screen at dx, dy. sx,sy specify the source x,y position and w,h specifies the width and height of the source to draw. sx,sy defaults to 0,0 and w,h defaults to the entire source bitmap.

Keyboard

Keyboard Input Functions

Keyboard.down([key])

Checks whether a key is down on the keyboard. The parameter key is the name of specific key. See http://wiki.libsdl.org/SDL_Scancode for the names of all the possible keys. If key is omitted, the function returns true if any key is down.

Keyboard.reset()

Resets the keyboard input.

Mouse

Mouse input functions.

These constants are used with Mouse.down() and Mouse.click() to identify specific mouse buttons:

  • Mouse.LEFT
  • Mouse.MIDDLE
  • Mouse.RIGHT

Mouse.position()

Returns the x,y position of the mouse.

Example:

 local x,y = Mouse.position();

Mouse.down(btn)

Returns true if the button btn is down.

Mouse.click(btn)

Returns true if the button btn was clicked. A button is considered clicked if it was down the previous frame and is not anymore this frame.