Insert variable value in bbcode rich text

Godot Version

godot-4

Question

I’m trying to add a live readout for debugging something and probably things like item pickup messages later on in an fs game I’m working on. how can I insert a variable’s value into rich text? currently my code looks like this :

curShot is the variable I’m trying to show, but it’s just showing the variable name, not the value

	text.bbcode_text = "[wave amp=50.0 freq=5.0 connected=1]{curShot}[/wave]"

Can you do

	text.bbcode_text = "[wave amp=50.0 freq=5.0 connected=1]" + curShot + "[/wave]"
1 Like

You can do this

text.bbcode_text = "[wave amp=50.0 freq=5.0 connected=1]{curShot}[/wave]"
text.bbcode_text = text.bbcode_text.replace("{curShot}", curShot) 

Or this

text.bbcode_text = "[wave amp=50.0 freq=5.0 connected=1]%s[/wave]" % curShot

I think those are all valid solutions. I don’t usually have to deal with concatenation and stuff so I’m a little rusty. I ended up using the % one because it seemed the most editable. thanks

1 Like

actually ended up with this solution so I can have multiple variables in one string more easily

	text.bbcode_text = "[wave amp=50.0 freq=5.0 connected=1]"+str(curShot)+"[/wave]"

I think for multiple variables, you could also go with
text.bbcode_text = "[wave amp=50.0 freq=5.0 connected=1]%s and %s[/wave]" % [curShot, curShot2]

I like the simple concatenation that i proposed but just pick whatever feels most comfortable and convenient for you :+1: