Node Not Found and Visibility Changes

Godot Version

4.1.1

Question

I have some code, but when it runs, it produces two errors.

Here’s the code:

extends Node2D

@export var transformation = Transform2D()
@export var rotation_speed = 1000 

@onready var pickcollision

var limb
var rotating = false  # Degrees per step
var total_rotation = 0   # Total rotation so far
var beardy
var picksprite
var tilemap
var small_movement = Vector2(0.1, 0.1)
# Called when the node enters the scene tree for the first time.
func _ready():
	limb = $"."
	limb.visible = false
		# Load the scene containing the TileMap
	var scene = load("res://Scenes/Themine.tscn")
	
	# Instance the scene to access its nodes
	var scene_instance = scene.instantiate()
	
	# Find the TileMap node within the scene
	tilemap = find_tilemap_node(scene_instance)
	
	pickcollision = $"."
	emit_signal("pickcollision_ready")
	pickcollision.set_deferred("disabled", true)
	
	picksprite = $"../AnimatedSprite2D3"

func _process(delta):
	limb = $".."
	pickcollision = $"."
	beardy = $"../../../CollisionPolygon2D"
	transformation.origin = pickcollision.global_position
	##var collide
	# Check for key press
	if Input.is_action_pressed("ui_accept"):
		CollisionManagement._on_body_exited(tilemap, delta)
	# Check for key release
	else:
		rotating = false
		pickcollision.set_deferred("disabled", true)
		limb.visible = false
		pickcollision.rotation = 0

func rotate_limb(delta):
	if rotating:
		# Rotate the limb
		limb.rotate(deg_to_rad(rotation_speed) * delta)
		
		# Update total rotation
		total_rotation += rotation_speed
		
		# Check if total rotation reaches 360 degrees
		if total_rotation >= 360:
			total_rotation = 0
			rotating = false

func find_tilemap_node(node):
	# Recursive function to find the TileMap node within the scene
	if node is TileMap:
		return node

The errors are

Can't change the visibility of the main window

(likely referring to limb.visible)

and

Node not found: "../../../CollisionPolygon2D" (relative to "/root/PickaxeSwing").

Any help would be appreciated.

Thanks.

Error 1: At the start of your _process function, you’re doing:

limb = $".."

Two dots mean “move up a step in the tree hierarchy”. Since your script is attached to your main scene and the main scene is a direct child of the tree root (a.k.a. the game’s window), when – a few lines later – you do limb.visible = false, you’re trying to hide the window, which – as the errors tells you – isn’t allowed. It’s a simple typo, what you probably wanted to do here is limb = $".".

That being said, there’s no real reason to do that in the first place! Instead of this:

limb = $"."
limb.visible = false

you could simply write visible = false

Error 2: In line 3 of your _process function, you’re doing:

beardy = $"../../../CollisionPolygon2D"

So, you try to go three steps up in the hierarchy and then look for a node named “CollisionPolygon2D”. However, since the script is attached to the main scene, which – as we have established already – sits directly below the tree root, going three steps up isn’t possible.

So, clearly, your node path is wrong!

But the code is linked to the highlighted CollisionShape2D
image
Is the node path wrong?

If that’s the case, then: no, the node path should be right!

However, the error message you posted stated “relative to /root/PickaxeSwing”, which suggests your tree root has a child node called PickaxeSwing, which is using the same script or at least is trying to look up the same node path?

PickaxeSwing is the name of the script, but it did used to be linked to the Pickaxe node. When I reassigned it, I forgot to change extends Node2D to extends CollisionObject2D. This fixed it. Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.