TheSwamp

Code Red => .NET => Topic started by: Kerry on October 28, 2015, 11:29:06 PM

Title: Mind Boggling.
Post by: Kerry on October 28, 2015, 11:29:06 PM

There's a cautionary tale in this somewhere.

Don't assume that published code is correct or economical or even sensical.

This snippet came from a book (a CookBook of Unity3D code) published a couple of months ago ( and not cheap)

I've commented out the offending code and replaced the Update() method.

The code is meant to update text on a GUI overlayed over the game visuals. Every Update() method in every class script attached to every GameObject runs for every frame displayed ... that's about 15 <-> 80 frames per second. As you can imagine frugal code to get the job done is a benefit ; particularly when the Game screen gets busy ... lots of decisions need to be made to get that frame displayed "Am I dead" "Is this bling" "where are the zombies" "does the player want to turn  here, speed up, slow down , jump, run away, fight, etc, etc"

It scares me a little that some peeps are going to just accept this code and use it in every game they build between now and when they get a 'real job'.

What concerns me more is that a similar situation is happening with AutoCAD plugin code.

Stay well and prosper !

Code - C#: [Select]
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5.  
  6. public class ClockDigital : MonoBehaviour {
  7.        
  8.         private Text textClock;
  9.        
  10.         // Use this for initialization
  11.         void Start () {
  12.                 textClock = GetComponent<Text>();
  13.         }
  14.        
  15.         // Update is called once per frame ( nom 60 FPS. )
  16.         void Update () {
  17.                 textClock.text = DateTime.Now.ToString("HH:mm:ss");
  18.         }
  19.        
  20.         // Load of Crap !!
  21.         //void Update (){
  22.         //      DateTime time = DateTime.Now;
  23.         //      string hour = LeadingZero( time.Hour );
  24.         //      string minute = LeadingZero( time.Minute );
  25.         //      string second = LeadingZero( time.Second );
  26.         //      textClock.text = hour + ":" + minute + ":" + second;
  27.         //}
  28.         //string LeadingZero (int n){
  29.         //      return n.ToString().PadLeft(2, '0');
  30.         //}
  31. }
  32.  
  33.  

Title: Re: Mind Boggling.
Post by: JohnK on October 29, 2015, 08:37:49 AM
lol

I thought I'd leave this here (I know it's not the right language, but it's always nice to see how the professionals do it once and a while).

Doom3 source code review.
http://fabiensanglard.net/doom3/index.php
Title: Re: Mind Boggling.
Post by: WILL HATCH on November 02, 2015, 11:34:35 AM
Just a thought here with no time to examine how the call to tostring adds up, but you may have introduced extra stack operations in there to slow things down by making the code read so beautifully.
Title: Re: Mind Boggling.
Post by: Kerry on November 02, 2015, 05:10:56 PM

Hi Will,

Your comment reminded me I haven't actually tested the code efficiency.

I'd guess my alternative would shave about 75% off the process time
... and it satisfies my aesthetic tastes.  :wink: