+1 vote

Hi!
I'm having a problem with assigning a function to clickable text inside of a RichTextLabel. My label is bbcode enabled. I'm able to get the text to be underlined and clickable by doing this:

label.bbcode_text = '[url="function_to_call"]Clickable text[/url]' 

But then I get stuck. I know there is a meta_clicked signal that this emits but I can't get it to work. I want function_to_call() to be called when the clickable text between url tags is clicked. What is the correct syntax?

in Engine by (422 points)

You mean something like that?

extends RichTextLabel

func _ready():
    bbcode_enabled = true
    bbcode_text = '[url="function_to_call"]Clickable text[/url]'

func _on_RichTextLabel_meta_clicked(meta):
    function_to_call()

func function_to_call():
    print("Label has been clicked!")

Doesn't work. Also, why would you need this:

func _on_RichTextLabel_meta_clicked(meta):
    function_to_call()

This to me looks like an auto generated function when connecting meta_clicked in the editor. This will not work for me as I am assigning the text from a remote script and the function I want to call is also in that remote script.

OK here's what i ended up doing and it worked:

func _ready():
    label.connect("meta_clicked", self, "label_clicked") 

func some_other_func():
    label.bbcode_text = '[url="do_something"]Clickable text[/url]'

func label_clicked(do_something):
    do something

1 Answer

0 votes

This is a bit old question, but it popped up in search and I didn't find other answers, so...
meta_clicked signal calls the function you connect it to and passes defined url as a parameter as a string (also note, write string without quotation marks). So we need to use that parameter to decide what to call. Thankfully godot has call function, which takes function name as a parameter.
The solution is to connect signal to call function:

extends RichTextLabel

func _ready() -> void:
    bbcode_text = "[url=print_hello]Click me![/url]"
    # This is the crucial bit. connect signal to call function.
    connect("meta_clicked", self, "call")

func print_hello():
    print("Hello world!")

Notice, that any function can be called from text this way, so don't use text from external sources (user input, file)!
If you want to be on safe side, you can make a custom handler function which runs logic in a match statement, for example:

extends RichTextLabel

func _ready() -> void:
    bbcode_text = "[url=print]Print something[/url] or [url=quit]quit[/url]."
    connect("meta_clicked", self, "handle")

func handle(argument):
    match argument:
        "print": print("Hello world!")
        "quit": get_tree().quit()
by (382 points)
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 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 webmaster@godotengine.org with your username.