Python Script Base Class

[ LiB ]

Almost all Python scripts called from BetterMUD (the command and logic scripts) have similar features. They have names , they can be saved to and from files, and they are always initialized with the ID of the entity they are attached to.

Because all scripts share similar features, it makes a lot of sense to make a base class that you can use for all Python scripts.

This class is stored in the /data/bettermudscript.py file:

 class bettermudscript:     # Initialize the script with an ID     def Init( self, id ):         self.me = id         self.mud = BetterMUD.GameWrap()         self.ScriptInit()     def ScriptInit( self ):         pass     def Name( self ):         return self.name     def LoadScript( self, s ):         pass     def SaveScript( self ):         return "" 

The Init function defines two variables : me , the ID of the current entity, and mud , a GameWrap object that allows you to access the Game module of BetterMUD. After that, Init calls ScriptInit .

When making scripts of your own, you should never create your own Init function; instead, you need to do all of your initialization in the ScriptInit function, which is actually empty here (the pass keyword tells Python that you don't want the function to do anything). So when you create your own script class, you just define a ScriptInit function and do your initializing there. The bettermudscript.Init function automatically calls your new init function. This acts like a pure virtual function in C++.

The Name function returns the name variable, which hasn't been defined at all. You'll see how this works when I show you an actual script.

Finally, the LoadScript and SaveScript functions assume that they don't have to load or save any data; they simply ignore what you pass in to them, and return an empty string when you ask it for save-data.

[ LiB ]


MUD Game Programming
MUD Game Programming (Premier Press Game Development)
ISBN: 1592000908
EAN: 2147483647
Year: 2003
Pages: 147
Authors: Ron Penton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net