0
\$\begingroup\$

I'm still getting confused as how to call a function from another script on another scene in Godot 4.x. In my example below, I want to call print_something() on menu.tscn from main.tscn

On main scene we have:

# main.gd
# main.tscn
extends Node2D

func _ready():
    print_something("Hello") #works on main

func print_something(astring):
    print(astring)

On menu scene we have:

# menu.gd
# menu.tscn
extends Node2D
signal mysignal
@export var message := "Gwen Stefani is bananas!"
# main.print_something(message) # Unexpected "Identifier" in class body.

When trying to connect the signal node I'm not given the option of picking another scene. connect signal node

Undated script as Alex CB suggested still results in error: extends Node2D

extends Node2D
# menu.tscn
@onready var main := get_tree().root.get_node("main")
@export var message := "Gwen Stefani is bananas!"
# then you can use
main.print_something(message)

unexpected "Identifier" in class body

\$\endgroup\$
4
  • \$\begingroup\$ I think you don't need a signal for this. The documentation states that signals are best used to respond to behaviour, not to start it. \$\endgroup\$
    – Alex CB
    Commented Aug 21, 2023 at 10:30
  • 2
    \$\begingroup\$ Does this answer your question? Is there a way to globally/uniquely reference a node in another scene? \$\endgroup\$
    – Theraot
    Commented Aug 22, 2023 at 0:51
  • 2
    \$\begingroup\$ By the way, about main.print_something(message), GDScript does not support top level statements. It would go inside some method. \$\endgroup\$
    – Theraot
    Commented Aug 22, 2023 at 0:51
  • \$\begingroup\$ You can't tell from the screenshot, but I'd forgotten to add the menu to the main node. Which naturally would complicate things... \$\endgroup\$
    – Ghoul Fool
    Commented Aug 22, 2023 at 5:58

1 Answer 1

1
\$\begingroup\$

To connect a signal by code, you do like this:

var err := main.connect("signal_name", callable_name)

But, for what I understand you want to do, you don't need a signal. According to the official documentation:

(signals) should be used only to "respond" to behavior, not start it. By convention, signal names are usually past-tense verbs like "entered", "skill_activated", or "item_collected".

Another way could be to access main from the menu, like this:

@onready var main := get_tree().root.get_node("main")
# or, if menu is a direct child of main
@onready var main := get_parent()


@export var message := "Gwen Stefani is bananas!"
# then you can use inside a function 
func _ready():
  main.print_something(message)

Also, you'll have to instance menu scene in your main scene by clicking the Instance child scene (chain icon, top left, next to the + symbol).

\$\endgroup\$
1
  • \$\begingroup\$ Thanks. I tried the non-signal method (pun not intended), but am still getting unexpected "Identifier" in class body \$\endgroup\$
    – Ghoul Fool
    Commented Aug 21, 2023 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .