Dynamic img path in BBCode

Godot Version

4.3

Question

I want to be able to dynamically set the img path in a BBCode in my dialogue. Currently, I’m using an array of strings, and each member is a phrase. In some of these phrases, I will display an icon for input (keyboard, gamepad, etc).

So, for example:

Use [img]res://assets/inputs/keyboard_mouse/keyboard_s.png[/img] and [img]res://assets/inputs/keyboard_mouse/keyboard_x.png[/img] buttons to rotate direction.

It will include the icons for keyboard s and x, respectively, in the text displayed. But I need this to be dynamically set because I want to let the player choose which input it will use.

This way, if they decide to change the button for a specific action, the phrase will display the correct icon. How can I do that with BBCode?

You need to make this text a variable and change it with code, then feed it to the RichTextLabel.text

var keyboard_s_path = "res://assets/inputs/keyboard_mouse/keyboard_s.png"
var keyboard_x_path = "res://assets/inputs/keyboard_mouse/keyboard_x.png"

label.text = "Use [img]%s[/img] and [img]%s[/img] buttons to rotate direction." % [keyboard_s_path, keyboard_x_path]
2 Likes

Thanks for the reply. The problem is that I wanted to use a Resource (a custom Dialogue Resource) to generate the dialogues. For example:

In the Content array I have each phrase from a speaker. In this screenshot, you can see me using the [img] with the path to the input button. I would like to try to keep this structure.

I loop through all the dialogue content and display them in the dialogue box.

I’m not sure how can I make it work with variables. It’s possible to maybe use something like str_to_var()?

I don’t see how str_to_var could be useful here.

Allowing users to change control bindings at runtime adds quite a bit of complexity to the project.
If I were you, I would first try to make the control binding changing before I make the dynamic dialogues. I believe working on this might shed more light on how to make your dialogues react to the dynamic bindings.
In case you still have issues then, let me know and we can try to help you achieve your goal.