Skip to content
The Chat Archive
gpt-4-0125-preview· OpenAI WildChat-4.8M (AI2)

Change label font runtime (Defold engine)

  1. User

    Change label font runtime (Defold engine)

  2. Assistant

    To change the label font at runtime in a Defold game engine project, you need to use the gui.set_font function. This allows you to dynamically switch the font used by GUI text nodes.

    First, ensure you have the fonts you intend to switch between added to your GUI. In the Defold editor:

    1. Open your GUI file.
    2. In the Outline pane, select the root node (gui).
    3. In the Properties pane, find the Fonts section.
    4. Add your fonts by specifying unique Ids for them and selecting the actual font resources. Remember these Ids as you will use them to switch fonts in your script.

    Now, to change the font of a label at runtime, you can follow these steps in your GUI script (*.gui_script):

    Step 1: Reference the Text Node and Fonts

    First, you need to get a reference to the text node you want to change the font of. You should also note the IDs of the fonts you added to the GUI.

    Assuming you have two fonts added with IDs font1 and font2, and a text node with an ID of text_node.

    Step 2: Scripting the Font Change

    In your script, when you want to change the font of the text node, use gui.set_font.

    Example on how to switch to font1:

    local my_text_node = gui.get_node("text_node") -- Get the text node by its ID
    gui.set_font(my_text_node, "font1") -- Switch to font1
    

    To switch to font2, you would just change "font1" to "font2" in the gui.set_font function call.

    Complete Example

    function init(self)
        self.my_text_node = gui.get_node("text_node") -- Get the text node at initialization
    end
    
    function on_input(self, action_id, action)
        if action_id == hash("change_font") and action.pressed then
            if self.current_font == "font1" then
                gui.set_font(self.my_text_node, "font2") -- Change to font2
                self.current_font = "font2"
            else
                gui.set_font(self.my_text_node, "font1") -- Change back to font1
                self.current_font = "font1"
            end
        end
    end
    

    In this example, when the input action with the ID change_font is triggered (and pressed), it toggles the font of the label between font1 and font2.

    Note

    Make sure action handling is set up in your input method for the script, and you have registered the input actions (change_font in this example) in your game’s input bindings.

    This basic example shows how to change label fonts at runtime. You might need to adjust it based on your project's complexity, like handling different GUI scenes or dynamically created text nodes.

  3. User

    I need label component

  4. Assistant

    Changing the font of a label component at runtime in Defold uses a different approach than manipulating GUI nodes. For label components, you'll work within a game object context, rather than a GUI context.

    To change the font used by a label component at runtime, you need to use the label.set_font function. Before proceeding, ensure you have the font resources ready and properly set up in your Defold project.

    Here's how you can dynamically change the font of a label component within a game object:

    Step 1: Preparing Your Fonts

    1. Add Font Files: Make sure you've added the font files you want to use into your Defold project.
    2. Create Font Resources: For each font, create a font resource (.font file) in your project. You do this by right-clicking in the Asset panel, choosing New... > Font, and then configuring each font resource to use your font file. Give each .font resource a unique name so you can reference them in your scripts.

    Step 2: Configuring the Game Object

    1. Label Component: Ensure your game object has a label component. You can add a label component by right-clicking the game object in the Outline panel, choosing Add Component > Label, and setting up its properties.
    2. Referencing Fonts: In the properties of the label component, you can only specify one font. However, you'll switch fonts dynamically in your script, so ensure your additional .font resources are accessible.

    Step 3: Scripting the Font Change

    You'll now write the script to change the font of your label component at runtime. Suppose you have two font resources named font1.font and font2.font, and your label component is attached to a game object named my_label.

    Add a Script to Your Game Object

    1. Create a Script: Create a Lua script file (.script) if you haven't already, by right-clicking in the Assets panel, selecting New... > Script, and naming it appropriately.
    2. Attach the Script to Your Game Object: Drag this script onto your game object in the Outline panel.

    In your script file, you'll write Lua code that looks something like this:

    function init(self)
        -- Initially set to the first font
        self.current_font = "font1"
    end
    
    function on_input(self, action_id, action)
        if action_id == hash("toggle_font") and action.pressed then
            -- Toggle font on some input action
            if self.current_font == "font1" then
                label.set_font("#my_label", "font2") -- changing to font2
                self.current_font = "font2"
            else
                label.set_font("#my_label", "font1") -- changing back to font1
                self.current_font = "font1"
            end
        end
    end
    

    Make sure to replace "#my_label" with the actual id of your label component if it's different. Also, ensure "font1" and "font2" match the names of the font resources you intend to use.

    Step 4: Configuring Input

    To trigger the font change based on some input (like a button press), make sure you've set up the corresponding input bindings in your game.project file, and your script's on_input function is prepared to handle it.

    For example, if you're toggling fonts with a key press, you would:

    1. Go to your game.input_binding file.
    2. Add a new input with the key_trigger type.
    3. Name it accordingly (e.g., toggle_font) and assign a key.

    Make sure your project is correctly set up to trigger input actions for this script to work. This example demonstrates toggling between two fonts, but you can adjust the logic to fit your game's needs, such as changing fonts based on game events or player achievements.