Learn DEFOLD with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
1
Defold Lua Script Example - Simple Player Movement
-- player.script
function init(self)
self.speed = 200
end
function update(self, dt)
local move = vmath.vector3()
if go.get_action("left").pressed then
move.x = move.x - 1
end
if go.get_action("right").pressed then
move.x = move.x + 1
end
go.set_position(go.get_position() + move * self.speed * dt)
end
A basic Defold script in Lua to move a player left and right using arrow keys.
2
Defold Lua Script Example - Player Jump
-- player_jump.script
function init(self)
self.speed = 200
self.vspeed = 0
self.gravity = 500
end
function update(self, dt)
local move = vmath.vector3()
if go.get_action("left").pressed then move.x = move.x - 1 end
if go.get_action("right").pressed then move.x = move.x + 1 end
if go.get_action("jump").pressed and self.vspeed == 0 then self.vspeed = -300 end
self.vspeed = self.vspeed + self.gravity * dt
move.y = self.vspeed * dt
go.set_position(go.get_position() + move * self.speed * dt)
end
Adds jumping to the player with simple gravity.
3
Defold Lua Script Example - Simple Shooting
-- player_shoot.script
function init(self)
self.speed = 200
end
function update(self, dt)
if go.get_action("shoot").pressed then
factory.create("#bullet_factory", go.get_position())
end
end
Player shoots a bullet when pressing space.
4
Defold Lua Script Example - Enemy Follow Player
-- enemy.script
function update(self, dt)
local player_pos = go.get_position("/player")
local pos = go.get_position()
local dir = vmath.normalize(player_pos - pos)
go.set_position(pos + dir * 100 * dt)
end
Enemy moves towards the player.
5
Defold Lua Script Example - Timer Example
-- timer.script
function init(self)
self.timer = 0
end
function update(self, dt)
self.timer = self.timer + dt
if self.timer >= 1 then
print("Timer triggered")
self.timer = 0
end
end
Runs a repeating timer action.
6
Defold Lua Script Example - Mouse Click Detection
-- mouse.script
function update(self, dt)
if mouse.is_button_pressed(mouse.BUTTON_LEFT) then
print("Left click")
end
if mouse.is_button_pressed(mouse.BUTTON_RIGHT) then
print("Right click")
end
end
Detects mouse clicks in the game window.
7
Defold Lua Script Example - Display FPS
-- fps.script
function update(self, dt)
local fps = 1 / dt
print(string.format("FPS: %.2f", fps))
end
Prints FPS each frame.
8
Defold Lua Script Example - Random Movement
-- random_move.script
function update(self, dt)
local pos = go.get_position()
pos.x = pos.x + math.random(-1,1) * 50 * dt
pos.y = pos.y + math.random(-1,1) * 50 * dt
go.set_position(pos)
end
Moves an object randomly each frame.
9
Defold Lua Script Example - Simple Animation
-- animation.script
function update(self, dt)
self.time = (self.time or 0) + dt
if self.time >= 0.1 then
sprite.play_flipbook(go.get_id(), hash("anim"))
self.time = 0
end
end
Cycles sprite animation frames automatically.
10
Defold Lua Script Example - Collision Detection
-- collision.script
function on_message(self, message_id, message, sender)
if message_id == hash("contact_point_response") then
print("Collision detected with", sender)
end
end
Checks collision with another game object.