diff options
| author | alex <[email protected]> | 2026-11-23 12:00:20 +0100 |
|---|---|---|
| committer | alex <[email protected]> | 2026-11-23 12:00:20 +0100 |
| commit | 7f4bd1856869d7ae10fa8f07708fbd75c5bb7b1e (patch) | |
| tree | e1fa2d87a79bc8bae87da9e62b19521fe27643e5 /scripts/WeaponHolder.cs | |
| parent | 45326f4f10ba2af92ff5d35f0f63e1dc35a4f8eb (diff) | |
| download | godot-testing-items.tar.xz godot-testing-items.zip | |
pesitems
Diffstat (limited to 'scripts/WeaponHolder.cs')
| -rw-r--r-- | scripts/WeaponHolder.cs | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/scripts/WeaponHolder.cs b/scripts/WeaponHolder.cs index 210be60..6f6c7b2 100644 --- a/scripts/WeaponHolder.cs +++ b/scripts/WeaponHolder.cs @@ -2,9 +2,9 @@ using Godot; public partial class WeaponHolder : Node2D { - [Export] public PackedScene[] WeaponScenes { get; set; } + [Export] public PackedScene[] WeaponScenes { get; set; } = System.Array.Empty<PackedScene>(); - private Weapon _currentWeapon; + private Weapon? _currentWeapon; public override void _Ready() { @@ -17,15 +17,22 @@ public partial class WeaponHolder : Node2D if (WeaponScenes == null || WeaponScenes.Length <= index || WeaponScenes[index] == null) return; + EquipWeaponScene(WeaponScenes[index]); + GD.Print($"WeaponHolder equipped weapon index: {index}"); + } + + public void EquipWeaponScene(PackedScene weaponScene) + { + if (weaponScene == null) + return; + if (_currentWeapon != null) { _currentWeapon.QueueFree(); } - _currentWeapon = WeaponScenes[index].Instantiate<Weapon>(); + _currentWeapon = weaponScene.Instantiate<Weapon>(); AddChild(_currentWeapon); - - GD.Print($"WeaponHolder equipped weapon index: {index}"); } public override void _UnhandledInput(InputEvent @event) @@ -40,4 +47,4 @@ public partial class WeaponHolder : Node2D if (@event.IsActionPressed("weapon_1")) EquipWeapon(0); else if (@event.IsActionPressed("weapon_2")) EquipWeapon(1); } -}
\ No newline at end of file +} |
