- Home /
Code Optimization, Variables local or on Parent.
I am making a Hexagon based game and I am coding the Hex's atm. 1000 Hexs per scene, with a parent called HexMaster.
I have them working how I want but I have a question regarding Optimization. Atm I have two scripts Hex and HexMaster. Hex is applied to every Hex and HexMaster is applied to the single parent HexMaster.
The Hex script currently stores all the variables about the Hex it is attached too as well as all the functions that work out the variables. The HexMaster script just initiates the functions on each Hex.
From a laymen it appears that this approach is not good as my 1000 Hexs have 1000 copies of each function in the Hex script.
I am considering storing all the variables about all the Hexs in the HexMaster as multi-dimensional arrays rather than as single arrays on each Hex. As well as moving all the functions over to the HexMaster.
Is this going to be more optimized or is this sort of thing optimized when the code is compiled?
Thanks
Sounds unnecessary complicated using multi-dimensional arrays for Hex data in the Hex$$anonymous$$aster, when you can easily store each individual Hex in a array, and access the Hexs' data through function calls from the Hex$$anonymous$$aster.
Answer by snakeylake · Dec 05, 2012 at 07:31 AM
First thing to ask is if it should be optimized?
To know if your optimization is successful, you need to be able to profile it.
If you don't have a copy of pro, you should be able to profile your script with a timer.
If you have 1000 separate monobehaviours updating doing the exact same thing it will probably be faster to do the whole calculation on the HexMaster.
Before doing a complex optimization that is harder to write, and makes your script harder to maintain, you should probably figure out if you're slowed down by draw calls or algorithmic complexity. ( 1000 simple updates probably shouldn't make your game run slow )
"If you have 1000 separate monobehaviours updating doing the exact same thing it will probably be faster to do the whole calculation on the Hex$$anonymous$$aster." Thanks I guess that is what I needed to hear.
Drawcalls are not an issue atm the Hexs are just empties. I have already optimized my algorithms as much as I can so the only thing left to optimize is the way the Hex functions and variables are stored.
I don't have any slowdowns its just that I am adding more complexity and I need to make sure the foundations are sound.
Thank you.