How to get better at reading/understanding errors?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By David000

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

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.

jgodfrey | 2022-04-14 22:21

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.

David000 | 2022-04-15 12:30

:bust_in_silhouette: Reply From: jgodfrey

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.