Page 1 of 1

PlayerGPS.GetNearbyObjects() should filter out null game objects

Posted: Tue Aug 02, 2022 1:27 pm
by DunnyOfPenwick
I can't think of a reason not to.

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

Posted: Wed Aug 03, 2022 12:13 am
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.

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

Posted: Wed Aug 03, 2022 4:26 am
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.

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

Posted: Wed Aug 03, 2022 6:14 am
by Interkarma
Sure, I can do that for you. :)