Search Unity

What is more efficient

Discussion in '2D' started by chukk, Jul 22, 2016.

  1. chukk

    chukk

    Joined:
    Jun 28, 2016
    Posts:
    11
    i would like to know how you handle the scripts.

    when i have different objects that work similar, it is easier to make one script with many variables, so i can change direction of the shots,damage or something like that. But that means more work for the pc or does it make no dfference when i make more small scripts for each object that it similar instead of one more complex script?
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    @chukk

    Use your discretion to decide whether to make a new script or to reconfigure an existing script. Usually, no matter which angle you consider, the difference in performance isn't significant.

    In theory, creating another script instead of reusing one will increase the size of your game (space on disk), but you're not likely to notice the difference in impact compared with larger files like textures or videos. An exception would be if the script was really massive.

    No matter which method you pick, the system has to execute through an instance of the script used on the object. Since both scripts are similar, we can reason that their execution times are likely similar, so the processor load won't really change either. An exception here is if some expensive logical loops are added or removed.

    For manageable code, I think most would argue that you should try to keep the number of scripts to a minimum, and to put a bit of special effort into making your scripts generalizable. That said, sometimes that just isn't feasible; techically you could create just ONE script to run every single thing in your game, but good luck maintaining that (and that probably would cause noticeable processor load due to running a bunch of seriously huge scripts at the same time).

    In my game, each enemy uses a common damageProperties, targetProperties, and targetItemDrops, scripts to name a few, and each can be configured in the inspector for use on a specific target. However, each enemy has its own unique AI behavior script and AI state script, most of which are loosely based on template scripts, then modified. It would be hard to use the same behavior script for and enemy that slinks along the ground and for an enemy that darts around in the air.

    The fact that you're asking this question makes me think you have good instincts, so I'd recommend just going with your gut and remembering that both options are usually acceptable. Remember to consider manageability, and happy coding.
     
  3. guzzo

    guzzo

    Joined:
    Feb 20, 2014
    Posts:
    79
    One complex script should be faster, mainly, because you probably would have less method calls (each method means one address jump, the creation of his own stack, execution and jump the return address). However, the performance difference between both alternatives is small and you should choose the "happiest" choice.
    Then, I would recommend you to be modular, making different scripts for each behaviour. It will be easier to mantain and reuse in the future.
     
  4. chukk

    chukk

    Joined:
    Jun 28, 2016
    Posts:
    11
    thanks for the explanation. really good answers.