Zippy-Egoboo Home EgoWiki > Documentation > MakingZippyObjects > ZippyScriptingGuide > ZippyNamespaceGuide EgoWiki webs:
Main | TWiki | Know | Sandbox
Documentation . { Changes | Index | Search | Go }
This topic is all about naming your character types. Of course it's important to give them names that aren't taken yet, but it's also important to think about how future objects will be named. Just think, if you name an object "MyWand", isn't it likely that someone else will want to make their own wand too? So instead, we could put a prefix in front of every type we name; so I could name all my types "ElminSomething". But this is a little messy-looking.

Another thing you might be concerned about is how your script files get loaded. For very simple scenarios it's OK to put do_file() in the module's loadai.lua file, but there are more complicated things that could happen. What about objects that never get spawned initially, but are spawned by another object type sometime in the middle of play? What if the objects that spawn other types of object are used really often? Do you have to put several do_file() statements in the loadai.lua file for each module? Why not just put them in the spawning object's script and be done with it? But what if some other type of object wants to spawn these annoying little critters? How do we gaurantee that the script doesn't get loaded twice?

These two questions have a single answer: Namespaces. And if you don't understand one or both of them, that's OK -- just use Namespaces anyway. Because Namespaces are Good.

Namespaces in Zippy

The bottom line is, now instead of giving your character types and ActionClasses and so forth one name, we want you to give them two -- sort of like your parents gave you two names. There may be many Johns, but John Smith is (or should be) the only John in the Smith family. Just so, there may be many types called RangedWeapon, but there should be only one in the Basic namespace, and it's full name is Basic.RangedWeapon.

So what does this mean for your scripts? First of all, when you define character types, do it like this:

register_type{
  name = "RangedWeapon",
  namespace = Basic,
  ...

In other words, specify a namespace. Second, you need to make sure you choose a namespace defined in the file objects/make_namespaces.lua. Third, you need to add a line to that file each time you add some kind of name to a namespace. So if you're adding to the Basic namespace, you'll see part of the file that looks like this:

Namespace{"Basic";
  ...
}

Inside that part, add a line that looks like this:

  RangedWeapon = in_file("objects/ranged_weapon.lua"),

OK, this is really easy. "RangedWeapon" is the name of the thing we're defining, and we'd like to let everyone know it'll be defined in "objects/ranged_weapon.lua". For our object, we're done with namespaces here.

Requirements

Now, to answer the second question: how do you load other scripts safely and in a way that's really Good? The answer is, you Require() them. If you've programmed in almost any other high-level language, you know about this. The way to require something is this:

Require(Basic.RangedWeapon)

Note that we're passing the actual name we want to access. The Require() function is smart, and it reads in a roundabout way the information we gave it in make_namespaces.lua, so it knows that RangedWeapon is in the Basic namespace, and that to load it we have to run "objects/ranged_weapon.lua". What's more, it keeps track of the files it has already loaded, so we don't have to worry about loading a file twice. After you call the Require() function, you can refer to RangedWeapon as Basic.RangedWeapon.

So, in summary, when writing a new script you need to do this:

  1. Specify a namespace for your object.
  2. Add your object's name to make_namespaces.lua.
  3. Require() every object you need to refer to in your script, always.

-- ElminI - 05 Oct 2004

Topic ZippyNamespaceGuide . { Edit | Attach | Ref-By | Printable | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.3 - 06 Nov 2004 - 23:24 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.