Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon
24. September 2018

Defold is a free 2D Lua powered game engine made by King.  If you are interested in learning more about the Defold game engine, be sure to check out our existing Defold Tutorial Series, and stay tuned for our upcoming Defold Crash Course, currently available in preview form for Patreons.  The 1.2.137 release includes a new angular velocity mode for particle systems, new build options and the removal of Linux and OSX 32 bit version.

Full details from the release:


  • DEF-1593 - Added: New particle orientation mode for angular velocity.
  • DEF-2477 - Added: Engine build variants - debug, release and headless in Bob and Editor bundle dialogue
  • DEF-3487 - Changed: Removed 32 bit Linux+OSX engines from bob.jar.
  • DEF-3496 - Fixed: Missing iPad Pro 10.5" launch images.
  • DEF-3047 - Fixed: Log spam on Android 8+ devices related to CPU profiler.
  • DEF-3494 - Fixed: http.request() failed under certain conditions.
  • DEF-3456 - Fixed: Cloned spine nodes were not getting correct skin and animation.
  • DEF-1966 - Fixed: Collection proxies did not handle input consumption correctly.
  • DEF-3489 - Fixed: X11 context initialization for the Linux engine.
  • DEF-2929 - Fixed: on_input in GUI was missing accelerometer fields.
  • DEF-3464 - Fixed: Bug with stored notifications were not received on Android.
  • DEF-3479 - Fixed: Added API check before attempting to login with Facebook.


  • DEFEDIT-1420 - Changed: Long-running tasks such as bundling, building and saving are now performed on a background thread.
  • DEFEDIT-1420 - Fixed: The editor will no longer overwrite external changes when saving if it has not detected them.
  • DEFEDIT-1420 - Fixed: Broken library URLs are now reported correctly when bundling or building for HTML5.
  • DEFEDIT-1420 - Fixed: Sometimes the progress bar could disappear, leaving only the percentage-label visible.
  • DEFEDIT-1427 - Fixed: The Property and Outline panels could stop redrawing while a Particle FX or animation was playing.
  • DEFEDIT-1430 - Changed: Visibility Filters now have separate toggles for components inside or outside of GUI scenes.
  • DEFEDIT-1432 - Fixed: Improved progress reporting in several areas.
  • DEFEDIT-1437 - Fixed: Fixed occasional “File Not Found” errors when returning to the editor after switching branches.
  • DEFEDIT-1439 - Fixed: Updated autocomplete definitions for the Code Editor.
  • DEFEDIT-1440 - Fixed: Bundling a project with native extensions reported Ready too early.

GameDev News

6. September 2018

Recently we have broken down lists of 3D game engines that use the C++ language or C# language as a programming language for game logic.  Today we are going to look at game engines using the Lua programming language.  This doesn’t mean the game engine was created using the Lua language, instead we are covering engines that can be scripted using Lua.  In this list, unlike the previous two, we are going to include both 2D and 3D game engines in the list.  Additionally, for 2D engines, we will also include frameworks that don’t necessarily include level editors.

2D Lua Engines:

Defold (Learn More)


LÖVE (Learn More)


Raylib (Learn More)


3D Lua Engines:

Spring RTS

GameGuru (Learn More)



Lumberyard (Learn More)

Roblox Studio

Urho3D (*Needs to be enabled)

Stingray|3DS Interactive


CryEngine (Deprecated)

Cocos2D (Undocumented)

PolyCode (Seemingly abandoned)

Marmalade (Deprecated)



21. February 2017


Somewhat delayed from the rest of the Defold Engine tutorial series, I realized there was a major gap in subjects we covered... GUI programming.  Defold actually has some really solid UI functionality built in, and in this tutorial we are going to look at the basics of handling a UI in Defold. Well very conventional in it’s approach, if you’ve gotten accustomed to the Defold way of doing things, you will find Defold’s approach to handling UI remarkably consistent to the way you do just about everything else using the Defold game engine.


As always, there is an HD video version of this tutorial available here.


