Nifflas,
@Nifflas@mastodon.gamedev.place avatar

Jesus Christ, I'm in a situation where Unity's fake null check causes havoc.

I can't wait to switch to an engine that doesn't override the null equality check. I hate that thing so much :(

slembcke,
@slembcke@mastodon.gamedev.place avatar

@Nifflas IIRC uou can get around it with the null coalescing operator (??) or conditional call operator (?.) since those don’t call == internally. We have unspoken rule that you can’t use those operators in Unity code because of it.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@slembcke Yeah, it's just... like, I don't know what to say X)

slembcke,
@slembcke@mastodon.gamedev.place avatar

@Nifflas Yeah, it was a terrible mistake made too long ago to fix it. :(

oldschoolpixels,
@oldschoolpixels@mastodon.gamedev.place avatar

@Nifflas one of the worst design decisions in unity, IMO.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@oldschoolpixels Yeah, I feel this is almost unfixable because fixing it would basically practically destroy any Unity project.

It's just outrageously frustrating, I'd even be up for it being fixed and breaking all my old projects.

oldschoolpixels,
@oldschoolpixels@mastodon.gamedev.place avatar

@Nifflas yeah, it would be very breaking. To be honest, I don't even know how easy it would be to change implementation, because (I don't know if you're aware) it stems from Unity's architecture itself.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@oldschoolpixels I can't see why that would be hard to change tho (given it's OK to break all previous Unity projects). Remove the fake null operator override, and expose an "isDestroyed" property that returns the exact same information as a bool.

oldschoolpixels,
@oldschoolpixels@mastodon.gamedev.place avatar

@Nifflas ok, I stand corrected 😬 It wouldn't be that hard.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

I can use ReferenceEquals to check if it's true null. But, how do I check if it's fake null? Comparing the output of ReferenceEquals against the result of the fake null check?

This is an absolute disaster.

Ebeeto,
@Ebeeto@mastodon.social avatar

@Nifflas Have you tried turning it off and on again?

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@Ebeeto I will try! But odds are, something new will be broken if I do that!

runevision,
@runevision@mastodon.gamedev.place avatar

@Nifflas I suppose yes compare the actual and fake check. And wrap that into an extension method?

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@runevision Yeah, I should do that!

It just messes with my head so much whenever the fake null check is involved. Usually code is clear, but I can't build a mental model whenever dealing with this :( Things aren't what they're supposed to be and my brain is unable to process the information.

I'll probably add a "IsFakeNull" and "IsRealNull" extension method. But ideally, I need to figure out a way to output an IDE warning whenever I accidentally compare an UnityEngine.Object against null too.

toerror,
@toerror@mastodon.gamedev.place avatar

@Nifflas @runevision
I note that Object.GetHashCode returns 0 on "fake" null objects, so this should do what you want:

if (go is null || go.GetHashCode() == 0) ...

toerror,
@toerror@mastodon.gamedev.place avatar

@Nifflas @runevision ( I'm assuming you want to avoid the lifecycle check here... )

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@toerror Yeah, I only want to check if Destroy has been called on it.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@toerror @runevision Wait, isn't that severely going to mess with any collection that uses the hash code to find the object, like a Dictionary or HashSet?

This is getting scarier every second.

toerror,
@toerror@mastodon.gamedev.place avatar

@Nifflas @runevision In that all unassigned objects are the same I suppose.. Topic for philosophers ;)

Nifflas, (edited )
@Nifflas@mastodon.gamedev.place avatar

@toerror @runevision It's a performance loss for binary search with lots of instances sharing the same hash code. Starting to form an idea why this is happening tho. Overriding the equality check also requires overriding the hash code. And they shouldn't have different hash codes if comparing their equality should return true, otherwise collections will break. So Unity sets them all to 0.

This is a bigger mess than I anticipated.

Why didn't they just not override the null check? Oh my god!

Nifflas, (edited )
@Nifflas@mastodon.gamedev.place avatar

@toerror @runevision So much stuff only to be able to check if (x == null) to see if it's destroyed.

Doesn't make sense for a beginner who doesn't know what null is. isDestroyed would make more sense. Current implementation heavily distorts a learner's concept of when a reference can become null (normally it cannot if you're referencing the object).

Doesn't make sense for an advanced programmer like me who values accessing the underlying information. isDestroyed would have made more sense too!

runevision,
@runevision@mastodon.gamedev.place avatar

@Nifflas @toerror It's not only a check to see if an object that did exist before has now been destroyed. It's a check if it exists at all. Your isDestroyed property suggestion would throw a nullreference exception if the object was never assigned. I think an Exists() method could be implemented as an extension method, but I don't think those existed back when Unity made its fake null implementation.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@runevision But I want that. I want to have to do a proper null check, then a isDestroyed check.

