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.
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.
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:
make_namespaces.lua
.
Require()
every object you need to refer to in your script, always.
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. |