0 votes

How can i be able / get better at understanding errors?

in Engine by (99 points)

Do you have an example error? I know your question is more general, but having a specific error might allow someone to point out the important bits it contains as well as provide input regarding the process used to understand and resolve the problem.

say something like "Invalid call. nonexistant function 'length' in base 'Dictionary'." Or something like "Attempt to call function 'get_children' in base 'null instance' on a null instance."

Idk how good these examples are as I just found them on the internet. I picked them becaues I sometimes get these errors.

1 Answer

+1 vote

OK, so the breakdown of the sample errors would be something like this:

Invalid call. nonexistant function 'length' in base 'Dictionary'

Here, you have a reference a Dictionary object and are trying to call a length function on that object. An example of that could be something like:

var some_dict = { "key1": "value1", "key2": "value2" } # create a dictionary
var l = some_dict.length() # This will cause the error...

That's because a Dictionary doesn't have a length() function. You can verify that by looking at the docs for the Dictionary Object.

If you look at the Methods section of the linked docs page, you'll see that rather than a length() method, a Dictionary has a size() method that returns the number of keys in the dict. Based on that, the fix for this error is to change the above to this:

var l = some_dict.size()

For the next error:

Attempt to call function 'get_children' in base 'null instance' on a null instance.

This is simply a case where you're trying to call the get_children() function on an object without a valid reference. Here's an example.

Say you have a node in your tree named Node2D and you want to get its children. One (correct) way to do that would be this:

var children = $Node2D.get_children()

The above assumes that the Node2D object is a direct child of the node the script code is attached to. If that's the case, the above code will work. However, if that's not the case (either the Node2D lives somewhere else in the tree, or you don't even have a node named Node2D), you'll see the problem you mention.

That's because before Godot can call the get_children() method, it needs to first get a valid reference to the node in question. To do that, it attempts to find the node called$Node2D in the tree. If it can't do that, that reference ends up being null.

So, now, instead of having a valid reference to a Node2D object, you instead have no reference at all. So, the get_children() call gets made on an invalid reference. In this case, you can think of the original code:

$Node2D.get_children()

being resolved into something like this:

[null].get_children()

And, since get_children() can only be called against a valid object (which null is not), you get the mentioned error.

Bottom line, this error always indicates that the object being used to call the method shown in the error does not have a valid reference. So, you need to figure out why that reference is null. Likely, you've type its name wrong, or it's in a different place in the tree than you've said in your code.

Hope that helps.

by (12,460 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.