Reputation: 382
What I've been trying to do so far -
I have a class Singleton script called recipes
. I have a signal launched
in it. I have objects of the recipe
class. One of the objects is noodles
. I want to connect the signal launched
to a function in noodles called after_launch
. I am not sure what node to connect the signal to. I am using a class because I have multiple recipes
, each having its own launched
signal.
I also want to know how to connect a signal to a function in a Singleton Script - without having nodes at all.
Code I've been using for the same -
connect("launched", self, "launched_noodle")
I do understand that using "connect" without using a node in front of it gives an error. Error -
error(3,1) Unexpected token Identifier:connect
How do I -
1) Connect it to the node on which the code is written on - w.r.t recipes
and noodles
. What nodes do you generally connect your signals to?
2) Connect when I just want to connect it to the Singleton Script.
I tried to Google the problem and came across this question on Godot Forum for a similar issue. I don't understand the full question or answer. Perhaps might be a help for you to refer to.
Additional Information -
Code of my class (Singleton)recipe
extends Node2D
class_name recipe
signal launched
Code of noodles
and other children -
extends recipe
connect("launched", self, "launched_noodle")
The flow of Code -
User triggers thecooking station
and the cooking station
node opens up on the screen.
After that, the user's level and other parameters are checked and an appropriate recipe
is shown on the screen. This recipe
is a defined class
- a Godot Singleton - and has many physical nodes linked under cooking station
Once the appropriate node is targetted and shown on the screen, the signal
that I wanted to launch should be emitted by cooking station
. This signal
- depending on the node is it emitted on, makes certain changes on the screen. That is through a function named launched_recipename
Upvotes: 2
Views: 2447
Reputation: 7228
The error Unexpected token Identifier:connect
occurs because connect
is a function, so you need to call it within the scope of another function, like _ready
. Based on the flow you describe, it sounds like launched
should be a signal defined in the cooking station, not recipe
, so you might have something like this:
launched
)
_on_launched
)In Noodles.gd
:
extends Recipe
func _on_launched():
print("Noodles was launched")
func _ready():
var cooking_station = get_parent()
cooking_station.connect("launched", self, "_on_launched")
There's really no need for a Singleton here. If you were thinking you needed a Singleton so Noodles
could inherit from Recipe
, it isn't necessary. Just adding the class_name Recipe
directive will put Recipe
in the global scope so other scripts can inherit from it.
Upvotes: 0