Logic

[ LiB ]

Logic modules are modules that react to actions. When I showed you the entity classes in Chapter 12, I told you about the Logic and the LogicCollection classes, which are simple classes that wrap around scripts (or C++ implemented functions too, if you want).

The Logic class is basically just an interface that wraps around a script. The class skeleton looks like this:

 class Logic : public Script { public:     virtual bool CanSave();     virtual int Attribute( const std::string& p_attr );     virtual int DoAction( const Action& p_action ); };  // end class Logic 

NOTE

The Logic class is abstract and doesn't define an implementation. Originally, I had planned on making both C++ and Python implementa tions of logic modules, but after playing around with it, I realized that C++ modules aren't even needed; you can do anything you want from within Python. The design is still there, however; so if you ever feel like implementing hard-coded logic modules in C++, feel free. Just beware that you'll have to stop and restart the MUD to make any changes, and that's not good.

The first thing of interest in the listing is the CanSave function. The reason for this function is that some logic modules shouldn't be saved to disk when an entity is saved to disk. For example, you'll see in Chapter 16 that I implement player connection interaction classes as Logic modules (logic modules that dictate how to send data to a connection), but obviously these modules aren't important when a character exits the game (because he is no longer connected), so he shouldn't be saved to disk.

Logic modules can have attributes that are accessible from outside the module, and this accessibility is implemented by using the Attribute function. This is especially useful for quests, since other logic modules in the game need to check the state of a quest in order to react to it.

NOTE

Even though this code doesn't show it (since it is an abstract class), all logic modules need to be initialized with the ID of the object that it is attached to. You'll see this when I show you the PythonLogic class in Chapter 17 .

The final function is the DoAction function, which performs an action on the particular logic module.

The Python-implemented version of this class can be found in the directory /BetterMUD/ scripts/python. I go over the code in Chapter 17.

[ 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