PlayerGPS.GetNearbyObjects() should filter out null game objects
- DunnyOfPenwick
- Posts: 277
- Joined: Wed Apr 14, 2021 1:58 am
- Location: Southeast US
PlayerGPS.GetNearbyObjects() should filter out null game objects
I can't think of a reason not to.
- Interkarma
- Posts: 7251
- Joined: Sun Mar 22, 2015 1:51 am
Re: PlayerGPS.GetNearbyObjects() should filter out null game objects
The nearby objects collection is updated 3 times a second via UpdateNearbyObjects(). This uses FindObjectsOfType<> to locate scene objects of a specific type that are added to the nearby collection.
No object references should be null at time of collection, but due to throttling latency might be null/destroyed when you consume the list. You should perform null checks when consuming the list to ensure no objects have been destroyed in the time since list was last refreshed. I recently had to do this myself on the compass for detect markers:
https://github.com/Interkarma/daggerfal ... 7c2927b843
Does that help explain what's happening? Let me know if any more questions around it.
No object references should be null at time of collection, but due to throttling latency might be null/destroyed when you consume the list. You should perform null checks when consuming the list to ensure no objects have been destroyed in the time since list was last refreshed. I recently had to do this myself on the compass for detect markers:
https://github.com/Interkarma/daggerfal ... 7c2927b843
Does that help explain what's happening? Let me know if any more questions around it.
- DunnyOfPenwick
- Posts: 277
- Joined: Wed Apr 14, 2021 1:58 am
- Location: Southeast US
Re: PlayerGPS.GetNearbyObjects() should filter out null game objects
I replaced the calls to FindObjectOfType in my code with calls to GetNearbyObjects and checked for null objects.
But another modder in the future might not be aware of how GetNearbyObjects works, or they might just forget,
which might lead to unexpected null errors popping up on occasion.
So why not just tack on a && no.gameObject != null to the where-clause of the query in GetNearbyObjects?
In fact, tacking on a && no.gameObject.activeInHierarchy would probably be useful as well.
But another modder in the future might not be aware of how GetNearbyObjects works, or they might just forget,
which might lead to unexpected null errors popping up on occasion.
So why not just tack on a && no.gameObject != null to the where-clause of the query in GetNearbyObjects?
In fact, tacking on a && no.gameObject.activeInHierarchy would probably be useful as well.
- Interkarma
- Posts: 7251
- Joined: Sun Mar 22, 2015 1:51 am
Re: PlayerGPS.GetNearbyObjects() should filter out null game objects
Sure, I can do that for you.