mattrdowney / planetaria

A Unity framework for Euclidean 2-sphere games (e.g. 2D virtual reality games) [quasi-MIT license]
Other
10 stars 2 forks source link

Resource Acquisition Is Initialization (RAII) #125

Open mattrdowney opened 5 years ago

mattrdowney commented 5 years ago

How to implement: Create a tag for any component that wants to spawn a hidden object. That tag will be the global unique identifier of the component that is the parent (using https://answers.unity.com/questions/33597/is-it-possible-to-create-a-tag-programmatically.html). Whenever the object changes (Reset, OnValidate, Destroy), check for validity - easiest way is to destroy all children and re-add them.

Previously referenced in https://github.com/mattrdowney/Planetaria/issues/122

mattrdowney commented 5 years ago

Editor fixes aren't useful. This needs to work at runtime, so the component must delete its dependencies (or change them) as necessary.

Global unique identifiers (GUIDs) don't exist at runtime, so don't go with that solution.

mattrdowney commented 5 years ago

If a required object already existed, do not delete it on destruction. Do not hide the object either. If the required object does not exist, create it, hide it, and delete it on destruction.

mattrdowney commented 5 years ago

If the required object exists (but multiple objects of that type can exist) do not reuse the existing object.

mattrdowney commented 5 years ago

Unity's Entity-Component System pairs well with PlanetariaRenderer (I think).

Useful tutorial: https://m.youtube.com/watch?v=QD2DpeuOrS0

The main advantages: 1) you can attach multiple PlanetariaRenderers per object, 2) you can have an individual Entity per each renderer (which can be deleted as RAII cleanup so long as the Entity is stored), 3) OnValidate can create a hidden GameObject child in editor-time, but when play is pressed in the Editor you can use preprocessor directives to unhide them, 4) the bootstrap system should work out of box.

mattrdowney commented 5 years ago

You still have a hidden object, but you should seldom if ever reference it directly.

You can have renderer facilities for float offset, float2 scale, bool flip_horizontal, bool flip_vertical, and a tag for the PlanetariaRenderer (and its type) - all of which the Entity-Component System can control.