The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+1 vote

I'm trying to use signals connected by code. In my case here's an example:

#Here, the value of $Textline.text is "bar"
func _ready():

     print("the message is" + str(message))

Right now if I click the button, the function will print "bar", everything it's normal, but, if I change the value during runtime, to "foo", and click the button, the fuction will print "bar" again. As it took the value that the $Textline.text when the _ready() method was called.

So what I need is a way to reference the property and not the value. So when the signal gets triggered, it will take the current value and not the one that was there in the _ready call.

The example is just to give some context, my current project needs, to reference that property and I can't simply put it inside the fuction as it has to be dynamic. I don't want to have extra variables running around to pass a dynamic value, because it's quite probable that there is a way to do it but I don't know how.

Godot version 3.2.3
in Engine by (108 points)
retagged by

2 Answers

+3 votes
Best answer

You could use an Expression.

#Here, the value of $Textline.text is "bar"
func _ready():
    var expression =
    var expression_text = 'get_node("Textline").text'
    var err = expression.parse(expression_text)

    if err == OK:
                'Failed to parse "%s" as an expression. Error: %s'
                % [expression_text, expression.get_error_text()]

func print_textline_text(expression):
    var message = expression.execute([], self)
    if expression.has_execute_failed():
                "Failed to execute expression %s."
                % [expression]
        print("the message is " + str(message))
by (371 points)
selected by

Okay it took me some days and many re-reads to understand what was happening since the docs are really incomplete with Expressions. But wow, this is amazing. I need to learn more about this, it's so powerful. Thanks!

0 votes

You should use NodePath type for this signal. Check it in documentation and You will learn how to contruct nodepaths leading to properties.

Why won't You just bind value while emitting signal instead of while connecting ? If you would pass text on emitting it will always be actual

by (8,188 points)

How do I emit the signal if it is a button? Isn't it automatically emitted without the chance of passing binds?

It is, there are several ways around this. I usually do one script for buttons and make custom button press. Still it requires scripting the button, but I never knew about expression class as in other answer below, It sounds like more elegant sollution.

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.