- Home /
Basic Architecture / Handling many GameObjects with different update strategies
for my new project i tried some different approaches in the last weeks but with no good solution. here are my "requirements".
there will be many "Levels" with many objects. the player only can be in one level at a time, but all other levels needed to be updated (details below). most objects of a level are static and wont even need to be updated. i guess 70% are static and dont get ever moved and very very rarely updated. 10% will be static but needs to be updated(details below) and the missing 20% will be movable. one level could hold 15.000objects or even more.
what is an object: its an entity that will be managed from an scenemanager -> levelmanager and can represent a simple object like an Asteroid or some obstacle. but it can also a "root container" for a much more complex thing like a station. in this case the station is in charge to handle updatestuff for its child components(not Unity Component)
the active "Level" where the player is currently in most objects need to be updated with every frame (Behaviour update). some can be updated within a different (fixed)Update. all other objects that are in levels that are currently "inactive" will be updated in different (fixed)updates depending on the objects and there status.
if a level is intactive, there is no need to have a GameObject (costs lot of memory). for a bunch of obstacles that will not move, i can just create a root object and hold there position/rotation. So all "business logic" like generation of resources over time has to be done within this objects. For objects that can move, i have a Manager that will take care of the movment (simple position translation and no physics or so).
in case that the objects are in the active level, i have to change some behaviours. updating resources will still get through the scene/level manager. In Case of moving objects the "Movementsmanager" has to switch from simple translation to a more complex system. for every Object (and children) there is a corresponding GameObject that is linked to the Object and has some Components for movement/interaction and stuff that is only releveant when the object is visible for the player. in case of the player SpaceShip (game)object there is a component that has methods for movement and interaction with other stuff. it also can hold Components to visualize things like shields and so on. but all operations like shieldenergy update/recharge are NOT a Unity Component.
for example a object Ship(class) can hold a WeaponManager, EnergyManager, CargobayManager,... that gets updated from a LevelManager. when the Ship is "active" and can be seen from player(is in active Level). there corresponding GameObject has a Component to "trigger" the Guns or to dump the cargo. therefore i link the Component with the Object and the machting Manager.
the benefit is that i can manage/update/use all objects without using a GameObject or even Unity. but its very OOP and that could be getting tricky when creating Prefabs that will match to an Object (Gun Positions and so on). Also i love the style of drag and drog a Component on a Prefab and say "ok now u get a Shield". so everyone can configure a Prefab like a ship. but in this case i have to hold every Object as an GameObject. even when it is disabled it takes much memory and from my tests there will be more than 1.000.000 objects. a hybrid approach could be that i build my own Component based Structure that will fit my needs. (Collection of IObjectComponent?) but still with loose of the Drag and Drop. or is it possible to get the UnityComponents from an object and use it, even if the GameObject is destroyed?
to avoid the problem with (float)jittering i use the floating origin approach with 2 coorinate systems. 1: "subLevelSpace" has are range from -6.000 to 6.000(example) and will positioning the object within a LevelSpace. 2: LevelSpace: is a Vector3 with INT and acts like a multiplier from the max SubLevelSpace. Example: object has SubLevel: 100,0,50 and LevelSpace: 60,2,0 the absolute coords will be: 360.100,0,50.
so my Questions are: - What is the better/best approach handling the (game)Objects and the corresponding Components? - Is the approach of having 2 Coord. systems in relation with floating origin ok?
Thank you in advance!