Welcome to this course on how to create your own graphical interface in Defold. I designed it to help you start from a beginner level and gradually reach an intermediate level. We’ll follow a clear, step-by-step progression to get there.
There are so many things I want to show you in this module! You’ll learn how the GUI system works, how to create your own graphical elements, the basic functions you need to know, and even get an introduction to metatables and proxy components to load specific collections when needed.
Before we continue, you need to know that, just like in the tilemap course, we will focus only on Defold’s built-in tools. We will not use Druid.
My goal is to teach you everything in a “vanilla” way, so you really understand how the GUI system works. Later, if you want to go further, you will be able to use frameworks like Druid without any problem.
Understand the Defold GUI system
Understand how the GUI works
Introduction to the different node types (Box, Text, Pie, Template)
Organizing your interface with root nodes and anchors
A GUI is a system that works independently from the game world and allows us to create graphical interfaces. Unlike game objects, GUIs are rendered separately and are not affected by the game camera.
Important characteristics:
GUIs are attached to game objects and created from .gui files.
Each GUI has its own script.
They work independently from the game world.
Begin by downloading the project file. It already includes some images and settings : download
Inside the assets folder, you will find images, an atlas containing all the necessary graphics, and a few fonts.
Create a new folder in the asset browser and name it GUI.
Inside this folder, create a new .gui file and name it simple_gui.
A window should automatically open, showing the usual interface areas: the view area, the outline with its different “folders”, and the properties panel.
The panel properties are showing the properties of our GUI because it currently has the focus. We have different parameters, but the ones that interest us are the script properties. This is where we will attach our future script.
Let’s go back to the outline area and talk about nodes. Nodes are where we create our different components, like images, buttons, checkboxes, sliders, panels, menus, etc.
To add a node, right-click on the folder and select Add.
As you can see, there is no button option or anything like that. We only have box, pie, text, template, and particleFX. There is no ready-made component for us like in Godot or Unity. We have to build them ourselves!
Let’s start by using a box to begin our adventure!
A box node is a rectangle used for backgrounds, button images, and more. It can display a texture from an atlas and supports 9-slice scaling. You will use it a lot!
Let’s create our first node. Right-click the Nodes folder and add a new box. This box will be the root of our interface. It will contain every other node, act as their parent, and make it easier to transform and control the whole GUI at once.
You should now see a white box appear.
In the Outline, click on the box node to display its properties. In the Properties panel, change the Id of the box and name it "ROOT".
The root node should match the size of the game and be centered on the screen. Set its Size to 960 x 640 and its Position to 480 x 320.
Then, uncheck the Visible property so the root box does not render, but can still be used to group and control the rest of the interface. Finally, set its Adjust Mode to Fit.
Ok, our root is set up. Let’s add a background so we can experiment a bit. Add a new box node, name it background_main, and set its size to 960 x 640. You should now see a white rectangle filling the whole game area.
Now we need to assign an image texture to this box. To do this, right-click on the Textures folder in the Outline, then select Add ▸ Textures and choose interface_atlas.
Then go back to the background_main node. For the Texture property, choose simple_background from the list.
Now let’s see how our GUI looks in the game. In the Assets browser, find main.collection and open it.
You should see an empty collection for now. Add a new game object and attach the GUI to it:
Right-click in the collection outline and choose Add Game Object, then name it "gui".
Right-click the gui game object, choose Add Component File, and select simple_gui.
You should now have this setup in your main.collection: one gui game object with the simple_gui GUI component attached.
Run the game with F5 and try resizing the window. Because the root node uses Fit as its adjust mode, your GUI should always stay perfectly scaled inside the window.
Now let’s switch the root node to Stretch, and also set background_main to Stretch so you can clearly see the difference.
If you leave the background in Fit, it will keep its proportions inside the root node instead of stretching to fill the available space.
As you can see, the background now resizes with the window.
Now let’s switch the root node to Zoom. You can set the background’s adjust mode to either Fit or Stretch if you want, both will let you see the difference.
Now, with the Zoom mode, our background seems to adapt its size proportionally to the window while trying to cover all the available space.
This is why it often makes more sense to set the root node to Stretch, so your GUI stays responsive on any device and adapts correctly on mobile, tablets, and more.
In my case, I think Fit is more intuitive for beginners at first, because your GUI always stays in the game aspect ratio you set, making it easier to manage.
But for now, let’s keep it on Stretch.
Let’s talk a bit about anchors.
Anchors help make your interface responsive. They define a reference point, for example the top-left or bottom-right corner, that Defold uses to calculate where a GUI component should stay on the screen.
Without anchors, your interface can break when the screen size changes, and components may drift away from where you originally placed them.
Before showing you how to create your own anchor, I’ve prepared a small example so you can see exactly what it does.
Go to the main collection, delete the simple GUI inside the game object, and add anchors.gui.
You should have something like this. Launch the game and try resizing the window.
As you can see, the green element has an anchor and stays in the right place, but the red element does not because it doesn’t have any anchors and is just placed in Fit mode. Each blue square represents where an anchor was placed.
Go to simple_gui, add a new box node under the root, and name it N_ANCHOR. Then change these properties:
Position to (0, 320, 0) to place it at the top border
Size to (32, 32, 0), this defines the size of our margin
Visible property to false
Adjust mode to Stretch
Pivot point to North, since this will be the north anchor
You should have something like this.
👉
As an exercise, try creating NW_ANCHOR, NE_ANCHOR, S_ANCHOR, SW_ANCHOR, and SE_ANCHOR. Don’t hesitate to check the example I provided in the file!
Now, what’s the benefit of this and how do you use it?
It’s simple: just add your node to the anchor and position it where you want.
For example, add a box node under N_ANCHOR and set its position to (0, -32, 0). Set its pivot point to North, and give it the button_pressed texture.
Now, if we launch the game, we should see this.
The GUI system uses layers to control how nodes overlap in your interface. Otherwise, all nodes are rendered in the order they appear under the “Nodes” folder.
Right-click on the Layers folder in the Outline and choose Add Layer. The first layer you create will be the bottom layer, and any new layers will be added above the previous ones.
Keep in mind that any node without a layer will appear behind nodes that have one assigned.
So let’s create three layers. Name the first one "general_bottom", the second "general_middle", and the last one "general_top".
Then let’s assign the background_main node to general_bottom, and assign our button to general_middle.
Next, add a Text node inside the button node. You’ll probably see an error because the font property isn’t set. Since we haven’t added any fonts to this GUI yet, let’s add one.
(And just as a note: I renamed the box node to button.)
So, right-click the Fonts folder in the Outline, choose Add > Font, and select kenny_future. It’s a font resource I’ve already prepared for you. If you don’t have it in your project, you’ll need to create it first.
And voilà! 🎉 Thanks to the layers, the renderer groups nodes so that each one is drawn within its own layer. This lets us control exactly how nodes overlap.
I have to admit, in this simple example, setting up a layering system wasn’t really necessary. Our nodes are already listed in order in the folder, so the visual result would be the same without layers. However, it’s important to understand that a layer system exists and how it works. By grouping nodes into layers, you can also improve draw call performance in more complex GUIs.
And that’s the end of this first part. In the next lesson, you’ll create a simple interactive button with a GUI script. You will learn about 9-slice scaling and work with GUI nodes using functions like gui.get_node(), gui.pick_node(), gui.animate(), and more. You’ll detect when the mouse is over a node, handle click and release events, and turn the button into a reusable template.
As always, you can download the final version of this course if you want: download