r/badcode • u/GodIsDead_ • Jun 17 '20
c# You've heard of spaghetti code, get ready for sausage link code
143
u/Farsqueaker Jun 17 '20
TFW you hope you're looking at a circular reference...
89
Jun 17 '20
it is, everything in unity has a gameObject attribute that points towards its GameObject, including GameObject instances
104
u/Koxiaet Jun 17 '20
It's the Unity equivalent of
this.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis.window.self.globalThis11
u/stauffski Jun 18 '20
Why is this a thing? I'm naive.
21
Jun 18 '20
A GameObject instance represents an actual object within a Unity game. Sometimes an event is given a component which is part of a GameObject, but not the GameObject itself. For example, when 2 objects in Unity collide, and you want to run some code when that happens, you set up an OnCollisionEnter function. This function will only know the Collider component of the GameObject that collider, not the GameObject itself. If you want to get the actual thing itself, not just the collider, you can do collider.gameObject to reference its GameObject parent. It’s just a quirk that GameObject also has a .gameObject.
(I’m not great at explaining things so I’m not sure if that’ll make things clearer but I hope it helps)
4
u/HoiTemmieColeg Jul 14 '20
In Unity, all Objects are represented as a GameObject. Game objects have components (more specifically MonoBehaviours) which are scripts that you write with stuff like ‘void Update()’ that runs every frame. This is how unity allows you to write your own code. All components have a gameObject attribute which points to the GameObject it is attached to, including the GameObject itself.
gameObject.gameObject.gameObject.gameObject is a circular reference that keeps pointing back to itself.
1
8
u/DreadCoder Jun 18 '20
Scene graph. the world is a tree, and the 'floor' is the first node.
like, a brief glowly particle is probably a GameObject, and you would attach hundreds to a straight line if you wanted to make a beam weapon. which is attached to a gun => and arm => a t-posed skeleton => the floor => the world
6
u/Farsqueaker Jun 18 '20
Well, that would be a linear reference chain.
Now take your line of thinking, extend it to the number of gameObject getters that are in the OP, and consider the level of nesting that would be required there.
A circular reference, on the other hand, is when an object has a property that ultimately refers back to itself, either directly or indirectly by referencing an object that has a reference back to it. Anyone that has to do with serialization eventually runs into this issue, as it's a common source of infinite loops.
In this case, here's the property:
public GameObject gameObject => this;Gah, there's no way to kill a joke like having to explain it, is there?
4
u/DreadCoder Jun 18 '20
That sounds like
class MyThis { public MyThis getThis() { return this; } } foo = new (MyThis).getThis()with extra steps
3
1
u/Speedswiper Jun 19 '20 edited Jul 14 '20
GameObject inherits from MonoBehaviour, which has a lot of functions and properties used by Unity objects. Other components also inherit from MonoBehaviour, and these components are generally attached to GameObjects. For the most part, referencing gameObject is useful. The only time it's completely circular is for GameObjects themselves, and the only way to fix that uselessness is to stop GameObjects from inheriting MonoBehaviour, which means it loses all other properties of MonoBehaviour.
2
u/HoiTemmieColeg Jul 14 '20
To be a little more specific, Component inherits from GameObject and MonoBehaviour inherits from Component. (Most built-in unity components aren’t MonoBehaviours but are instead Components).
1
253
u/Mrinin Jun 17 '20
Yandere dev code be like
114
u/iiStik Jun 17 '20
are ya coding son
67
34
u/Mrinin Jun 17 '20
i just hate how yandere dev uses 'this' and '== true' and the post just reminded me of unnecesary long code
51
u/aurosvr Jun 17 '20
He most likely doesn’t, the decompiled code that everyone saw, was... decompiled. It automatically adds “this” and converts if (foo) to if (foo == true) when compiled. The rest of the code is still absolutely horrendous
4
u/HoiTemmieColeg Jul 14 '20
He used to stream himself coding though so there’s some examples of the real code there.
47
u/AnAutisticSloth Jun 17 '20
this.this.thisity.this.getComponent<idk>().gameObject.getComponent<Spaghetti>();
7
u/YaboiMuggy Jun 18 '20
I wish, its just ifs. Too many excessive ifs. Ifs used instead of switches. Ifs that don't need to be evaluated every frame but do. More of his code is ifs than not ifs. He has only 1 switch statement and he made it to prove he can use them. It isn't used. He has a terrible set of ifs that does the same thing get called instead.
3
1
u/_TRN_ Jun 19 '20
Yandere dev has been educated multiple times by multiple people but he insists that his way is better because "that's what he knows best". Maybe if he wasn't so egotistical he would've improved by now.
45
24
22
10
u/ballfun Jun 19 '20
ok hear me out
gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>().gameObject.GetComponent<MeshCollider>()
9
1
u/kjl3080 Jun 21 '20
Pointers? What’s that?
—unity dev not knowing the power of c++
3
3
2
2
1
1
1
0
66
u/lunareffect Jun 17 '20
jabrils is an awesome coder though. Check out his YouTube channel if you haven't done so yet.