[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This section explains how you can assign your own game specific data to Crystal Space objects (i.e. like meshes and sectors).
Almost all objects in the Crystal Space engine implement the
iObject
interface. Because of this you can attach your own
objects to them. This can be very useful to attach game specific
data to Crystal Space objects. For example, you may want to attach
the state of an actor to the mesh object representing that actor (like
current amount of amunition, health, ...).
To define data that you can attach to any other Crystal Space
object (or any object implementing iObject
) you also have to
create an object that implements iObject
. The easiest way to do
that is to inherit from csObject
which implements iObject
for you. Here is an example on how you can do this easily:
SCF_VERSION (MyOwnData, 0, 0, 1); class MyOwnData : public csObject { public: int my_data_int; ... SCF_DECLARE_IBASE_EXT (csObject); }; SCF_IMPLEMENT_IBASE_EXT (MyOwnData) SCF_IMPLEMENTS_INTERFACE (MyOwnData) SCF_IMPLEMENT_IBASE_EXT_END |
Basically you create a class called `MyOwnData' which inherits
from csObject
. In that class you can do whatever you want
with respect to your own game data. In the example above we only
have a variable `my_data_int' but of course you can do whatever
you want. The SCF macros make sure that you can later query for
that object (using SCF_QUERY_INTERFACE
or
SCF_QUERY_INTERFACE_FAST
for example).
In addition you also should put the following in your application header:
SCF_DECLARE_FAST_INTERFACE (MyOwnData) |
This makes sure that you can use SCF_QUERY_INTERFACE_FAST
instead
of SCF_QUERY_INTERFACE
. This is not strictly required but it makes
the query considerably faster.
To attach your own data to some Crystal Space object you can use
iObject::ObjAdd()
. The example below shows how you can attach
an instance of MyOwnData
to a mesh:
MyOwnData* mydata = new MyOwnData (); mydata->my_data_int = ...; iMeshWrapper* mesh = ...; mesh->QueryObject ()->ObjAdd (mydata); mydata->DecRef (); |
mesh->QueryObject()
is interesting. Many Crystal Space objects
that implement iObject
have a conveniance function called
QueryObject()
which will return a reference to the iObject
implementation. This code is equivalent to:
iObject* obj = SCF_QUERY_INTERFACE (mesh, iObject); |
with the important difference that QueryObject()
does not increment
the reference count. If an object doesn't have this conveniance function you
must use SCF_QUERY_INTERFACE
instead.
To find if some object has your data attached to it you can use the following code:
MyOwnData* mydata = CS_GET_CHILD_OBJECT_FAST(mesh->QueryObject(), MyOwnData); if (mydata) { ... (use mydata) mydata->DecRef (); } |
To remove your data you can use:
MyOwnData* mydata = CS_GET_CHILD_OBJECT_FAST(mesh->QueryObject(), MyOwnData); if (mydata) { mesh->QueryObject ()->ObjRemove (mydata); mydata->DecRef (); } |
The include files useful for this section are:
#include "iutil/object.h" #include "csutil/csobject.h" |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |