Building a dictionary works in one Node but not another

Godot Version

4.2.1.stable.official [b09f793f5]

Question

New to Godot, not to programming. I am using a dictionary to contain spawning information. As i have been expanding the size of the game, the dictionary has gotten larger. It has been working just fine but I wanted to move it to its own Node for readability.

I have copied the whole dictionary from one Node to another. Now when loading the dictionary it always causes the following error on the line indicated below:

Invalid get index ‘position’ (on base: ‘null instance’).

I have found other people getting this error and I understand that it means i am trying to reference something that does not exist. However, the code is copied from one Node to another. The only change that was make was the dictionary is now named differently.

Ex:
Original code:
var Levels = {}
Levels[“Level_1”] = { … Works fine and loads as expected

New code:
var lvl = {}
lvl[“Level_1”] = { … always fails with the above error

The new Node is a child of the original, and the original calls a function in the child in the same way it called a function in itself.

Ex:
Original Code:
This_Level = GetLevel()

New Code:
This_level = $lvl.GetLevel()

Really scratching my head as to what the difference between the two could be. Any help would be greatly appreciated.

this mostly caused by wrong nodepath
make sure to drag and drop the node from scene tree to script if you want correct path

Thank you for the tip on dragging a dropping the node names. That is a nice feature i was unaware of. But unfortunately that is not my issue. The Node path is correct.

did you try to get the lvl on _ready()?

Just tried it and, unfortunately, it has the same issue.

no i meant if you try to get the level at _ready function, because it may be that your node read too fast before the node of lvl is ready/existed in the scene, hence why the error said it’s null
try add

await get_tree().process_frame

before you get any level

Invalid get index ‘position’ (on base: ‘null instance’)

This isn’t a dictionary access error.
This error is telling you that you tried to set the position property on something that is null.
You need to show the line the error is on.

Here is the line the error is on. When i mouse over it all it shows is the empty dictionary.

lvl: { }

as you can see, your value is only available once the node is ready aka added as child

try removing the @onready

To me it appears setting the position of fodder is the actual issue. Its saying that instance is null.

If the following is what you intended, it unfortunately did not change the anything.

Did i put it in the wrong place?

image

I have tried that, no change

I tried defining the variable in line in the “BuildLevels” function, no change

I have tried constructing the dictionary at the time of defining the variable; both inside and outside of the “BuildLevels” function, in this instance the lvl dictionary is “Null” in stead of empty
image

Since your error mentions position, I would take a look at those $S1, $S4, and $S5 nodepaths - have those been updated since you moved the code?

1 Like

As Tayacan says it is a problem with $S1, $S4, and/or $S5.
Before accessing lvl[...], try:
printt($S1, $S4, $S5)
If any of those print null, then you need to figure out what is going on with those nodes.
More than likely the code you show is from the other original initiation that you did, and now that the code is in a different node, S1, S4, S5 are at a different path structure.

Solved!

Thank you for helping work through this.

The issue was that the markers, that were holding the position data, were part of the Parent Node (Original location) and not the Child node (New Location).

I was focusing on the dictionary being empty. I was thinking that it would have added some of the sub-dictionaries (Like “Wave_1” or “Ripple_1” before any error would have triggered.

Thank everyone for their help, I very much appreciated it!