ChaiLove API  0.28.0
Public Member Functions
love::script Class Reference

This module provides all the global functions and events that are called. More...

Public Member Functions

void conf (config &t)
 Modify some configuration options. More...
 
void load ()
 This function is called exactly once at the beginning of the game. More...
 
void update (float delta)
 Callback function used to update the state of the game every frame. More...
 
void draw ()
 Callback function used to draw on the screen every frame. More...
 
void reset ()
 Called when the game is requested to be reset. More...
 
void joystickpressed (int joystick, const std::string &button)
 Called when a joystick button is pressed. More...
 
void joystickreleased (int joystick, const std::string &button)
 Called when a joystick button is released. More...
 
void mousepressed (int x, int y, const std::string &button)
 Called when a mouse button is pressed. More...
 
void mousereleased (int x, int y, const std::string &button)
 Called when a mouse button is released. More...
 
void mousemoved (int x, int y, int dx, int dy)
 Called when the mouse is moved. More...
 
void keypressed (const std::string &key, int scancode)
 Called when a key on the keyboard has been pressed. More...
 
void keyreleased (const std::string &key, int scancode)
 Called when a key on the keyboard is released. More...
 
std::string savestate ()
 Called when requested to save the current state. More...
 
bool loadstate (const std::string &data)
 Called when requested to load a state. More...
 
void cheatreset ()
 Called when requested to reset the state of all the cheats to their default state. More...
 
void cheatset (int index, bool enabled, const std::string &code)
 Called when requested to enable or disable a cheat. More...
 
void exit ()
 Callback function triggered when the game is closed. More...
 

Detailed Description

This module provides all the global functions and events that are called.

The following main.chai is an example of some of these callbacks being used in unison.

global logo
def load() {
logo = love.graphics.newImage("logo.png")
}
def draw() {
love.graphics.print("Hello World!", 400, 300)
love.graphics.draw(logo, 100, 100)
}
def update(dt) {
// Change something on the screen.
}

Member Function Documentation

◆ cheatreset()

void love::script::cheatreset ( )

Called when requested to reset the state of all the cheats to their default state.

Example

global invincible = false
def cheatreset() {
invincible = false
}
See also
cheatset

◆ cheatset()

void love::script::cheatset ( int  index,
bool  enabled,
const std::string &  code 
)

Called when requested to enable or disable a cheat.

Parameters
indexThe index of the cheat.
enabledWhether the cheat is to be enabled or disabled.
codeThe code for the cheat.

Example

global invincible = false
def cheatset(index, enabled, code) {
if (code == "invincible") {
// Enable or disable invincibility
invincible = enabled
}
}
See also
cheatreset

◆ conf()

void love::script::conf ( config t)

Modify some configuration options.

Parameters
tThe config object to modify.

Example

def conf(t) {
// Attach a development console, toggle with `.
t.console = false
// The ChaiLove version this game was made for.
t.version = "0.27.0"
// The width and height of the game.
t.window.width = 1024
t.window.height = 768
}
See also
love.config

◆ draw()

void love::script::draw ( )

Callback function used to draw on the screen every frame.

Example

Draw an image that was loaded in script::load (putting graphics.newImage in script::draw would cause the image to be reloaded every frame, which would cause issues).

global hamster
dev load() {
hamster = love.graphics.newImage("hamster.png")
}
def draw() {
love.graphics.draw(hamster, 100, 100)
}

◆ exit()

void love::script::exit ( )

Callback function triggered when the game is closed.

Example

def exit() {
print("Thanks for playing. Please play again soon!")
}

◆ joystickpressed()

void love::script::joystickpressed ( int  joystick,
const std::string &  button 
)

Called when a joystick button is pressed.

Parameters
joystickThe joystick number.
buttonThe name of which button was released.

Example

global buttonPressed = ""
def draw() {
love.graphics.print("Button Pressed: " + buttonPressed, 100, 100)
}
def joystickpressed(joy, button) {
buttonPressed = button
}
See also
joystickreleased

◆ joystickreleased()

void love::script::joystickreleased ( int  joystick,
const std::string &  button 
)

Called when a joystick button is released.

Parameters
joystickThe joystick number.
buttonThe name of which button was released.

Example

