Is it Better to Combine my Unit Movement Script with My Targeting Script? (RTS)

Hi guys, so I have 2 Scripts ( Movement and Targeting ) , and both Update pretty regularly, also both scripts iterates through so many different Lists.

Now the thing is both scripts need to reference each other, for example, my unit Movement Script needs to know if a target is " != null" thus if its in attack move or patrol, it would do something different. etc etc

I would rather keep the scripts separate because they are both so big and I dont want to be confused, but in terms of processing, will this cost me much? Keep in mind both scripts will be in constant communication and also both scripts are on every unit. Atm, with 500 Units all on patrol, I have reduced to 50 Fps, and i cant afford to go lower.

I ask this because I have no idea if its taxing when scripts reference each other.

Thanks in advance

Chances are, you don’t need run them every frame.

Keeping separate scripts for movement and targeting is right approach.
Easier to maintain.
You just need some script, which will marry both of them. Kind of manager.

What you can do, is instead having scripts on each GameObbjects, have list/array of all life units, run for loop and execute logic in that way.

In that for loop, you can add methods, like movement and targeting. And more methods, while keeping all in own separate files.

@Antypodish , Thank you for reply bro

Its not every frame, but theres a lot of things going on, considering its an rts game thus many events and triggers.

sounds like a good idea but, Would This not slow it down?

This is not an option when it comes to looking for your target based on distance, (it works if you have relatively low amount of units), But when you have Large amount of Units (1000) and enemy has large amount of units (1000), what will happens is that you will have 1000 units iterating though a list with 1000 entries. and the FPS drops to <5 (this is what happened on my first attempt - not to mention the iteration was capped to once per second and that is very slow for and rts game). However, I have worked around that. But as a result every unit comes with his own dedicated list. Its messy but it increases my fps from 10 to 50.

But this game is still in its first month of development, and thus 50 fps is too low considering there is hardly and graphics and many systems that are not even implemented yet - This is why I am trying everything to increase performance, even if it means merging 2 large scripts.

Question is; does it actually effect performance if both those scripts where merged into one? Trying it just to find out will be too much work, so I would love to know before hand.

Thanks in advance

Performance is rarely related to how many scripts you have.
If you have change in performance, because single, or multiple scripts, you have serous design flow.
If you marge script, you will run soon into maintaining issues.
This is where you should use methods.

Is up to you, if you want keep methods in same, or different scripts.
But try to avoid one singular large method, if possible. And for your case, is 100% possible.

Weather you run scripts on GameObject with Update, or via for/foreach loop, is pretty much the same thing. If you see difference, then you again have somewhere else issue. The only reasonable answer is, if you disable / destroy GameObjects, means less to Update. And contrary you don’t maintain active GameObjects in a list correctly, which means, you process inactive GameObjects by iterating through list.

@Antypodish ,

Your right, but the reason why the fps is higher, its because my List Only populates what is within range of that individual unit/Object, meaning; Instead of one massive list, I use many very small/empty Lists. List remain empty unless a Unit gets close enough to my objects radius. (this was my solution and I couldn’t think of any other way)

ok I will keep them separate, but, what do you exactly mean when you say “methods”.

Thanks again

In C# called methods, in other languages may be called functions. That may clarify.
Principle is the same.

You still can have smaller lists of targets, for each unit.
But you can iterate through all units in one for / foreach loop, and check for targets that way.
If that makes sense?

How you check for targets btw?

@Antypodish ,

no that doesn’t make sense to me, iterating through the large list is whats so taxing in the first place, having both the “large” list and small list on top of that makes no sense to me, since you only need one. but I think I missed your point lol.

,

with “OnTriggerEnter”, its very efficient

You move all units right?
So you need iterate through all of them anyway.
Or otherwise Update does that, for each GameObject, having Move script.

But you don’t need use single loop list, if you are not comfortable to use it. In the end, it works for you, as you got.

Yep, and for your case and simplicity I would stay with that.

Try to split your larger methods / functions, into smaller scripts and methods. Ensure you follow Single Responsibility Principle.
If you not aware of this approach, read on internet. It will help you on long run.
Profile it and check, where is your current bottleneck.
Do you use Profiler?

You should find out, which part of your scripts takes much of computing power.

Let us know, if you stuck at any point.

@Antypodish ,

Thanks again bro, Never Used Profiler before, I should look into that, but atm I am facing another big problem.

I will make another post about it.