Big List of Defold Pro Tips!


Hash is fast, but it’s still more efficient to pre-hash everything. See this thread for other ways to do it Is calling hash() every time inefficient?

1 Like


Also, it’s good to keep all the pre-hashed values in variables so that you don’t accidentally misspell something.



Just little insight that maybe some newcomer didnt notice, in GUI when you want to sort like where the render first (Node), it must in order from the top to bottom list. To make sure you wanna move it, just press “alt + up” or “alt + down” to reorder the nodes…



The above tip for rearranging nodes is good for simpler UIs. For more complex GUI scenes you’re likely to want to use hierarchies and layers.



How to fixed uncaught unexpected data size
Fixing uncaught unexpected data size in html build.
This error happened when you maka a build in windows and them commit that html build it git. When git commit it replace line endings. So file size can be changes and runtime can’t load it.
To fix it you need:
1)add .gitattributes file to project root
2)in that file add “* binary”. It said to git that all files is binary add it will not change file endings. example

* binary

3)commit that file
4)fixed current endings


App crashes during HTML5 bundle load - "Unexpected Data Size"

Line break
There is a very useful property for text node in gui:


and for regural game object’s label:


that allows to divide the text and flow it over several lines without additional coding. I’ve discovered it right now, so I’m posting it for the others overlookers :smiley: It is of course described in the documentation :stuck_out_tongue:



Module with prehashed message ids
As many of you know, it is a good practice to pre hash message ids, so for a generic solution prepare a module (e.g. msgs.lua) that just returns one table with key indexed hashes of message ids, like this for example:

return {
CONTACT = hash("contact_point_response"),	-- defold generated message, sent on one point collision of kinematic and dynamic objects
COLLISION = hash("collision_response"),		-- defold generated message, sent on collision
TRIGGER = hash("trigger_response"),				-- defold generated message, sent on trigger collision
ANIM_DONE = hash("animation_done"),				-- defold generated message, sent when a sprite animation is done
SET_PARENT = hash("set_parent"),					-- defold message to change parent for a game object
RAY_CAST = hash("ray_cast_response"),			-- defold message sent as a response to a raycast
WIN_RESIZE = hash("window_resized"),			-- defold message sent on window resize event
ENABLE = hash("enable"),									-- enable game object or componenet
DISABLE = hash("disable"),								-- disable game object or component

TOUCH = hash("touch"),							-- popular input for mobile control
ANIM = hash("animate"),							-- play an animation (anim)
STOP_ANIM = hash("stop_animation"),	-- stop any animation
FLIP = hash("flip"),								-- flip a sprite
REGISTER = hash("register"),				-- register as a subsriber to receive all messages from a publisher
UNREGISTER = hash("unregister")			-- deregister form subscription list


and then use it after requiring globally once or locally per script, for example:

msgs = require "main.msgs"

like this:

if message_id == msgs.ANIM_DONE then ... end

Don’t name your module msg (it is used by Defold, e.g. msg.url()). Don’t send DISABLE message and then try to do something with a component/go - I spend whole day on finding bug, where I send DISABLE message because I tried to make something BEFORE deleting the object and was wondering why it is not perfomed and object disappers :confused: ANIM_DONE message is only sent after a animation that is not looped - so never wait for a looped animation to be done - use another variable to lock animation and unlock it if needed. WIN_RESIZE is useful when you want to manage different windowed appplication or mobile with horizontal and vertical GUI - you have to manage changes on your own and on this event it is possible. If you have any other useful messages or ideas to unify message system, share it :wink:


To hash or not to hash (SOLVED)

Shallow copying

Again, I’ve bumped into an unusual problem, that when realized could allow to make better programs in Lua. :smiley: I’ve created a table - event, in a Lua module - events and a script with such update function:

function update(self, dt)
	for key,state in pairs(events.event) do
		print(key,[key], state)
		if[key] ~= state then
			print("Event changed!", key, state)[key] = state, state)

I simply wanted to check if anyone, having access to that module, changed one of the entries. If so, I would like to inform subscribers that should be notified about the change -, state)

The problem I had was, that[key] was always the same as events.event[key] (== state if loop). Why? I copied the events.event table to in init: = events.event

And that was a mistake. I didn’t need another variable having this events.event table, but I needed a copy of it, so I can detect the difference and then change it inside if statement.

I found Lua’s shallowcopy and deepcopy implementation
and it solved my problem! :smiley: = shallowcopy(events.event)


I just discovered your book! I’m currently writing some documentation in french about Defold and I’m glad that other people are trying to share their knowledge as well! Good job!



In your book, you said that the builtins folder is read only. Is this still the case as of today?

1 Like


Yes. And any dependency for that matter is read-only :slight_smile:



Perfect. I’ll add it to my doc then!



Yeah!!. It took me a long while to understand that userdata values are referenced by ‘=’ operator and not equated :wink:



Parent-child relation in particle fx

Did you know you can apply different modifiers to specified emitters by making a child-parent relation? :wink:


In that case Radial and Vortex modifiers won’t affects particles from emitter1.



Dynamic instance of watch - helper function

If you need to start a timer in a dynamically changing environment and you need to ensure, that you didn’t started any or overwrite the current ticking or cancel some countdown you can make use of this little wrapper module I wrote:

local WD = {}

local id = 0

function WD.create(delay, cb, repeat_flag)
	local instance = {
		delay = delay,
		cb = cb,
		repeat_flag = repeat_flag or false
	id = id + 1 = id
	instance.stop = function()
		if instance.handle and instance.handle ~= timer.INVALID_TIMER_HANDLE then
			instance.handle = nil
			print("Stopped watch ",
			print("Stopping watch FAILED",, instance.handle)
	instance.start = function()
		instance.handle = timer.delay(instance.delay, instance.repeat_flag, instance.cb)
		assert(instance.handle ~= timer.INVALID_TIMER_HANDLE, "Starting watch FAILED - id:"

	print("Created Watch with delay", delay)
	return instance

return WD


local watch = require ""

local zoom_to_normal = watch.create(2, function(), m.ZOOM, {zoom = m.NORMAL}) end)

-- when input released try to start a timer, which will change the zoom after 2 sec:

-- when input is pressed cancel the current countdown (if any):

I’m using it to because even if I trigger some countdown in one state, the player could quickly change it (move further, so you’re leaving idle state), so I needed to ensure the callback won’t be called when I’m in different state - thus the .stop() method is useful.


How to call a function after a certain amount of time has elapsed? (SOLVED)

Defold recently dropped support of Internet Explorer 11 for HTML5 builds. If you want to run your game on IE11 (but without sounds) then add this code to your engine_template.html right after the <body> tag.

Tested on Defold 1.2.167.


2020-04-16 - Added “resume” implementation for AudioContext for Edge ≤18, Chrome <41, Firefox <40 browsers.


Defold and IE11 incompatibility (issue-4719)

Typical random seed value of os.time() is not the best choice because between subsequent launches the seed value changes too little, resulting in similar random values.

There are better alternatives. The first is to get the seed value from a memory address of a temporary Lua table.

math.randomseed(tonumber(hash_to_hex(hash(tostring({}))):sub(4, 8), 16))

The second is to use the fractional part of socket.gettime()

math.randomseed(100000 * (socket.gettime() % 1))


Lua gamedev libraries

There are great libraries of gamedev focused Lua functions:




split this topic #102

6 posts were split to a new topic: Defold and IE11 incompatibility



Defold Github repos

If you want to find people who already contributed to Defold and made a lot of libraries and extensions, here’s the github topic: