Looping through children vs Script on each child

unrelated to needle
kinda related to unity itself

wats more efficient?
objective is to move a bunch of child objects independently on button press

1- having a script on a parent, loop through children and move em

2- each child has a script, which moves itself on trigger

there are around ~200 child objects

Original Post on Discord

by user 389432819056771072

Answer as so often is “it totally depends”
Keep in mind that basically when you have a manager to manage 200 child objects that’s mostly identical to having 200 scripts since those are just as well managed by something. So depends on whether your logic for managing them is smarter / better / more efficient than just a general “update loop manager” that every engine (no matter if Needle Engine or Unity Engine) has anyways

Does “independently on button press” mean: they all move at the same time? Or each one has a button

they all move at the same time
just have different coordinates to goto

by user 389432819056771072

rn wat im doing is

on start creating an array of a class which holds an object, and two vector3s, lets say a and b, a being the current position

every frame im checking if the button is pressed or not (the button itself changes a global variable, every other object look at that global variable to kno the state)

if yes, then loop through each child and lerp to position b

other way around would be, to assign a script to each child, will check the global variable (this part i can probably eliminate using sm tricks, so can save sm performance there)
and then just lerp

eliminating the for loop

i just cant figure which one would me more efficient

by user 389432819056771072

In general, with questions like this, the answer is “profile your specific case” :slightly_smiling_face:

@herbst🌵 didn’t i hear a few weeks back that you implemented some of the entity/component system stuff in the engine? In my experience you can’t go wrong separating your data from your code if you want performance.
If your data is packed properly, the system can use SIMD or vector instruction set to loop over it quickly. If your data is scattered around, cache misses will kill your performance even if there is no explicit ‘loop’

by user 563549194137239552

Not sure what you heard!

Data oriented approaches can definitely be faster, and a lot so, but it still requires careful planning and data layout and making sure that introducing the data orientation doesn’t introduce slowness by itself. Not saying that’s wrong :slightly_smiling_face: but rather than giving a blanket “the manager will be faster” response I still think this should be profiled, and the additional (mental) overhead of thinking in systems and entities can make it not worth it too. Harder to debug, maintain, and change, etc, so can’t give a blanket recommendation :slightly_smiling_face: