dev:abstract_elements
                Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| dev:abstract_elements [2014/09/23 18:38] – created tsmall1 | dev:abstract_elements [2014/09/23 19:35] (current) – [Create abstract class] tsmall1 | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| It may be useful to abstract the behavior of your Elements. In this tutorial, we are going to create two Elements which inherit from a common base Element. | It may be useful to abstract the behavior of your Elements. In this tutorial, we are going to create two Elements which inherit from a common base Element. | ||
| + | For this tutorial, we are going to make three Elements: '' | ||
| + | |||
| + | ===== Create abstract class ===== | ||
| + | |||
| + | First we need to create '' | ||
| + | |||
| + | First, we cannot use ElementParameters in the base class. This creates conflicts when saving **.mfs** files and all children will be loaded with the same parameters. | ||
| + | |||
| + | Second, our constructor must take a '' | ||
| + | |||
| + | < | ||
| + | AbstractElement_Wanderer(UUID u) : Element< | ||
| + | { } | ||
| + | </ | ||
| + | |||
| + | We should also not define '' | ||
| + | |||
| + | We also cannot define **THE_INSTANCE**, | ||
| + | |||
| + | We need a behavior method which wanders the same for all children. This looks like: | ||
| + | |||
| + | < | ||
| + | virtual void Behavior(EventWindow< | ||
| + | { | ||
| + | SPoint wanderPt; | ||
| + | Random& rand = window.GetRandom(); | ||
| + | MDist< | ||
| + | Dir d = (Dir)rand.Create(Dirs:: | ||
| + | |||
| + | Dirs:: | ||
| + | |||
| + | wanderPt *= Dirs:: | ||
| + | |||
| + | if(window.IsLiveSite(wanderPt)) | ||
| + | { | ||
| + | if(window.GetRelativeAtom(wanderPt).GetType() == | ||
| + |  | ||
| + | { | ||
| + |  | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | Finally, we need a new pure abstract method. This will allow a sub-class to tell this base class how far it would like to wander per event. We write it like this, in a '' | ||
| + | |||
| + | < | ||
| + | virtual u32 GetWanderDistance() const = 0; | ||
| + | </ | ||
| + | |||
| + | ===== Create child classes ===== | ||
| + | |||
| + | Next, we need some child classes to inherit from this one. We will create two classes, which are nearly identical, to do this. | ||
| + | |||
| + | First, each of these classes gets an '' | ||
| + | |||
| + | Next, change the class definition and the constructor to use '' | ||
| + | |||
| + | Next, define '' | ||
| + | |||
| + | Finally, define '' | ||
| + | |||
| + | < | ||
| + | protected: | ||
| + |  | ||
| + | { | ||
| + | return (u32)m_wanderDistance.GetValue(); | ||
| + | } | ||
| + | </ | ||
dev/abstract_elements.1411497505.txt.gz · Last modified: 2014/09/23 18:38 by tsmall1
                
                