Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Maximum Script length ?

Discussion in 'Scripting' started by MrZeker, Jun 11, 2019.

  1. MrZeker


    Nov 23, 2018
    Hello, i have a question that i didnt find any answer on internet (i mean, i found, but i found but werent any good, just opinions withouth facts)

    I have a main script in my game that is ATM on 4000 lines. and im about to add the passive skill triggers, which i believe will take me around 500 to 1000 lines, considering that i have to take into account a lot of possiblities.
    My quetion, does the lenght of the script slow things up? like, would it be better to have everything in one script ? or have in two and find a way to call functions from one script to the other?
    and is it detrimental to performance that a script is 5000+ lines?
  2. csofranz


    Apr 29, 2017
    Well, the number of lines is a bad way to judge the size of a script, usually it's number of charaters. That being said, let's say your lines average 30 charaters per line, a 10'000 line file would be 300'000 Bytes in size, or, 0.3 MB, the size of a very small image.

    No, file size is not going to be an issue.

    Now, you ask if that would be detrimental to performance. Since scripts are compiled, during compilation most offsets (for branches, jumps, invocations, array access) are pre-calculated. So, no again. The Initial compile will be impacted by file size, of course, but that is not a concern during execution, plus a large single file will probably compile slightly faster compared to the same code split into multiple files. But this has no Impact during execution.

    Historically, I believe that the last time I cencountered a file size maximum (32'000 bytes) was some 30 years ago, on a Motorola 68000, where some lazy bum used a 16 bit index Register for adressing. Mayhaps that bum was me.
    Last edited: Jun 11, 2019
    MrZeker likes this.
  3. kdgalla


    Mar 15, 2013
    Your code is eventually compiled into machine code, which bares no resemblance to the code you type. Whether you have 5000 lines in 1 file or 2500 in each of 2 files is completely meaningless.

    You should separate your code into scripts in a way that is logical to you, the programmer.
    MrZeker, angrypenguin and TonyLi like this.
  4. TonyLi


    Apr 10, 2012
    Generally speaking, an extremely long script increases the possibility of dropping your performance to zero frames per second if it becomes too long for you to manage and introduces a bug that breaks the game. ;-)

    As kdgalla writes, organize your code into units that make it as easy as possible to understand for someone else -- or yourself after 12 months away from it. This usually means smaller files that each serve one purpose and offloads details to other scripts.
    MrZeker and angrypenguin like this.
  5. MrZeker


    Nov 23, 2018
    Thanks everyone for the replies!
    im gonna keep working on my omniscript then. it is really easier for me to look everything in the same script, and im working alone, but the point about no more than one people can work on the same script is somethng that i will keep in mind for the future.
    again, thanks for the answers, i was worried i was making the game unnecesary slow.
  6. Kurt-Dekker


    Mar 16, 2013
    If you want to make incremental steps towards breaking apart a single giant Monobehavior script, you can declare it as a partial class, then start physically pulling parts of it out into a different C# file, but one that is ALSO a partial class with the same name.

    This is often an easy way to start making headway into massive scripts that need refactoring. Once all the functions related to X have been pulled out, you can make a fresh class with only X stuff in it, and continue with the next area of general functionality.

    ALSO: I highly recommend you use source control (git works great) so that if you slip up and damage something, you can recover trivially easily.
    SparrowsNest and lordofduct like this.