[Update]Unity, can I make small scripts for everything?

Started by
6 comments, last by Nypyren 6 years, 1 month ago

I am making Inverse and Forwards Kinematics and I noticed that Unity uses small scripts for it's joints. I was wondering if I could use small scripts on every joint; instead of one script that solves all joints.

Assume we have 12 chain objects, what would be better?

 

I could make one script with a public array, I add each chain into the array and the code solves the math using loops.

A small script, that wants a target and then makes the object follow or push the target. This way, each object will need there own script to solve there own math.

 

Looking online it appears most Unity users recommend using the small scripts but I am very worried over performance. Each player can have a max of 12 joints with a max of 24 players. So in the end it could be 288 small update calls VS 24 update calls.

At the moment I want to go with the small scripts, making each object follow a other object is easier this way.

 

Advertisement

I'm a fan of the small scripts approach from a cleanliness perspective. I don't think you'll run into performance issues so long as each script is relatively light. YMMV on mobile :/

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

For most projects you can do as an individual, you will be best served with small, easily understandable scripts.

In large projects there are often reasons to consolidate functionality.  There are costs to iterate, costs to look things up, costs to hop around memory.  Big projects cannot afford those costs, so they make more complex systems to reduce the actual work.

I remember the developer for Yandere simulator had like 200 c# scripts in unity and was complaining that it took 2 hours to compile everything. You can try it for the sake of some sense of abstraction/structure, but like others have said, you'll hit a (compile/cognitive) cost sooner or later.

I am not 100% sure, but I think that if you do not define an Update or LateUpdate function in the script, Unity will not call (even a virtual) update function. If this is the case, you can have hundred of thousands small scripts attached everywhere in your scene, it will only cost you memory. As soon as you add an Update function, well that is when things will slow down to a crawl.

For most games, this will not be an issue, imagine a game where you never have more than 20 or 50 enemies at a given time, the performance of those enemies will most likely not cause any trouble.

I would advice to do some research on the subject because right now, you are making a decision without any empirical data. So I think you should create roughly the number of game objects you expect to have in your scene at once, then add roughly the number of scipts you think you will have and run the profiler or just check the "FPS" counter and see how well it runs, then add more objects until your system drops below a tolerable FPS rate, which is usually lower then 60 or maybe 30, depending on your game.

If you choose to do this, please report back to this thread with the result, I think it would interest many people.

Hi, Update on what I did. So after I made the IK script I realized there is no reason not to test both methods and see what does best. My 7 year old laptop was used as I am traveling at the moment.

 

Method: Made a small IK script and a Large IK script, then used the scripts to make a 2000 IK chain.

Large IK script: This uses a single script that uses an array to hold each object. The code then uses Index-1 to find the head and Index+1 to find the tail. It loops every Item in the list every Update() to solve the IK math.

Render Time: Averaging  54 FPS.   ||     Memory: 211 MB

Small IK script: Using a very small script I allow each object to solve it's own math in it's own update(). It's script has a Public Gameobject for the head and tail that is hooked either manually or with a spawn script.

Render Time: Averaging 72 FPS    ||     Memory: 416 MB

 

Amazing right? The small script actually rendered faster, the exact opposite of what I feared would happen. The down side is more memory.

The memory is mostly from Vectors and Quaternions, the small script causes every object to make there own variables where the large script re-uses it's variables.

What does 2000 IK chain look like?:

ChainFull.thumb.jpg.1db4943474375a4ff41dd76d85cd9414.jpg

Is it really IK? Yes but only the very basics, I need it only for small animation purpose.

IKSimple.gif.360e9c8987046f4d1dcb480da30e1192.gif

WAIT! Isn't the color adding to the memory? No, I added it after the test to make these images you see more interesting.

 

In the end I decided to go with small scripts, the extra memory is very small for a desktop PC and it's only 102 kb per object I add. Since my worst case is 12*24 it's only going to be 28.8 mb.

On 2/28/2018 at 1:01 AM, swiftcoder said:

I'm a fan of the small scripts approach from a cleanliness perspective.

This is one more bonus to doing it this way. Not only is it easy but it keeps everything in there own neat little class that is easy to use and read.

On 3/1/2018 at 12:00 AM, frob said:

In large projects there are often reasons to consolidate functionality.  There are costs to iterate

This is correct. The costs of small scripts is accumulative.

Although 205 MB isn't a lot for a desktop PC, in a large game it can be what makes or breaks the game and this memory is often better used elsewhere. Also this was only the most basic of IK calculations if I added Polar and tension the memory costs would more than triple.

It looks like for complex and large games, using a large script would be better.

On 3/1/2018 at 6:34 AM, ddengster said:

I remember the developer for Yandere simulator had like 200 c# scripts in unity and was complaining that it took 2 hours to compile everything.

Ah, this was something my test could not reveal, thanks for pointing this out.

In my test I was just using the same script 2000 times, so no compile errors. It looks like a lot of different small scripts will lead to long compile times. Something to keep in mind as the game progresses.

16 hours ago, flodihn said:

For most games, this will not be an issue, imagine a game where you never have more than 20 or 50 enemies at a given time, the performance of those enemies will most likely not cause any trouble

And this is why I feel that small scripts is a fantastic way of doing things. In most games you won't have thousands of a script, only a few copies at a time.

16 hours ago, flodihn said:

but I think that if you do not define an Update or LateUpdate function in the script, Unity will not call (even a virtual) update function.

I did try this and it only worked with a inheritance. So each script must call it's Update() to update.

 

Although my test shows the large script was rendering slower, I think the For Each Loop is to blame, because I run it once every frame.

The time the loop takes to solve is added to the calculation and even if it's small it will be added each frame, resulting in a accumulative.

 

What I believe is: that if there was no loop and 2000 math problems where solved in 1 Update() call, it would have the same cost as running 1 math problem in 2000 updates.

In other words there is no extra cost to having more Update() calls.

On 2/28/2018 at 8:34 PM, ddengster said:

I remember the developer for Yandere simulator had like 200 c# scripts in unity and was complaining that it took 2 hours to compile everything.

We have several thousand .cs files in our projects at work and they take 20 seconds for the code to compile in the editor (without any use of asmdef files or precompiled DLLs), and 10-15 minutes for the iOS IL2CPP build.

Most of the time of large scale Unity builds is taken up by either image compression (PVRTexTool, I hate you!), AssetBundle creation, or the IL2CPP conversion process.

This topic is closed to new replies.

Advertisement