0 votes

So I have a Raycast that when it detects the player, it emits a signal called "crossed_line" (for a racing game). I followed the directions of many tutorials to create the following code:
in the Raycast script:

signal crossed_line

func _on_Area_body_entered(body: Node) -> void:
emit_signal("crossed_line")
print ("nice")

func _on_WallDetector_crossed_line():
print ("AH")

and in the Player script

func _ready() -> void:
var wall = get_node("/Main/Path2D/follow/WallDetector")
wall.connect("crossed_line", self, "handle_crossed_line") 

func handle_crossed_line() -> void:
print ("I crossed the line!")

This is exactly the format I see multiple tutorials have you do, however I get an error in the player script that says "Attempt to call function 'connect' in base "null instance" on a null instance."

When I delete the player script and run the game, the game does print "nice" and "AH" whenever the player crosses the line, so I know that the raycast2d works and that the signal works.

I am also positive that the path to the node is correct.

I am not sure what I could be doing wrong, though I know it's something...any help would be greatly appreciated!

in Engine by (18 points)

1 Answer

0 votes

I'm not sure but just an idea to help :
It seems that the WallDetector node has not yet entered the tree when you reference it in the ready function of the Player script.
Try to reorder the nodes in the tree.

by (59 points)
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 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 webmaster@godotengine.org with your username.