global buttonReleased = ""
def draw() {
love.graphics.print("Button Released: " + buttonReleased, 100, 100)
}
def joystickreleased(joy, button) {
buttonReleased = button
}
See also
joystickpressed

◆ keypressed()

void love::script::keypressed ( const std::string &  key,
int  scancode 
)

Called when a key on the keyboard has been pressed.

Parameters
keyThe name of the key that was pressed.
scancodeThe scancode of the key that was pressed.

Example

def keypressed(key, scancode) {
print("Key Pressed: " + key)
print("Scancode: " + to_string(scancode))
}
See also
keyreleased

◆ keyreleased()

void love::script::keyreleased ( const std::string &  key,
int  scancode 
)

Called when a key on the keyboard is released.

Parameters
keyThe name of the key that was released.
scancodeThe scancode of the key that was released.

Example

def keyreleased(key, scancode) {
print("Key Released: " + key)
print("Scancode: " + to_string(scancode))
}
See also
keypressed

◆ load()

void love::script::load ( )

This function is called exactly once at the beginning of the game.

Example

Establish some variables/resources on the game load, so that they can be used repeatedly in other functions (such as script::draw).

global hamster
def load() {
hamster = love.graphics.newImage("hamster.png")
}
def draw() {
love.graphics.draw(hamster, 100, 100)
}

◆ loadstate()

bool love::script::loadstate ( const std::string &  data)

Called when requested to load a state.

Parameters
dataA JSON array representing the state to load.
Returns
bool True if loading the state succeeded.

Example

global score = 0
def loadstate(data) {
var info = from_json(data)
score = info["score"]
return true
}
See also
savestate

◆ mousemoved()

void love::script::mousemoved ( int  x,
int  y,
int  dx,
int  dy 
)

Called when the mouse is moved.

Parameters
xThe mouse position on the x-axis.
yThe mouse position on the y-axis.
dxThe amount moved along the x-axis since the last time love.mousemoved was called.
dyThe amount moved along the y-axis since the last time love.mousemoved was called.

Example

global mousex = 0
global mousey = 0
def draw() {
love.graphics.setColor(255, 255, 255)
love.graphics.point(mousex, mousey)
}
def mousemoved(x, y, dx, dy) {
mousex = x
mousey = y
}

◆ mousepressed()

void love::script::mousepressed ( int  x,
int  y,
const std::string &  button 
)

Called when a mouse button is pressed.

Parameters
xThe mouse position on the x-axis.
yThe mouse position on the y-axis.
buttonThe mouse button name of which was pressed.

Example

global buttonPressed = "None"
global mousex = 0
global mousey = 0
def draw() {
love.graphics.print("Mouse Button Pressed: " + buttonPressed, mousex, mousey)
}
def mousepressed(x, y, button) {
buttonPressed = button
mousex = x
mousey = y
}
See also
mousereleased

◆ mousereleased()

void love::script::mousereleased ( int  x,
int  y,
const std::string &  button 
)

Called when a mouse button is released.

Parameters
xThe mouse position on the x-axis.
yThe mouse position on the y-axis.
buttonThe mouse button name of which was released.

Example

global buttonReleased = "None"
global mousex = 0
global mousey = 0
def draw() {
love.graphics.print("Mouse Button Released: " + buttonReleased, mousex, mousey)
}
def mousereleased(x, y, button) {
buttonReleased = button
mousex = x
mousey = y
}
See also
mousepressed

◆ reset()

void love::script::reset ( )

Called when the game is requested to be reset.

Example

global textX = 10
dev update(dt) {
textX += dt * 20
}
def draw() {
love.graphics.print("Hello World", textX, 100)
}
def reset() {
textX = 10
}

◆ savestate()

std::string love::script::savestate ( )

Called when requested to save the current state.

Returns
string A JSON array representing the current state.

Example

global score = 0
def savestate() {
return to_json([
"score": score
])
}
See also
loadstate

◆ update()

void love::script::update ( float  delta)

Callback function used to update the state of the game every frame.

Parameters
deltaTime since the last update in seconds.

Example

Change a variable var at a constant rate (+/- 3 per second in this example).

var position = 10
var rate = 3
def update(dt) {
if (love.keyboard.isDown("down")) {
position -= dt * rate
}
if (love.keyboard.isDown("up")) {
position += dt * rate
}
}