Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

What's your worst hack?

Discussion in 'General Discussion' started by TheGaul, Nov 17, 2019.

  1. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    I trusted you.
     
    Ony likes this.
  2. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    My condolences.
     
    pcg likes this.
  3. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    Char arrays are faster than Strings, as strings are objects.
     
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    You could think the second of these two would be faster since you do not need to create the extra Span<T>.

    Code (CSharp):
    1.             var watch = new Stopwatch();
    2.             watch.Start();
    3.  
    4.             for (int i = 0; i < 100000; i++)
    5.             {
    6.                 var index = "foobar".AsSpan().IndexOf("bar");
    7.             }
    8.  
    9.             watch.Stop();
    10.             Console.WriteLine(watch.ElapsedTicks);
    11.  
    12.             watch.Restart();
    13.  
    14.             for (int i = 0; i < 100000; i++)
    15.             {
    16.                 var index = "foobar".IndexOf("bar");
    17.             }
    18.  
    19.             watch.Stop();
    20.             Console.WriteLine(watch.ElapsedTicks);
    But in fact the first one is about 9 times faster (Core 2.2).
     
  5. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Wait hacks are pretty common like

    Code (CSharp):
    1.         private IEnumerator ApplyQualitySettings() {
    2.             yield return new WaitForSeconds(0.1f); //We need to wait here for a bit otherwise settings does not apply
    3.  
    4.             QualitySettings.antiAliasing = settings.AntiAliasing;
    5.             XRSettings.eyeTextureResolutionScale = settings.RenderScale;
    6.             XRSettings.gameViewRenderMode = settings.DesktopMode;
    7.         }
    or

    Code (CSharp):
    1.         public override IEnumerator Execute()
    2.         {
    3.                 ShowPopup(item.ColliderParent.transform, GetGrabText());
    4.  
    5.                 while (!item.IsAttached)
    6.                 {
    7.                     yield return null;
    8.                 }
    9.  
    10.                 yield return null; //Wait one for steamvr to catchup with its action state      
    11.         }
     
  6. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    Anders in regards to initialising the var inside the for loop

    1_tO2UvhOdAdZpKKZOj2BA-g.png
     
  7. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149

    I think I saw a comment or two there buddy, I trusted you!
     
    Ony likes this.
  8. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I never comment, but when I do I comment why not how ;)
     
    iamthwee likes this.
  9. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149

    No comment
     
    AndersMalmgren likes this.
  10. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    No comment is always better than a comment that tells how ;)
     
    iamthwee likes this.
  11. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    SMH, so when you told @Ony you don't ever use comments and you shouldn't - you were essentially lying. What else am I to think about these unity forum users?! Maybe @Murgilod isn't an angry young lady, but a softly spoken sweetheart IRL!
     
    Last edited: Nov 19, 2019
    Ony likes this.
  12. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,141
    Yes, because they're totally (nope) going to remember the names of them let alone what they are just like I totally (nope) remember the names of the patterns I use on a daily basis. :p
     
    iamthwee likes this.
  13. iamthwee

    iamthwee

    Joined:
    Nov 27, 2015
    Posts:
    2,149
    I saw this really hot girl in the nightclub last night and she was wearing a chessboard patterned shirt...

    So, I made a move on her
    only joking I don't go nightclubbing I'm an introvert!
     
    Ony likes this.
  14. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    And yet you are commenting all the time how ...
     
  15. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Oh god, yeah - I've wait hacked.

    I also used to have a lot of
    Code (csharp):
    1.  
    2. IEnumerator Start(){
    3.   yield null; // wait a frame because if i don't something bad will happen
    4.   ...
    5. }
    6.  
    which naturally led to...
    Code (csharp):
    1.  
    2. IEnumerator Start(){
    3.   yield null;
    4.   yield null; // wait two frames because if i don't something bad will happen
    5.   ...
    6. }
    7.  
    My first forays into Unity had a lot of stuff like that. I eventually got much more organized about how I handled object initialization, but waiting a frame as a hacked script ordering was something I used to do a lot (I hate script order).

    My worst hack was... using the native charactercontroller component as a basic character collider/gravity control. (A hack in itself)

    Animations were using rootmotion and a couple animations would ignore gravity. So, when certain animations fired, guys could hover above the ground. This led to a ...

    Code (csharp):
    1. // something like this
    2. private void Update(){
    3.   charactercontroller.Move( Vector3.down );
    4. }
    Yuck. I spent so long trying to figure out what the cause of that was.
     
    Kiwasi, AndersMalmgren and iamthwee like this.
  16. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Wait in start and awake is often a indicator that you are doing things wrong. Awake should never have external dependencies for example because you cant know they are ready. My examples are a bit different. Still hacks :D
     
  17. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I didn say that. I said you should comment why not how. And 99% of all comments are how. Which you shouldnt comment. Thats documented by the code itself
     
  18. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I never comment how. Thats just noise.

    //Do something
    doSomething();
     
  19. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    I just see on the forum. Always commenting how and why :)
     
    AndersMalmgren likes this.
  20. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    haha, thats another kind of comment :D
     
    Antypodish likes this.
  21. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    Yah, I am glad you got that joke :)
     
  22. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    agreed, I started building organization for initialization sequence later.

    and you can't wait in awake, only start with ienumerator. :p
     
  23. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    You cant? ah, didnt know :D Strange they made it inconsistent.
     
  24. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    awake is synchronous with instantiate, start is called in framework after (next frame iirc, but could be other). they behave very differently.
     
  25. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Awake and Start run in the same frame. Atleast for objects instantiated after scene load. It might be as you describe for objects existing in scene on scene load, it sounds familiar, but not 100% sure.
     
  26. steve-thud

    steve-thud

    Joined:
    Jul 18, 2019
    Posts:
    9
    The worst script I have ever written for a game was very recently. I had to implement a parallax scrolling background that also looped; so moving from the left side of the screen brings the objects from the right side of the screen back into view. It was to give depth to the scene so that it's easier for the player to track the character and what objects they can interact with with the football. It's a keepie ups game!

    This work was for a client, and I had 4 weeks. I was working on the game by myself, so I didn't need to worry too much about my code being interpretable by human beings.

    Even with these conditions I'm still ashamed at what I birthed. It's by far the worst thing I have ever created but I had hurt my head so much trying to figure out the maths that I just winged the numbers until it worked. I didn't have any more time to be able to properly refactor the parralax system, but it works and that was good enough for the time constraints I had. Good enough until I remember just how bad it was. Here's a sneak peak:

    Code (CSharp):
    1. float awesomeNumber = Mathf.Lerp(shmentityWorldSize, entityWorldSize, awesomeBias);
    2. float magicNumbers = ((awesomeNumber / bloopityBloopNum) -100000) + squeakyWorldOffset;
    3.  
    4. newPos.x = Mathf.Lerp((((int)(entityWorldPos / awesomeNumber) * awesomeNumber) + primeXCoords[i]) + magicNumbers;
    My sanity really began to deteriorate by the time I started using magic numbers.

    plz no ban
     
    Ony likes this.
  27. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Should have just deleted them. :p

    Somewhat ashamed to say I still use that one. If I get to more than one wait a frame line I do proper sequencing. But it seems overkill for just one frame.
     
    AndersMalmgren likes this.
  28. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,365
    I once wrote something along the lines of:
    Code (csharp):
    1.  
    2. public Animator animator;
    3.  
    4. void Start()
    5. {
    6.   if (animator==null)
    7.   { animator=getComponent<Animator>();
    8.   }
    9.   if (animator==null)
    10.   { animator=getComponentInParent<Animator>();
    11.   }
    12.  if (animator==null)
    13.   { animator=getComponentInChildren<Animator>();
    14.   }
    15. }
    16.  
    17.  
     
    Last edited: Nov 19, 2019
  29. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    Such pretty and looks functional. But most importantly, did it work? :p
     
  30. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,365
    Yeah. It's just a workaround for wanting to quickly slap the same component on a bunch of different compound objects that were all set-up haphazardly. I'm sure it was for a game jam.
     
  31. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,125
    You forgot to add FindObjectOfType so it's not quite foolproof yet.

    And could be improved even further with Resources.LoadAll.
     
  32. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,365
    If it were actually fool-proof than it wouldn't be my worst hack. ;) I'd say a real hack is one that breaks under certain indeterminable and unintuitive circumstances but just happens to work in your one use-case.
     
    SisusCo likes this.
  33. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I think you need to add at the end:

    Code (csharp):
    1.  if (animator==null)
    2.   { animator=AddComponent<Animator>();
    3.   }
    Just for completeness sake.
     
  34. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I do something similar to find nearest mesh renderer from a collider to paint bullet holes on it :) but it's editor time script atleast :)
     
  35. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    How about
    animator = Animator.Instance;

    Just in case there's one out there...

    edit: Oh wait, Animator is actually a unity class. This doesn't work so well anymore.
     
    Last edited: Nov 20, 2019
  36. TurboNuke

    TurboNuke

    Joined:
    Dec 20, 2014
    Posts:
    69
    One of my favourite hacks is something you don't really get a chance to do any more.
    Late night, the courier is there waiting to take the final version of an Atari ST game to the duplicators. Someone had recently found a rare bug which causes a crash.. I tracked it down and something is writing a single byte in to the code in a function.
    Spent a while trying to find the cause, but there was no time, so I reserve some space ( ds.b ) before the function so that whatever is writing to memory is now writing to unused memory! (Luckily, we always kept a few k free for this sort of last minute nightmare!)
    Fixed the bug afterwards, but the game was duplicated and released in this state. :)
     
  37. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    I was making this platformer game based on curved surfaces and this ball that rolled/bounced around, and I gotta tell you, trying to keep the ball anchored to the surface while going along it, trying to not jolt at intersections, trying to get it to land properly after a jump and all that, I had such a horrendous hack of physics colliders and raycasts and all this "just in case" stuff it was ridiculous, took more than a month just to get it to seem to work almost right. Seemed like a horrendous solution just to try to get the physics to do what I intended.
     
    Ony and steve-thud like this.
  38. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    We do a lot of direct memory management in process engineering. So many nightmares!
     
  39. TurboNuke

    TurboNuke

    Joined:
    Dec 20, 2014
    Posts:
    69
    From a similar time, ie. Amiga or ST: this was a good hack. We had our art formatted in 320x200 'sprite sheets' (DPaint!) with hand written lists of offsets and sizes for each frame. One frame had a stray extra pixel in the corner of the frame.
    The artist refused to fix it, so I ended up having to 'poke' the pixel away in code at runtime once the sprites were uncompressed in to memory. Genius.
     
    Ony likes this.
  40. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Aah, good times when you wrote directly to the gfx memory to display images. All kinds of fubar when you made errors :)
     
  41. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    Other than hacking my thumb's skin with a brand new kitchen knife by accident the day prior to an initial (heavy) debug session 1 week prior to a soft release?

    Mmm... It's actually hard to say because most of those "hacks" could also simply be pointed at as "lack of knowledge or skills" as whenever I have the right understanding, I'm usually finding solutions that aren't even on the web because I dig deep into everything by myself. (Yeah... even in my thumb.) Either than, or I put the project on a hiatus if possible.

    I remember vaguely a comic (I think it's a comic from VG Cats) where it state that no matter how much you paint over poop and put gloss and beauty over it, it's still poop in the end. I feel kinda like that with my codes. If my code feels like poop, it's usually means it's not just a feeling and I should find a way to remove such thing from my code.