In this tutorial we are going to implement the simplest of UI, but all of the concepts of a much more complicated interface can easily be extrapolated from what we cover here. We are going to create a UI that pops up when the user hits the ESC key, dimming the existing screen, showing the UI, handling input, then un-dimming the UI.  There is a ton more UI functionality available in Defold, but it should be fairly easy to pick it up once you’ve got the basics down.  So without further ado, lets jump right in.


A UI in Defold consists of two different file types, a .gui and a .gui_script file.  A gui_script file is just a traditional lua script file, but has access to the gui namespace.    Let’s take a step by step look at creating a UI that appears when you hit the ESC key and handles a simple button being clicked.


First we need a font, drag any applicable TTF file to your project.  I personally created a folder called MyFont and dragged it there.  Next I created a Font file in the same folder.



Next open the newly created Font file and select the ttf file you just imported.  Please note that the filter is broken and you should manually type *.ttf to locate your font.


After selecting the font I also increased the size to 30pts.  This is aesthetic and purely optional.


Now that we have a font selected, we need to define an Input map.  I fully describe the process here for more information on how Input Maps work in Defold.  This are the bindings I created:



Next we create our gui file.  Simply select Gui File:



We can use the Gui File to arrange the controls that make up our UI.  Here are the options:



In this simple UI we are simply going to create a button by creating a box then adding a text label to it.  We will also create a label telling the user to click the button.  First we need to set up our font that we created earlier.  In the GUI editor, right click Fonts and select Add Font:



When prompted, select the font you just created:



Now right click Nodes and add a Box.  Once created it can be positioned by hitting W then moving manually.



Next right click the newly created box node and create a child Text field:



Child nodes automatically inherit the position of their parents.  With the Text node selected, lets set it’s font and text, like so:



I also created another Text field telling the user to click the button.  This one in the root of the GUI hierarchy and not parented to the box.  You’re outline should look something like:



While your Gui layout should look something like:



Now that we have a gui file, let’s right a script that will display it.  In our main.collection I simply create a new script and attach it to the default logo objecct.



Now of course we need to add the UI to our game object.  Create a new root level GameObject named UI, then add component from file and select our gui file:



So now you main.collection should look something like:


Now we enter the following code for main.script:

function init(self)
	-- we want focus and to hide our UI until needed".", "acquire_input_focus")"UI","disable");

function on_message(self, message_id, message, sender)
	-- Wait for a message from the UI layer that the UI has been dismissed
	-- un-dim our sprite
    if(message_id == hash("DONE_UI")) then

function on_input(self, action_id, action)
	-- If the user hits the ESC show the UI and dim our sprite
    if(action_id == hash("ESC") and action.released) then
    	-- UI needed now, reenable"UI","enable")


This code does a couple things, first on input it tells Defold we want to get input messages.  We also start out by disabling the UI, by sending it the built-in message disable.  When the user actually hits the escape key, we send a message to re-enable the UI layer.  We also dim the logo sprite so it’s not visually in focus when the UI is active.  Also note we wait for the DONE_UI message to undim our sprite.  This is sent by the UI script, which we will create now.


If you select your .gui file, in the properties you will notice there is a setting for Script.



There is a special kind of script, .gui_script, that is used to control gui objects, the Gui Script File.  Let’s create one in the same folder as our .gui file:



This is a standard lua script, but it has access to the gui namespace.  Once you’ve created your gui_script, set it as the script for your gui file.  Now enter the following script:

function init(self)
	-- We want input control.  AKA, pump input to the UI".", "acquire_input_focus")

function on_message(self, message_id, message, sender)
	-- Expect to be enabled by main when needed.  Acquire focus and set text back to click me
	if(message_id == hash("enable")) then".", "acquire_input_focus")
		gui.set_text(gui.get_node("text"),"Click Me")

function on_input(self, action_id, action)
	-- handle left clicks.  On left click, see if click was within box
	-- if so change our text (this wont actually be seen), disable ourself and pass a message back
	-- to logo that we are done so it can undim itself
	if(action_id == hash("LEFT_CLICK") and action.released == true) then
		local box = gui.get_node("box")
		if(gui.pick_node(box,action.x,action.y)) then
			local text = gui.get_node("text")


