–1 vote

My goal to to be able to use the text editor to pace the value of a variable in a rich text document, without knowing ahead of time, what the value of the string is or how many times it will be used, or in what order with other variables. Is there a way to do this with BBCode or some other techique? I know you can interpolate with %s but you have to know how many times you're using the variable and in what order it will appear with other variables in the text. I can't make that work for my purposes. Is there a way to interpolate a variable on the fly using a tag or other method?

I also can't find any information on how to add bullets to Unordered lists. I can apply the tag, but as far as I can tell, it doesn't do anything to the text by default.

in Engine by (35 points)
edited by

1 Answer

+1 vote
Best answer

This is more of a string formatting problem than something related to RichTextLabel; As long as you change $RichTextLabel.bbcode_text, it will reflect what you give it. As such, you can pass it a string with an arbitrary number of fields.

# Call a fictional method that returns something like:
# "Hi, {player}, welcome to {place}, I am {speaker} and I invite you to [b]{action}[/b]. Do it, {player}, or else!"
var dialogue = data_origin.get_string()

# Also get the values:
# {
#   "player": "x",
#   "place": "the morgue",
#   "speaker": "Dr. Bones",
#   "action": "lay on this bed right now"
# }
var fields = data_origin.get_fields()

$RichTextLabel.bbcode_text = dialogue.format(fields)

Regarding your second question, I believe you will have to insert the bullet character (• or U+2022) by code. Ha, 2022!

by (1,295 points)
selected by

Does this formula use a dictionary? In other words, can I create a dictionary and pass it to the format string method to make this work?

Thanks for your answer, I didn't realize this was possible!

Yup, it uses a dictionary, but it can also work with an array, if you find it more appropriate to your use case.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.