Weapon system recursion issue

Godot Version

4.5

Question

I’m making a 3D first person shooter with a dynamic weapon system. This system has weapon drop objects that act as rigidbody3D objects that the player can interact with, as well as the 3D weapon nodes themselves that are added to the player when a weapon drop is interacted with. The player can also drop their weapons, which removes the weapon from the player and instantiates the assigned weapon drop. The weapons themselves use a resource that holds info for the weapon stats, including the weapon drop scene. The weapon drop just has an export variable that holds the weapon scene to be added to the player.

The problem is that the weapon drops have a PackedScene var that references the weapon that should be added to the player when interacted with, and the weapons have a PackedScene var that references the weapon drop that should be created if the weapon is dropped. This has created a recursion issue. What can I do to resolve this issue? Do I need to somehow combine these two objects so that they share the same data?

Use an @export_file instead of exporting packed scenes, you’re correct that exporting PackedScenes requires and attempts to load the assigned packed scene. Generally it’s better to load as late as possible, optimally using threaded request as soon as the drop is _ready and threaded get when the weapon scene actually needed. Same issues occurs with preload, avoid these for large files with any dependencies.

@export_file("*.tscn") var weapon_scene_path: String

func _ready() -> void:
    ResourceLoader.load_threaded_request(weapon_scene_path)

func pickup() -> Node:
    var weapon_scene: PackedScene = ResourceLoader.load_threaded_get(weapon_scene_path)
    return weapon_scene.instantiate()
2 Likes

This fixed my problem! :+1: Thanks for the info, I’ll keep this in mind for any future recursion issues.