Want to use BBCode to place a variable an unknown number of times thorough the text

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By monsterousoperandi

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.

:bust_in_silhouette: Reply From: skysphr

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!

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!

monsterousoperandi | 2022-03-27 01:44

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

skysphr | 2022-03-27 13:44