Zippy-Egoboo Home EgoWiki > Documentation > MakingZippyObjects > ZippyScriptingGuide > ZippyInheritanceGuide EgoWiki webs:
Main | TWiki | Know | Sandbox
Documentation . { Changes | Index | Search | Go }

I'm Not a Programmer, I'm an Artist!

Sure you are. Seriously, though, there's no point in learning all the principles of object-orientation just to create a few objects. So this isn't even a whirlwind tour; we're only going to talk about how inheritance can benefit your objects, and how to make it happen.

So what's the deal? The torch in the demo module is flammable. Say you want to make a brazier -- that's flammable too, right? Now, flames are not trivial things, because there are a lot of particles involved, so why should you have to define that flaming behavior all over again? Then again, a brazier is a very different sort of thing than a torch; braziers sit on the ground and can't be picked up, but torches are items and do damage when they hit someone. This is where inheritance becomes important. Torches and braziers are clearly different, but they have some common ground in the fact that they are both flammable. So there is a class called Flammable which describes all things that go up in flames; the Torch class derives from it, and so should the brazier. Let's create a brazier and see how inheritance factors in:

Require(Basic.Flammable)

register_type{Basic.Flammable;
  name = "Brazier",
  namespace = Default,

OK, so right off the bat we can see the difference: there's an extra bit before the name, the name of the Flammable class delimited by a ";". This means that the object we are about to specify inherits from this class. You can specify multiple values here, separated by commas, but we'll talk about that later. Let's continue:

  profile = {
    invincible = 1,
    reflect = 1,
    bump_size = 20,
    bump_dampen = 0.0,
    model = "brazier.md2",
    skins = {
      normal = Skin{
        name = "Just a brazier",
        texture = "brazier.bmp",
        icon = "b_icon.bmp"
      }
    }
  }
}

Pretty simple, right? But wait, where's all the complicated particle stuff we need to set up for flames? It's all in the Flammable class. If you look at the Torch class, you'll see the same thing, just a name and a profile. How does that work?

Occasionally someone will look at a class find out what its values are. For example, the engine often needs to know whether a class has an alert handler or not, and what it is. When this happens to a class that inherits from another, the engine will look in that other class if it doesn't find what it needs in the first one. So for example, the engine might look at the Torch to see if it handles the particle_bumped alert -- the torch doesn't do this itself, but it inherits from Flammable, which does. This also applies to methods; the following will work, even if the Torch type doesn't define an "ignite" method itself:

foo = Torch(...)
foo:ignite()

It will work for a Brazier as well, the way we've defined it above. Which brings us to the point: the Flammable class defines a "new" method that starts the burning process, and since our Brazier doesn't define that method, the engine reverts to the one that Flammable provides.

-- ElminI - 13 Apr 2004

Topic ZippyInheritanceGuide . { Edit | Attach | Ref-By | Printable | Diffs | r1.1 | More }
Revision r1.1 - 13 Apr 2004 - 22:18 GMT - ElminI
Parents: WebHome > MakingZippyObjects > ZippyScriptingGuide
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding EgoWiki? Send feedback.