Godot Version
4.3
Question
howdy folks, wondering if anyone has experience with a similar issue to mine:
I have a resource called item that contains this export variable:
@export_multiline var description: String = “”
In the editor, I set the String to this text:
This is an item " + Global.itemName
When i load the resource in my game, the display is supposed to print the description as: This is an item Iron Sword
The code for that is as follows:
text = item.description
rich_text_label.parse_bbcode(text)
The issue is that the text comes out as: This is an item " + Global.itemName
How can i get the bbcode to properly parse?
If I set the description in code like this:
rich_text_label.parse_bbcode("This is an item " + Global.itemName)
it works, but I am trying to set this in the resource for better usability and functionality.
Any thoughts?
It appears you are simply missing your opening ". Try adding the " to make your string "This is an item " + Global.itemName
Thank you for the response, however, if i set the text in the editor to:
"This is an item " + Global.itemName
It simply prints with the extra block quote at the start.
The bbcode is used for formatting the text with colors and similar, not for inserting variables into it!
What you are doing is you are basically writing an Expression. Which you can do in Godot and it’s very cool. That can be extremely powerful, but it’s annoying to worry about gdscript syntax in text fields. And also check your performance, just to be sure.
var description = '"This is an item" + Global.itemName'
var expression = Expression.new()
expression.parse(description, ["Global"])
var result = expression.execute([Global])
print(result) # This is an item Iron Sword
OR you can do (blind) string formatting and hardcode the variable. This may not be as flexible as you need it to be. This technique is used in localization pretty often.
var description: String = "This is an item %s"
rich_text_label.parse_bbcode(description % Global.itemName)
1 Like