Fix null reference error in weapon system collision observer
Some checks failed
Build / build (push) Failing after 19s
Some checks failed
Build / build (push) Failing after 19s
Fixed TypeError when accessing collision observable on disposed physics bodies. The error occurred in two places: 1. Inside collision callback when trying to remove observer after hit 2. In timeout cleanup when projectile disposed before 2-second timer Added defensive null checks and try-catch blocks in both locations to handle race conditions where physics bodies are disposed during collision handling. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
4ae272dea9
commit
37c61ca673
@ -129,20 +129,34 @@ export class WeaponSystem {
|
||||
hitRecorded = true;
|
||||
|
||||
// Remove collision observer after first hit
|
||||
if (collisionObserver) {
|
||||
if (collisionObserver && ammoAggregate.body) {
|
||||
try {
|
||||
ammoAggregate.body.getCollisionObservable().remove(collisionObserver);
|
||||
} catch (e) {
|
||||
// Body may have been disposed during collision handling, ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Auto-dispose after 2 seconds
|
||||
window.setTimeout(() => {
|
||||
// Clean up collision observer
|
||||
if (collisionObserver) {
|
||||
// Clean up collision observer if body still exists
|
||||
if (collisionObserver && ammoAggregate.body) {
|
||||
try {
|
||||
ammoAggregate.body.getCollisionObservable().remove(collisionObserver);
|
||||
} catch (e) {
|
||||
// Body may have already been disposed, ignore error
|
||||
}
|
||||
}
|
||||
|
||||
// Dispose if not already disposed
|
||||
try {
|
||||
ammoAggregate.dispose();
|
||||
ammo.dispose();
|
||||
} catch (e) {
|
||||
// Already disposed, ignore
|
||||
}
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user