Help with instantiating scene

Godot Version

Godot 4.4

Question

Hello, I am trying to implement a strange inventory system. I have a drill that is a characterbody and couldn’t figure out how to translate that into a resource that I could use with the built in systems for inventory management, so I have a sprite2d box literally sitting in the map (at this game’s “base”) with an area2d that is checking for this drill item (which is in it’s own group). If the drill item is there and I press the “put in” button it deletes the scene, everything is good there.

However, when I try to “collect” the drill from this box, I am preloading the drill scene as a variable, and when the “collect” button is pressed, setting a new variable to be drill_scene.instantiate() and then setting the new variable’s property which relates to the character holding the drill, so it would be like the player character holding the drill instance as if they picked up the og off the ground.

But when that happens it errors out and tells me that the player character is a null_instance and I can’t access its properties. I didn’t think my logic for the character holding the item was awful, but maybe I am going at it all wrong. Here is what I think is the relevant code:

I have the player’s unique name in the drill script and in the inventory/storagebox script,
and then in the physics process of the drill script:

if drill_picked_up:
			global_position = player.global_position

it works fine when I pick it up in the game world, but when instantiated acts like the player is a null_instance and won’t let me access the property,

here is what’s in the inventory/storagebox script:

if Input.is_action_pressed("COLLECT") and occupado and player_by_box:
		occupado = false
		drill_icon.visible = false
		var drill = drill_scene.instantiate()
		drill.drill_picked_up = true
		get_tree().get_root().add_child(drill)

Thank you for reading.

Unique names are limited to the same scene, they’re not supposed to be used as Global identifiers, more like shortcuts within the scene.

If the player variable is null, then how are you defining player in the drill script?

1 Like

I definitely did not realize that and I think that’s my problem.
I just have the unique player from the game level set as an onready variable. I drug it over and did the press control release click thing to set it as an onready variable in both the drill and inventory scripts.

I was actually considering if I could somehow set the player variable to be the scene in the ready function, but I didn’t go too far down that road because it didn’t make sense why I would be able to set it there and have it work when it doesn’t as an onready variable.

So I am definitely coming at this incorrectly lol. Thank you for letting me know.