-
Notifications
You must be signed in to change notification settings - Fork 3
Description
In Tiamblia I made a shortcut ('R') to regenerate the selected entities, if they use any randomness during construction.
It works by creating two new entity instances, comparing what properties are different, and copying the changed properties over to the original entity.
Caveats
It's not perfectly general; it has some ad hoc logic that says to replace arrays and Structure
s wholesale, and it could fail if the entity contains references that only make sense in the context of a specific instance. I'm not sure what that would look like exactly, but it feels like it would come up. I guess one case would be if you stored a reference to an item in an array, e.g. @sides = [@left = {}, @right = {}]
If the array is replaced wholesale, but the other properties which reference items within the array are not (as they're merged instead, being objects), the references could end up broken.
Encapsulated state like private properties might cause problems.
However, it's a pretty elegant algorithm with very short code, and it worked for regenerating tree entities in Tiamblia.
If you stick to easily serializable data, like storing indices into an array instead of references, you'll probably be fine.
UI
- Shortcut 'R' (stands for "Randomize" or "Regenerate" or "Re-Roll")
- Context menu item 'Randomize', enabled only if some selected entities are randomizable
- Dice icon in sidebar to re-roll entity without placing it, shown only if entity is randomizable
Tiamblia also has a less general "Auto-spawn entities" feature, which I should probably also support in some capacity, since it's also accessed with 'R', but when no entities are selected. This could be a "Randomize Scene" item in the context menu when no items are selected.
API
Can detect whether an entity is randomizable by creating two instances and comparing properties just like in the randomization algorithm, although
Can provide a hint in entity definition like randomizable=RANDOMIZABLE|NOT_RANDOMIZABLE|..?
AUTO
? wait was there a reason I was thinking of using an enum just now other than having an "auto" state, which would be the default? Maybe MANUAL_ONLY
, implying it won't do any randomization by default, but procedural random generation can be triggered by the user?
randomize
method on Entity
that can be overridden, called when triggering randomization in the UI. You could also call this in your constructor, as one pattern to share code.
randomizeScene
method on World
?