CEGUI In Practice - User Data

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search

Written for CEGUI 0.8


Works with versions 0.8.x (stable)

Works with latest CEGUI stable!

CEGUI In Practice series

This page is part of a series, use links above to easily navigate through all chapters of the series.


CEGUI InPractice 7

This tutorial will be a short tutorial, as i will show how to do a small addon to the previous tutorial CEGUI In Practice - A Game Console showing you how to associate the ConsoleRoot with our GameConsoleWindow class. While not terribly useful in this situation, it can demonstrate a useful feature that you likely will use in your own game.


UserData

User data in CEGUI is basically a void pointer. This allows you to assign a custom structure or variable to each cegui window. This is extremely useful as it allows you to greater incorporate CEGUI into your development. For the previous example we created a Class to encompass our Console window which was great as it allowed us to easily encapsulate all the CEGUI windows and their handlers into a neat and easy to use function. It was pretty easy to go from GameConsoleWindow to each of the classes (by accessing the m_ConsoleWindow in the class and getChild() method). However, its not so easy to from the CEGUI::Window back up to the class level without recompiling CEGUI and creating custom widgets, unless you use userData.

Its almost so easy to use that its a waste of wiki space to show this, but regardless I see alot of posts about it on the IRC channel. Here is a line we would add to our CreateCEGUIWindow(); function.

void GameConsoleWindow::CreateCEGUIWindow()
{
 
...
 
            CEGUI::System::getSingleton().getDefaultGUIContext().getRootWindow()->addChild(m_ConsoleWindow);
            (this)->RegisterHandlers();
            m_ConsoleWindow->setUserData(this);    // <------ Add this line
...
}

That adds a pointer to the class we're currently in, which is an instance of GameConsoleWindow. Note tho, it stores this as a void* so when we go back to this pointer later, it will not know what type of data this is, so you will need to cast it back:

     GameConsoleWindow *ConsoleClass = NULL;
 
     ConsoleClass = static_cast<GameConsoleWindow*>(m_ConsoleWindow->getUserData());

This will convert the void* we stored above, into the ConsoleClass pointer. You could probably use dynamic_cast but then you'd need to turn on RTT, which is not always required for performance reasons. Regardless you can see how easily this is done. We will use this in a later tutorial demonstrating how to implement an inventory system.

Until next time..