Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help reducing number of IF statements ...

Discussion in 'Getting Started' started by Shin_Toasty, Dec 6, 2017.

  1. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    I'm scripting for several different enemies all with different names and different spawn points, so I'm having to use several if statements like this:

    Code (CSharp):
    1.     if (respawnYolanda == true) {
    2.             Yolanda.transform.position = YolandaSpot;
    3.             respawnYolanda = false;
    4.         }
    Could I replace these statements with one which basically goes like this:
    if (respawnENEMIES == true) {
    ENEMIES.transform.position = ENEMIESSpot;
    and so forth, so that all enemies are respawned from their respawn points?
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Why do you want to reduce one of the best and fastest tools we have? Using lots of if's is only ever a problem if it impacts how well your code can be maintained.
     
  3. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    I think op wants to know a way to enhance maintainability.

    There is not enough info to answer though.

    If ie all your enemies inherit from an Enemy class that has a respawn variable you could stick them in an array and loop through it.

    For each enemy in enemies
    If (enemy.respawn)
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Agreed, some context would be a bit useful...
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You might also use a Dictionary to map enemies to their respawn flags, and another to keep track of their respawn locations. Then you could just iterate over the dictionary keys (enemies), and reset any that needs respawned.

    Or, maybe the first one doesn't need to be a Dictionary at all, but a simple List of enemies that need respawned. Whatever code is setting that flag could instead throw the enemy onto this list.
     
    Kiwasi, Ryiah and Shin_Toasty like this.
  6. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    Yes I am trying to enhance maintainability.

    The code I have written works using the multiple if statements works; it seems wasteful though.

    Also, this shows how to turn different cameras on and off using ifs and I have used similar code, but I have 10 cameras (never more than one active) and it seems wasteful to tell Unity to switch off the other 9 each time a button is pressed.

    I've viewed the official Lists & Dictionaries tutorial, but I don't get how to add enemies which already exist to a list, or a dictionary.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    First, declare them:

    Code (csharp):
    1. List<GameObject> demoList;
    2. Dictionary<GameObject, bool> demoDict;
    Then to add to a list, or set a value in a dictionary...

    Code (csharp):
    1. demoList.Add(someObject);
    2. demoDict[someObject] = true;
    Finally, iterate over them to do stuff...

    Code (csharp):
    1. foreach (GameObject foo in demoList) {
    2.     DoSomethingWith(foo);
    3. }
    4. foreach (GameObject key in demoDict.Keys) {
    5.     DoSomethingWith(key, demoDict[key]);
    6. }
    (There are other ways of iterating over a dictionary, but I think the above is easiest for a beginner to understand.)
     
    Ryiah and Shin_Toasty like this.
  8. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    Most of the time it is a trade between maintainability and perceived “wastefulness” - I say “perceived” because we typically do not know the compiler results.

    I learned programming on a c64 and still hesitate to write “easy” code, which we should with today’s computing power and only optimize if we have to. In the old days you knew at the outset that you had to.

    As for the cameras, I doubt iterating over the 10 creates any performance issue. But in case there is ever only one on, it would likely be simpler to store the active one and when you turn to a new one, deactivate the current one, and store the new one as active.
     
    Ryiah and Shin_Toasty like this.
  9. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    We should write easy code? Well I'm glad to hear that. I was trying to optimise my code even though the easy code works fine. If optimised code is 100 lines shorter no-one will notice the benefit in 2018. Making games has never been easier, so maybe I can be easier on myself. Lists and dictionaries will be useful elsewhere though.

    Talking of 64s, my Dad wrote a Vic-20 game in 1983 (and sold it for £20!) and I remember how he struggled to pack everything in; but things have changed since then.
     
  10. nat42

    nat42

    Joined:
    Jun 10, 2017
    Posts:
    353
    Actually while I hear tooling for C64/128 and even Vic-20 development is better now, people still complain it's a struggle to pack everything in.
     
    Shin_Toasty likes this.
  11. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    It must be even harder to get a decent cassette recorder.
     
  12. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,951
    Cassette recorders are almost never used now. You can almost always find a Commodore floppy drive on eBay at any point in time and the floppies themselves are still being manufactured by a company called Athana.

    http://www.athana.com/

    Beyond that though there are SD card drives for the Commodore. The SD2IEC is the cheapest but the Chamelon and the 1541 Ultimate both emulate the actual drives.

    https://www.thefuturewas8bit.com/sd2iec-c.html
    http://www.syntiac.com/chameleon.html
    http://1541ultimate.net/content/index.php?option=com_content&view=article&id=2&Itemid=134
     
    Shin_Toasty likes this.