I am wondering how the tags in GUI text elements work.
I have a text element with “<color=#ff0000>Rune: ”
The red text shows up, but the image does not. I’ve also tried with an atlas and an animation. and I can’t ever get the image to show up.
I’ve started putting the text directly into the text properties box since it will tell me if the rich text is malformed, but that does not seem to be the case.
The tag is a little more complicated than it might seem at first. It doesn’t actually insert/render the sprite itself. Instead, it creates a placeholder in the text layout and reserves the specified space for the sprite.
You can then use label.get_layout_objects() or gui.get_layout_objects() to retrieve the layout objects. You need to iterate through them and look for objects where object.type == “sprite”.
Those objects contain all the information you need, such as x, y, width, height, and the tag attributes (including src and animation). You can then use that information to create/position the actual sprite on top of the placeholder in the text.
For example:
local objects = gui.get_layout_objects(gui.get_node("rich_text"))
for _, object in ipairs(objects) do
if object.type == "sprite" then
pprint(object.attributes)
end
end
So the tag is essentially a way of telling the layout system “reserve a sprite-sized space here” and providing the metadata needed to let your code place the actual sprite there.
Edit:
Obviously, once you have the sprite data, you just need to position a game object containing the specified sprite, creating it with a factory.
I saw the sentence in the documentation about it being a placeholder for any object, but without any additional code in the example I took that to mean that you could embed any object type as a sprite (you can! you just have to do it yourself)
I’ve implemented it so that a sprite is positioned appropriately and it’s working well.
Yeah, I initially thought the tag handled everything automatically and that my sprite would be inserted into the text automatically… But that wasn’t the case, hehe.
I agree that, in my opinion, the documentation could use a bit more clarification regarding sprites inside Rich Text. Many beginners will probably have a hard time understanding how the sprite insertion part works. A little more explanation, and maybe some example code, would make a big difference.
“sprite” immediately makes it clear that the tag is meant to insert a sprite into the text. However, users (myself included) might expect everything to be handled automatically and, like the other Rich Text tags, not require any additional management or setup.
“object” gives the idea of something more generic, with a wider range of possible applications. A user might think, “Okay, I could insert something that moves on its own, or something interactive. This is the tag I need!” In fact, the tag still creates an object with a sprite assigned to it, but that object could also have a script attached to it, allowing it to have more specific behaviours instead of simply rendering a single sprite.
In both cases, providing some additional details about how the tag is meant to be used and what it can be used for would still be necessary to make things clear in the documentation. And of course, including a simple, basic example would be ideal!
The name was picked to put that thought in your mind.
However, until we can actually do that (not sure we ever will be able to) it will remain confusing I guess.