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.
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)
signal
for this. The documentation states that signals are best used to respond to behaviour, not to start it. \$\endgroup\$main.print_something(message)
, GDScript does not support top level statements. It would go inside some method. \$\endgroup\$