This code waits for the enable message then sets input focus so the gui can receive input messages from Defold.  It also illustrates how you could change the text of a component within the gui.  The most important logic is in the on_input event handler.  We wait for the LEFT_CLICK input.  We then check to see if the click was within our box, if so we set the text of our control ( which is somewhat pointless as it’s about to be hidden! ) to “Clicked”, disable our self, release input focus then send the message DONE_UI back to main.script.  Now if you run the code:



Of course we only scratched the surface of what you can do in a Defold gui, but that should certainly get you started!


The Video


3. October 2016


Welcome to the next part in our ongoing Defold Game Engine tutorial series.  In this tutorial we are looking at working with the integrated Box2D physics engine built into the Defold engine.


As always there is an HD video version of this tutorial available here or embedded below.


The Defold engine makes it extremely easy to work with physics but is rather minimal in it’s implementation.  Let’s jump right in.


Creating a Collision Object


The heard of implementing physics in Defold is the Collision component.  This is composed of two part, the Collision Component that describes the physical attributes of a game object, and the Physics shape, which defines it’s dimensions.  I start off by creating this simple Gameobject, the image used in the sprite is 128x128 pixels:



Now we simple add a new physics component to the game object.  Right click the Game Object in the Outline, select Add Component, then select Collision Object:



Here are the default properties of a Collision Object:



Type is important and we will cover that shortly.  Mass is the weight of the object relative to dimensions, friction is how one surface reacts to another ( think traction ), restitution is the “bouncy-ness” of an object, linear damping and angular damping determine how motion of the object degrades over time, locked rotation determines if the physics simulation will rotate the attached game object or not.  Group and Mask are used to determine which collision objects interact with other collision objects.  Objects in the same group will collide, while objects not in the same group, or if that particular group isn’t added to the Group value ( comma separated ) will not interact.  So think of Group as the physics identity or type(s) this object belongs to, while Mask is the groups that object will interact with.


Next we need to define the shape of our physics object.  Simply right click the Collision Object and select Add Shape:


Next pick Box:



In the properties, set the Width and Height to match the size of our source image.  Depth is the value along the z-axis and can be left at the default value in most cases.



Now that we have our Collision object define, simply create a couple instances of our game object in the scene and the results are immediately demonstrated.



You probably didn’t want the boxes just dropping out of the world now do you?  So we need to create a physics body for the “floor”.  We can easily do this by creating a new Game Object in main and adding a physics body to it.  Like so:




The problem here is, our newly created floor will be effected by gravity, just like every other object in the scene!  This is where the Type of the Collision Object becomes important:



So far we have only used the default, Dynamic.  A dynamic object is a fully simulated physics object.  It will take part in the simulation, will be part of the simulation and will be affected by the simulation.  Static is what we want in this situation.  A static object will be part of the simulation, but will not be effected by it.  This is your proverbial unmovable force!  So other objects in the simulation will be able to hit the collision object and they will be affected by the collision, but the static object will not be changed in any way.  Kinematic objects are special objects that are generally moved directly by the character or code but also take part in the physics simulation and will be affected by it ( think that players sprite for example ).  Triggers on the other hand are simply shapes in the world that can detect a collision and can be hit, but wont have any affect on anything in the world.  They are often used to detect movement, for example the goal area in an air hockey game.


Now with our floor object set to static, our updated results!



So, how exactly do we control global physics properties, such as gravity?  These settings are available in the .project file:



Physics and Code

Now that we know how to set up a physics simulation, let’s look at how to deal with it in code.  For we are going to look at how you respond to a collision in code.  As you might be able to guess, messages are used.  Let’s look at a simple program that responds when a collision occurs.  Attach a script to your box object and add the following code:


function on_message(self, message_id, message, sender)
    if message_id == hash("contact_point_response") then"@render:","draw_line", { 
    		start_point = vmath.vector3(message.position.x -40, message.position.y - 
    		40, 0),
    		end_point = vmath.vector3(message.position.x +40, message.position.y + 40, 
    		color = vmath.vector4(1,0,0,1) })"@render:","draw_line", { 
    		start_point = vmath.vector3(message.position.x -40, message.position.y + 
    		40, 0),
    		end_point = vmath.vector3(message.position.x +40, message.position.y - 40, 
    		color = vmath.vector4(1,0,0,1) })	

This code simply listens for the contact_point_response message which is sent when a collision occurs with the attached game object.  When such a collision occurs, this code draws an X centered about the collision point by sending the draw_line message to the render subsystem.

There is a great deal of information about the collision passed along in the message parameter:


As you can see, information on what object was collided with, how fast, groups, velocity, mass etc are all available in the contact_point_response message.


On final topic to cover is occasionally you are going to want to be able to apply force directly to a physics object using code.  This is also easily accomplished using the following code:"#collisionobject", "apply_force", { 
    		force = vmath.vector3(0,5000,0),
    		position = go.get_world_position()})

Again using Defold’s messaging system, we send the apply_force message to our Collision Object component.  This applies 5000 newton of force along the Y axis.  Added inside our collision handler, it makes for some interesting results!



The Video


12. September 2016


Welcome to the next part in our ongoing Defold Game Engine tutorial series.  Today’s tutorial is going to show an area where the Defold engine really shines, creating 2D tile based levels.  This way of creating levels is to paint levels in a data structure called a tilemap, which itself is a collection of tiles.  A tile is simply a portion of an image that can be drawn over and over, much like you could use Lego blocks to create a castle.


As always there is an HD video version of this tutorial available here.


For this tutorial we need a tileset to work with.  This is simply an image composed of tiles, in this case with no spaces or padding between them.  I personally am using the tiles from in the Free Platformer Game tileset.  I have taken these individual tiles and merged them together into a single image you can download below.  (Be sure to click the image to get the full sized version.  In this example size does matter! ).


tilesThis is a 1024x512 pixel image composed of 64x64 pixel tiles.  Of course you can use whatever image you want, just be aware that if there is padding or different sized tiles your version is going to be different.


As always I assume you have gone through the prior tutorials in the series.  Alright, now that we are set up, let’s get going.


Creating a Tile Source


First things first, we need to create a directory for our level, tiles, images, etc.  I simply created a folder called level.  Inside this folder I created a directory called images and dragged the image above into that folder, like so:



Now we need to create a tile source.  You can think of this as the palette or selection of tiles we are going to paint with.  Don’t worry, it’s exceedingly easy.  Simply right click level, select New->Tile Source File



Called it tiles.tilesource like so:



Now in Outline make sure Tile Source is selected:



Now in the Properties panel, select the image file we added earlier for the Image property, then set Tile Height and Tile Width to 64.  Obviously if you used a different image you may have different settings here.



Now in your viewport you should see your tiles, with the grid drawn nicely around them like so:



All done creating our tilesource.  Make not of the collision property... we will come back for that in the next tutorial.  Make sure you save.


Creating a Tile Map

Now it’s time to actually create our tile map.  Once again right click the level folder and select New->Tile Map File.  Name this one map.tilemap:



Now in Outline make sure Grid is selected:



Now in the properties, pick out tilesource:



Now select layer1 in the outline window and we can begin painting!


In the viewport use the spacebar to bring up the tile source, then left click to select the active tile.  Now hit space again to hide the tile source and paint using the left mouse button.  You can also use the right mouse button to select multiple painted tiles to easily clone and reproduce compound tiles.



If you need to paint tiles on top of other tiles, simply create another layer, right click Layers and select add:



The z order determines the draw order of tile layers:



Right click an empty space then left click to delete a tile if required.  Save before continuing on!


Using a Tile Map

Finally it’s time to put our newly created tilemap to use.  This is also simple.  Open your main.collection in the editor then add a new game object just like we did in the past.  Right click the game object and select Add Component From File:



Select your .tilemap file, and voila:



The Video

Design Programming

AppGameKit Studio

See More Tutorials on!

Month List