[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 4.8.3 Attaching User Objects to CS Objects in a map file

In the previous howto (see section Attaching User Objects to CS Objects) you could see how you can attach game data to Crystal Space objects at runtime (i.e. from within code). It is also possible to attach game data to objects through the map file. Here we explain how.

#### Using Key/Value Pairs

Crystal Space doesn't do anything with the keys you specify in a map file. The only thing it does is store them so you can use them later. Here is how you would fetch all keys from a mesh object:

    void MyGame::ProcessKeys (iMeshWrapper* mesh)
    {
      csRef<iObjectIterator> it = mesh->QueryObject ()->GetIterator ();
      while (it->HasNext ())
      {
        iObject* obj = it->Next ();
        csRef<iKeyValuePair> key = scfQueryInterface<iKeyValuePair> (obj);
        if (key)
        {
          if (!strcmp (key->GetKey (), "monsterAI"))
          {
            // Process key->GetValue ()
            ...
          }
          else if (!strcmp (key->GetKey (), "monsterLive"))
          {
            // Process key->GetValue ()
            ...
          }
        }
      }
    }

This is only an example. You can call `QueryObject` on almost every Crystal Space object and loop over all keys (or other `iObject` implementing objects) with the code above.

To actually process the key you could check out the previous tutorial (see section Attaching User Objects to CS Objects) and make your own game data object that contains all the collected values and attach that to the mesh too.

#### Include Files

The include files useful for this section are:

    #include <iutil/object.h>
    #include <csutil/csobject.h>
    #include <cstool/keyval.h>

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ]

This document was generated using texi2html 1.76.