Is there wrong in my script?

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

Hello ,
I have this script called Button.gd ,
and it’s like this:

extends Button
func _ready():
if Button.pressed() == true :
set_text(“Press here to play.”)

So my goal is to show the text above , but when I play it shows this error:
(Invalid call. Nonexistent function ‘pressed’ in base ‘GDScriptNativeClass’.)
What’s the solve?

:bust_in_silhouette: Reply From: timothybrentwood
func _ready():
    self.connect("pressed", self, "_on_self_pressed")

func _on_self_pressed():
    self.set_text("Press here to play.")

When you press the Button, the pressed signal will be emitted causing your Button to call the _on_self_pressed() function. This is because that was the function connected to the pressed signal via the connect() function called in the _ready() function.

:bust_in_silhouette: Reply From: Wakatta

You’re trying to call a function and pressed is actually a variable.
Also pressed is of type bool so doing a comparison check is kinda redundant.

Change to

if Button.pressed:
    set_text("Press here to play.")

Or it’s getter func

if Button.is_pressed():
    set_text("Press here to play.")