null and destroyed are unrelated. They have nothing to do with each other. That's good. That's proper. That's how it's supposed to be.

And maybe we can implement an IsNullOrDestroyed method specifically when someone wants to check both at the same time.

runevision,
@runevision@mastodon.gamedev.place avatar

@Nifflas That's super verbose and I'd be super annoyed if I had to do that in every case where I can currently just check for (fake) null. You're asking so much code that's simple to be made more complicated, just so that your own use case that's so complicated you cannot easily explain it here would be simpler for you.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@runevision I edited the post. We can have an IsNullOrDestroyed method for that. Like a string has IsNullOrEmpty - which is how it's supposed to be done. Don't sacrifice consistency and accuracy for convenience.

The fake null check has been driving me insane for years. I want that goddamn thing gone and I'm ready to have every single Unity project broken over it.

runevision,
@runevision@mastodon.gamedev.place avatar

@Nifflas I can't be convinced before I understand your use case. >:)

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@runevision I can't explain it here, it'd be way too long. It's not even a single use case. I'm just running into it over and over again.

runevision,
@runevision@mastodon.gamedev.place avatar

@Nifflas I'm the opposite - I like the fake null check, as I just want to know if the object exists or not. If the C# object exists but the backing native object is destroyed, that's never of use to me. And I've never encountered a situation where the fake null check is an issue. Kind of curious what the situation is for you, where it is.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@runevision It's too much to explain here, it's about the part of the code that tracks stuff and verifies everything is correct. LinkedLists are involved.

I don't do that sort of thinking though. When I ask if something is "null", I'm not asking if the object has been destroyed. I'm literally asking if the reference is null, and ONLY if the reference is null.

I can't build mental models if the meaning changes. I deal in absolutes. I guess I'm a Sith Lord of Unity users.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@runevision Anyway, before I do anything else, I should figure out a way to add a compiler warning whenever I do a fake null check. Just remove the ability to do it for myself.

Then I'll start implementing extension methods that behave the way I want.

AngryAnt,
@AngryAnt@mastodon.gamedev.place avatar

@Nifflas @runevision Could you not just do your own equality operator overload and mark it obsolete? Worst case the compiler will ask you to be explicit on which of the two to use, which already achieves your goal?

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@AngryAnt Wait, can I really overload of an already overloaded one? I mean, I'm willing to try! I didn't know that was possible!

AngryAnt,
@AngryAnt@mastodon.gamedev.place avatar

@Nifflas Presumably not in the same namespace, but I'm guessing you are not working inside the UnityEngine namespace.

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@AngryAnt I'm working in my own namespace, but I am using the UnityEngine one in my scripts. Otherwise I can't access UnityEngine.Object which has the overload I'm trying to prevent myself from using.

AngryAnt,
@AngryAnt@mastodon.gamedev.place avatar

Hm. Yea sorry I was misremembering that recent C# versions had added the ability to do operator overloads as extension methods - that still appears to not be allowed and the places I've needed it I've relied on patching.

runevision,
@runevision@mastodon.gamedev.place avatar

@AngryAnt Is this patching a whole big ordeal, or could it be viable for adding those operator overloads? Where to learn more about it?

AngryAnt,
@AngryAnt@mastodon.gamedev.place avatar

@runevision We wrote our own cecil-based setup which I really should get around to cleaning up and releasing, but there should be other nice implementations around.

At some point Unity added what they call a "weaver" for their multiplayer stuff - also doing cecil-based patching. Perhaps reusing that is shortest path?

JeremiahFieldhaven,
@JeremiahFieldhaven@mastodon.gamedev.place avatar

@Nifflas @runevision "Sith Lord of Unity" now there's a thing to add to your bio 😆

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@JeremiahFieldhaven @runevision Haha, yeah :) I think I'll proudly wear that title! I do want to restore order to Unity, even at the cost of ruining previous projects made with it. Unity needs to conform!

Okay, wow. I am clearly the evil person here.

JeremiahFieldhaven,
@JeremiahFieldhaven@mastodon.gamedev.place avatar

@Nifflas If this is the dark side, I can make some cookies? 🍪 🍪

Given all the things that can break doing a unity upgrade, is it really evil to add another one? Really?

(I mean, it's easy for me to say this, considering I switched to Unreal...)

Nifflas,
@Nifflas@mastodon.gamedev.place avatar

@JeremiahFieldhaven I'll take some cookies thank you omnomnomnomnomnom

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • megavids
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • anitta
  • Leos
  • tester
  • provamag3
  • JUstTest
  • All magazines