PlayerGPS.GetNearbyObjects() should filter out null game objects

Discuss coding questions, pull requests, and implementation details.
Post Reply
User avatar
DunnyOfPenwick
Posts: 179
Joined: Wed Apr 14, 2021 1:58 am
Location: Southeast US

PlayerGPS.GetNearbyObjects() should filter out null game objects

Post by DunnyOfPenwick »

I can't think of a reason not to.

User avatar
Interkarma
Posts: 6896
Joined: Sun Mar 22, 2015 1:51 am

Re: PlayerGPS.GetNearbyObjects() should filter out null game objects

Post by Interkarma »

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.

User avatar
DunnyOfPenwick
Posts: 179
Joined: Wed Apr 14, 2021 1:58 am
Location: Southeast US

Re: PlayerGPS.GetNearbyObjects() should filter out null game objects

Post by DunnyOfPenwick »

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.

User avatar
Interkarma
Posts: 6896
Joined: Sun Mar 22, 2015 1:51 am

Re: PlayerGPS.GetNearbyObjects() should filter out null game objects

Post by Interkarma »

Sure, I can do that for you. :)

Post Reply