ChaiLove API  2.0.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 gamepadpressed (Joystick *joystick, const std::string &button)
 Called when a joystick button is pressed. More...
 
void gamepadreleased (Joystick *joystick, const std::string &button)
 Called when a joystick button is released. More...
 
void joystickpressed (Joystick *joystick, int button)
 Called when a joystick button is pressed. More...
 
void joystickreleased (Joystick *joystick, int 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.
}
The primary purpose of the graphics module is to draw to the screen.
Definition: graphics.h:25
graphics & draw(Image *image, int x, int y, float r, float sx, float sy, float ox, float oy)
Draws an image with the given angle, zoom, and origin.
graphics & print(const std::string &text, int x, int y)
Prints the given text to the screen.
Image * newImage(const std::string &filename)
Creates a new Image from a given filepath.
void load()
This function is called exactly once at the beginning of the game.
void update(float delta)
Callback function used to update the state of the game every frame.
void draw()
Callback function used to draw on the screen every frame.
This covers all the modules available through ChaiLove.
Definition: audio.h:9

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
}
void cheatreset()
Called when requested to reset the state of all the cheats to their default state.
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
}
}
void cheatset(int index, bool enabled, const std::string &code)
Called when requested to enable or disable a cheat.
See also
cheatreset

◆ conf()

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

Modify some configuration options.

Parameters
tThe config object to modify.

This callback can live in the conf.chai file.

Example

def conf(t) {
// The ChaiLove version this game was made for.
t.version = "0.29.0"
// The width and height of the game.
t.window.width = 1024
t.window.height = 768
}
void conf(config &t)
Modify some configuration options.
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!")
}
void exit()
Callback function triggered when the game is closed.

◆ gamepadpressed()

void love::script::gamepadpressed ( Joystick joystick,
const std::string &  button 
)

Called when a joystick button is pressed.

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

Example

global buttonPressed = ""
def draw() {
love.graphics.print("Button Pressed: " + buttonPressed, 100, 100)
}
def gamepadpressed(joy, button) {
buttonPressed = button
}
void gamepadpressed(Joystick *joystick, const std::string &button)
Called when a joystick button is pressed.
See also
gamepadreleased

◆ gamepadreleased()

void love::script::gamepadreleased ( Joystick joystick,
const std::string &  button 
)

Called when a joystick button is released.

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

Example

global buttonReleased = ""
def draw() {
love.graphics.print("Button Released: " + buttonReleased, 100, 100)
}
def gamepadreleased(joy, button) {
buttonReleased = button
}
void gamepadreleased(Joystick *joystick, const std::string &button)
Called when a joystick button is released.
See also
gamepadpressed

◆ joystickpressed()

void love::script::joystickpressed ( Joystick joystick,
int  button 
)

Called when a joystick button is pressed.

Parameters
joystickThe joystick object.
buttonThe number representing the button that was released.
See also
joystickreleased

◆ joystickreleased()

void love::script::joystickreleased ( Joystick joystick,
int  button 
)

Called when a joystick button is released.

Parameters
joystickThe joystick object.
buttonThe number representing the button that was pushed.
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))
}
void keypressed(const std::string &key, int scancode)
Called when a key on the keyboard has been pressed.
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))
}
void keyreleased(const std::string &key, int scancode)
Called when a key on the keyboard is released.
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
}
bool loadstate(const std::string &data)
Called when requested to load a state.
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
}
graphics & setColor(int red, int green, int blue, int alpha)
Sets the active drawing color to the given color.
graphics & point(int x, int y)
Draws a point.
void mousemoved(int x, int y, int dx, int dy)
Called when the mouse is moved.

◆ 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
}
void mousepressed(int x, int y, const std::string &button)
Called when a mouse button is pressed.
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
}
void mousereleased(int x, int y, const std::string &button)
Called when a mouse button is released.
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
}
void reset()
Called when the game is requested to be reset.

◆ 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
])
}
std::string savestate()
Called when requested to save the current state.
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
}
}
Provides an interface to the user's keyboard.
Definition: keyboard.h:15
bool isDown(const std::string &key)
Checks whether a certain key is down.