I don't understand why base is "Nil"

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

I’m a noob following a tutorial to make a resource-based inventory. The inventory screen populated fine when I assigned the relevant data to it directly, but once I assigned that data to a more relevant node, exported it, and tried to pass it over, everything broke. I just get a gray screen now.

I assume there has to be a problem referencing node paths but I don’t see it.

The last line here is getting flagged by the debugger:

@onready var clue_inv = $MarginContainer/ClueContainer/ClueInventory

func set_known_cluelist(known_clues: ClueListData) -> void:
    clue_inv.set_known_clues(known_clues)

It shows "Invalid call. Nonexistent function “set_known_clues” in base “Nil”. It also shows the relevant variable (clue_inv) as “null”. That tracks with the error message.

…Except that I defined clue_inv right there. I dragged the path directly from the tree. I don’t know why it’s null.

In case it’s relevant, here’s the set_known_clues function:

func set_known_clues(known_clues: ClueListData) -> void:
    populate_clue_list(known_clues.clue_slots)

populate_clue_list worked fine when I was manually assigning the test inventory data. I don’t think it’s the problem so I’m not including it here.

What am I not seeing?

As you’ve noted, the problem seems to be that your clue_inv variable is null (so, not related to the set_known_clues() function). How does set_known_cluelist() get called (and from where)?

jgodfrey | 2023-07-07 15:53

set_known_cluelist() gets called in the ready function of the root node:

@onready var StoryScreen = $/root/Node2D/GameContainer/StoryScreen
@onready var MenuDisplay = $/root/Node2D/GameContainer/MenuContainer/MenuBounds/MenuMargin/TabController/MenuDisplay

func _ready():
    MenuDisplay.set_known_cluelist(StoryScreen.known_clues)

“MenuDisplay” is the node to which the problem script is attached. “StoryScreen” is a node with a script that exports the variable known_clues, which holds the data I’m trying to access.

arthropod | 2023-07-07 16:41

If your script is on the root node, why do you use /root/Node2D/ in the path to get MenuDisplay?

I’m asking this because if it actually happens to be a different branch of the tree, it would mean that MenuDisplay is not a child of the node on which the script that calls set_known_cluelist is. Which in turn means it could be an execution order issue since _ready only guarantees that children are ready, not nodes of different branches.
(besides, it’s not a good idea to use absolute paths)

Zylann | 2023-07-07 17:37

First, I accidentally hit report instead of reply. I undid it but sorry if that did something.

This helped me figure it out, thank you so much!! I was accidentally trying to add set_known_cluelist to an autoload, not the root node script. I knew something was off there but it didn’t click until just now.

arthropod | 2023-07-07 19:43

:bust_in_silhouette: Reply From: Zylann

Assuming the script you posted is on a node, I see two possible reasons clue_inv is null:

    1. $MarginContainer/ClueContainer/ClueInventory is not a valid path. Maybe the node is not present relatively to the node your script is on.
    1. set_known_cluelist is called before the node was ready-ed (i.e _ready being called, which is what @onready means). This can happen for example if set_known_clues is called from the setter of an exported property (which is set when the node is being deserialized from the scene file, in which case its children arent even available yet). It can also happen if that function is called from _init or _enter_tree, or before the node (or its parent) is added to the tree, which are also too early.

I tend to think you are hitting 2), but it’s only a supposition so you may have to provide more details about how you are calling that method and when.
If you are using a property setter you might have to defer some logic into _ready if the node isn’t in the tree yet.

Sorry, set_known_cluelist is called in the ready function of the root node. I should have specified. I pasted it in the response to another comment.

arthropod | 2023-07-07 16:44

1 Like
:bust_in_silhouette: Reply From: DDdidi

Maybe you created a .gd class instead of a .tscn class in your parent node