Help with spear projectile

Godot Version

Godot 4.5.1

Question

Hello! so i have created an spear item , that has a collectable area , and when the player makes contact with it , the spear_item resource that was in the spear item makes its way to the inventory , from the inventory i can use the item which uses the resources script which has the function to use it , the function makes the player throw the spear and instantiate the spear projectile(which is a whole other node) and when the distance is too far , the spear projectile despawns and the spear item spawns. It all works untill the spear despawns , godot says that the packed scene is null , i tried using my rock projectile as a debug and it works , please help im losing my mind over this ( btw dont mind the sticking into things function right now , im working on it)
Spear scene and script:


Spear item resource script:


Spear projectile scene and script:

this is the player script where i throw the item:

and this is my rock projectile script if needed:

sorry if the screenshots are a little bit funky , didnt know how else to do it , and sorry for the messy script lol

You can post codes by pressing Ctrl + E that creates a box like this

type or paste code here

and you can just paste your code like

#Script name(don't forget to add the script name or it will be confusing)
print("hello world!")
5 Likes

Cool. Didn’t know you could press Ctrl + E to do that too.

2 Likes

wow thanks , ill def use that next time

Could be a cyclic dependency. Try to avoid using preload and exporting PackedScenes, instead using load just before instantiation.

You’re player’s throw_item for instance could take a item_scene_path instead

func throw_item(item_path: String) -> void:
    var item_scene: PackedScene = load(item_path)
    var item_instance: Node = item_scene.instantiate()
3 Likes

oh my god dude, thanks , this really helped! such a simple solution yet saved me hours of pain and suffering