@@ -537,7 +537,8 @@ function RopeInputController.refreshGunReference(grappleInstance)
537
537
-- Only refresh if we don't have a valid reference
538
538
if grappleInstance .parentGun then
539
539
local success , presetName = pcall (function () return grappleInstance .parentGun .PresetName end )
540
- if success and presetName == " Grapple Gun" then
540
+ local idSuccess , gunID = pcall (function () return grappleInstance .parentGun .ID end )
541
+ if success and presetName == " Grapple Gun" and idSuccess and gunID and gunID ~= rte .NoMOID then
541
542
Logger .debug (" RopeInputController.refreshGunReference() - Current gun reference is valid, no refresh needed" )
542
543
return true -- Current reference is fine
543
544
end
@@ -577,18 +578,63 @@ function RopeInputController.refreshGunReference(grappleInstance)
577
578
end
578
579
579
580
if foundGun and grappleInstance .parentGun then
580
- -- Update magazine state for the refreshed gun
581
- if grappleInstance .parentGun .Magazine and MovableMan :IsParticle (grappleInstance .parentGun .Magazine ) then
582
- local mag = ToMOSParticle (grappleInstance .parentGun .Magazine )
583
- mag .RoundCount = 0 -- Keep showing as "fired"
584
- mag .Scale = 0 -- Keep hidden while grapple is active
585
- Logger .debug (" RopeInputController.refreshGunReference() - Updated magazine state for refreshed gun" )
581
+ -- Test if we can actually access the gun's properties
582
+ local testSuccess , testID = pcall (function () return grappleInstance .parentGun .ID end )
583
+ if testSuccess and testID and testID ~= rte .NoMOID then
584
+ -- Update magazine state for the refreshed gun
585
+ local magSuccess , magazine = pcall (function () return grappleInstance .parentGun .Magazine end )
586
+ if magSuccess and magazine and MovableMan :IsParticle (magazine ) then
587
+ local mag = ToMOSParticle (magazine )
588
+ mag .RoundCount = 0 -- Keep showing as "fired"
589
+ mag .Scale = 0 -- Keep hidden while grapple is active
590
+ Logger .debug (" RopeInputController.refreshGunReference() - Updated magazine state for refreshed gun" )
591
+ end
592
+ return true
593
+ else
594
+ Logger .warn (" RopeInputController.refreshGunReference() - Found gun but cannot access its properties" )
595
+ grappleInstance .parentGun = nil
596
+ return false
586
597
end
587
- return true
588
598
end
589
599
590
600
Logger .warn (" RopeInputController.refreshGunReference() - Could not find any grapple gun" )
591
601
return false
592
602
end
593
603
604
+ -- Restore magazine state when grapple is being destroyed
605
+ function RopeInputController .restoreMagazineState (grappleInstance )
606
+ if not grappleInstance .parentGun then
607
+ Logger .debug (" RopeInputController.restoreMagazineState() - No parent gun to restore" )
608
+ -- Try to find gun one more time for restoration
609
+ if RopeInputController .refreshGunReference (grappleInstance ) then
610
+ Logger .debug (" RopeInputController.restoreMagazineState() - Found gun during restoration attempt" )
611
+ else
612
+ return false
613
+ end
614
+ end
615
+
616
+ -- Don't call refreshGunReference again if we already have a gun reference
617
+ -- Test the gun reference directly
618
+ local success , gunID = pcall (function () return grappleInstance .parentGun .ID end )
619
+ if success and gunID and gunID ~= rte .NoMOID then
620
+ Logger .info (" RopeInputController.restoreMagazineState() - Restoring magazine state for gun (ID: %d)" , gunID )
621
+
622
+ -- Restore magazine visibility and ammo count
623
+ local magSuccess , magazine = pcall (function () return grappleInstance .parentGun .Magazine end )
624
+ if magSuccess and magazine and MovableMan :IsParticle (magazine ) then
625
+ local mag = ToMOSParticle (magazine )
626
+ mag .RoundCount = 1 -- Restore ammo
627
+ mag .Scale = 1 -- Make magazine visible again
628
+ Logger .info (" RopeInputController.restoreMagazineState() - Magazine restored (visible, ammo: 1)" )
629
+ return true
630
+ else
631
+ Logger .warn (" RopeInputController.restoreMagazineState() - No magazine found to restore" )
632
+ end
633
+ else
634
+ Logger .warn (" RopeInputController.restoreMagazineState() - Gun ID invalid or inaccessible" )
635
+ end
636
+
637
+ return false
638
+ end
639
+
594
640
return RopeInputController
0 commit comments