Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring parameter templates to create new entities from?
    primarykey
    data
    text
    <p>My entity system for my game uses templates to lay out how entities are created. Something like:</p> <pre><code>EntityTemplateManager.register("zombie", new PhysicsComponent(10), // speed new SpriteComponent("zombie"), // graphic new HealthComponent(100) // health ); </code></pre> <p>I like this part of the system because it makes sure I don't miss any parameters or screw up their type.</p> <p>Then I can create a new entity like this:</p> <p><code>entity : Entity = EntityTemplateManager.create("zombie");</code></p> <p>Pretty straight forward.</p> <p>What I do now, to actually create the entity, is have the EntityTemplateManager create 1 entity to use as a default, then I call clone() on the entity and it goes through and clone()s all of its components and returns the result.</p> <p>Actual code:</p> <pre><code>public function clone() : Entity { var components : Vector.&lt;Component&gt; = new Vector.&lt;Component&gt;(); var length : int = _components.length; for (var i : int = 0; i &lt; length; ++i) components.push(_components[i].clone()); // copy my local components to the new entity return new Entity(_name, _type, components); } </code></pre> <p>The problem is, every time I create (design) a new component, I have to write (design) a clone() method inside of it AND keep track of all the parameters that were called on the constructor to create a brand new, default stated component.</p> <p>So I end up with junk like this:</p> <pre><code>public class ComponentX { protected var _defaultName : String; protected var _defaultStartingAnimation : String; protected var _defaultBroadcastAnimationEnded : Boolean; protected var _defaultOffsetX : Number; protected var _defaultOffsetY : Number; // other stuff hidden public function ComponentX(name : String, startingAnimation : String, broadcastAnimationEnded : Boolean = false, offsetX : Number = 0, offsetY : Number = 0) : void { super(); _defaultName = name; _defaultStartingAnimation = startingAnimation; _defaultBroadcastAnimationEnded = broadcastAnimationEnded; _defaultOffsetX = offsetX; _defaultOffsetY = offsetY; } public function clone() : Component { return new ComponentX(_defaultName, _defaultStartingAnimation, _defaultBroadcastAnimationEnded, _defaultOffsetX, _defaultOffsetY); } // other methods } </code></pre> <p>I really don't like this -- it's wasteful and error prone.</p> <p>How could I best store the parameters of the EntityTemplateManager.register() function (including the components' constructors parameters) and use that new storage to create entities from instead?</p> <p>I've tried some generic clone() methods like with a ByteArray or describeType(), but they don't work with protected / private variables.</p> <p>Ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload