0 votes

I have a Scene (called "Cave") which serves as a level transitioner. It works as intended, but when I have this Scene opened in the Scene Tab, or another Scene that has it as a child, the Output starts spamming

" res://Source/Solids/Cave.gd:11 - Invalid get index 'hitbox' (on base: 'KinematicBody2D (Player.gd)')."

The Game runs perfectly fine, but when i am working and the Output starts spamming this message, the pc starts lagging a bit (it is not high end haha), and it also annoys me.

Here is the relevant part of the script for the Cave:

tool
extends Node2D

onready var hitbox := $Hitbox
onready var anim_player := $AnimationPlayer
onready var player := get_parent().get_node("Player")

export var next_scene : PackedScene

func _process(delta: float) -> void:
    if player.hitbox.intersects(self.hitbox, Vector2(0,1)): 
        teleport()

In the Player Script I also have an onready variable called "hitbox", and what I understand form the Output message, is that there is some sort of problem with that variable. As I said, the game runs fine, and when the player runs into the cave, the level transition also runs fine, so the game actually "finds" or "use" the variable "hitbox" correctly.

Godot version 3.3
in Engine by (30 points)

1 Answer

0 votes

You made cave scene dependent on player by hardcoding the reference. Because of this cave scene can not be run independently, and will return error when player is absent. On top of that You are using TOOL keyword, which provokes this error even in the editor. In other words, You will be annoyed with error as long, as there is separate cave scene without player opened anywhere.

I don't think it is safe to force TOOL node to do stuff in process(). But maybe I am too afraid of this mode :)

Anyway You may want to make referencje to player more elastic. There are many ways of doing this ( exporting nodepath, observer pattern ) but I doubt it will be usefull in TOOL. What You can do is check if PLAYER isnodevalid() before calling its variables in process()

by (2,414 points)

Thanks!! By erasing the TOOL keyword, the error spam did stop. The reason TOOL was there is not that important, so i dont need to bring it back, but just out of curiosity, I still dont understand why the error was coming up, when i had a Scene tree with both Cave and Player as silblings. The spam part was due to it beinng in process(), but I dont get why it was an error.

As to making the hardcoded reference more elastic, i will look into oberver pattern, wich I think will also be more eficient, not having to use process() and checking 60 times every second, but I am not sure what you mean by exporting nodepath. Do you mean something like onready var player := get_node("/root/Level01/Player"), because that wouldnt work, since I use the Cave in diferent Levels and the path would be diferent each time.

Thanks again!!

I thought You only get error when separate cave is opened. But now when i look at it I also see that reference to player happens ONREADY. TOOL never reaches READY function, so player variable is always null. func on_ready() happens on notification ready, which means when it is instanced into scene tree, and this only happens in running project. In editor, your cave was looking for null node every frame.

By exporting nodepath I meant EXPORT keyword.

export ( NodePath) onready var Player = get_node(Player)

This way You can assign current path leading to player and it becomes pure reference on notification ready. This way You can change scene placement of player and only assign it in editor.

As for alternative to process() You should look into signals and setget. These are two options to execute code once at a given time, instead of unstoppably each frame. Collisions typically should be checked with signals.

Great. thanks a lot!!

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.