A Label component renders a piece of text on screen, in game space. By default it is sorted and drawn with all sprite and tile graphics. The component has a set of properties that governs how the text is rendered. Defold’s GUI supports text but it can be tricky to place GUI elements in the game world. Labels make this easier.
To create a Label component, right click the game object and selecting Add Component ▸ Label.
(If you want to instantiate several labels from the same template you can alternatively make a new label component file: right click a folder in the Assets browser and select New... ▸ Label, then add the file as component to any game objects)
Set the Font property to the font you want to use and make sure to set the Material property to a material that matches the font type:
To use a Vector font, follow the Vector font setup, assign the .font resource to the Label’s Font property, and set the Label’s Material to /builtins/fonts/label-vector.material. Use Font Size to control the displayed size.
For color emoji, also complete the COLRv1 fallback and bundling setup. The example below assumes a Label with the id label and a script on the same game object. Leave the Label’s Text empty to display the greeting only after its glyphs are ready:
function init(self)
local text = "Hello 😀 🍎 🚀"
local font_resource = go.get("#label", "font")
-- Add the fallback once for this shared font resource.
font.add_font(font_resource, "/fonts/NotoColorEmoji-Regular.ttf")
font.prewarm_text(font_resource, text, function(self, request_id, result, err)
if result then
go.set("#label", "text", text)
else
print("Could not prepare label text:", err)
end
end)
end
The ordinary letters use the original font, and the emoji use the added color font. If other Labels or GUI text nodes share this font resource, initialize the fallback in one place and reuse it.
Apart from the properties Id, Position, Rotation and Scale the following component specific properties exist:
Note that the default material has shadow rendering disabled for performance reasons.
The Blend Mode property defines how the component graphics should be blended with the graphics behind it. These are the available blend modes and how they are calculated:
src.a * src.rgb + (1 - src.a) * dst.rgbsrc.rgb + dst.rgbsrc.rgb * dst.rgbsrc.rgb - dst.rgb * dst.rgbBy setting the Pivot property you can change the alignment mode for the text.
Center, North or South, the text is center-aligned.West modes, the text is left-aligned.East modes, the text is right-aligned.You can manipulate labels in runtime by getting and setting the label text as well as the various other properties.
textstring). Available through go.get() and go.set().colorvector4)outlinevector4)shadowvector4)scalenumber for uniform scaling or a vector3 for individual scaling along each axis.sizevector3)
function init(self)
-- Set the text of the "my_label" component in the same game object
-- as this script.
go.set("#my_label", "text", "New text")
local text = go.get("#my_label", "text")
print(text) -- New text
end
label.set_text() and label.get_text() are deprecated in favor of the text property. The old functions remain available for compatibility. The old setter queues a message, while go.set() updates the text immediately.
function init(self)
-- Set the color of the "my_label" component in the same game object
-- as this script. Color is a RGBA value stored in a vector4.
local grey = vmath.vector4(0.5, 0.5, 0.5, 1.0)
go.set("#my_label", "color", grey)
-- ...and remove the outline, by setting its alpha to 0...
go.set("#my_label", "outline.w", 0)
-- ...and scale it x2 along x axis.
local scale_x = go.get("#my_label", "scale.x")
go.set("#my_label", "scale.x", scale_x * 2)
end
The game.project file has a few project settings related to labels.