+1 vote

Hi !

I understand that there is no exceptions in gdscript. However, I find myself in an odd situation.

During the processing of my game, I may or may not have created a node at runtime, with the name created_node. Now I want to delete it if it exists.

To find the node, I do a var node_to_delete = get_node("created_node"). Now, if the node exists, node_to_delete is now pointing to it, and I can do node_to_delete.free() or whatever. However, if it does not exists, node_to_delete now contains null. So I have to check for that before going further. The final code snippet is thus :

var node_to_delete = get_node( "created_node" )

if node_to_delete == null:
    return
else:
    node_to_delete.free()

This code runs as intended, however while testing it I noticed an error in the debugger, stating: Node not found: created_node

So, am I writing godotscript the way it was intended? What should I do with that error? Just ignore it? Or should I check that created_node exists before doing my get_node()? But won't that slow the process down for no good reason?

Please, enlighten me! (and as a bonus, convince me there is a good reason I can't do this with a simple try ... except ... block :] )

in Engine by (28 points)

2 Answers

0 votes
Best answer

Do you expect null reference often? If the probability of null is high, then it's not an exception. Well, there is no try except/catch in GDScript so you can't do it. Also, exception handling often impacts your performance more than basic null check (at least in Java) so even if Godot had exception system, simple check has_node(nodePath) would probably be better.

by (812 points)
selected by

In lack of a better answer I select that one for the alternative solution of using has_node().

But this does not explain why Godot prompts me an error but does not crash, and if using has_node() to prevent the error from happening is the correct way of doing thingsā„¢, or if I should just leave it alone and stop worrying. :]

Well, if you're not sure the node exists, has_node() is the correct way of doing things. Godot is built in a way to not crash every time there is a problem.

0 votes

Another solution I can think of for checking against dynamically created nodes, is storing them in an array or some container as they're created.

Then validating them by looping through that array or iterating your preferred data structure. An empty array will tell you if they're existing or not. Then if there are nodes, while iterating a loop you should be able to carry on with the desired logic from there on each item.

by (5,200 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.