Difference between revisions of "CEGUI In Practice - User Data"
m (moved CEGUI In Practice7 to CEGUI In Practice - User Data) |
|||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
− | {{VersionBadge|0. | + | {{VersionBadge|0.8}} |
{{Series|CEGUI In Practice|7}} | {{Series|CEGUI In Practice|7}} | ||
Line 10: | Line 10: | ||
==== UserData ==== | ==== UserData ==== | ||
− | User data in CEGUI is basically a void pointer. | + | 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. | + | 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. |
<source lang="cpp"> | <source lang="cpp"> | ||
Line 20: | Line 20: | ||
... | ... | ||
− | CEGUI::System::getSingleton(). | + | CEGUI::System::getSingleton().getDefaultGUIContext().getRootWindow()->addChild(m_ConsoleWindow); |
(this)->RegisterHandlers(); | (this)->RegisterHandlers(); | ||
m_ConsoleWindow->setUserData(this); // <------ Add this line | m_ConsoleWindow->setUserData(this); // <------ Add this line | ||
Line 27: | Line 27: | ||
</source> | </source> | ||
− | That adds a pointer to the class we're currently in, which is an instance of GameConsoleWindow. | + | 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: |
<source lang="cpp"> | <source lang="cpp"> | ||
Line 35: | Line 35: | ||
</source> | </source> | ||
− | This will convert the void* we stored above, into the ConsoleClass pointer. | + | 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.. | Until next time.. | ||
− | |||
[[Category:Tutorials]] | [[Category:Tutorials]] |
Latest revision as of 08:25, 30 March 2014
Written for CEGUI 0.8
Works with versions 0.8.x (stable)
Works with latest CEGUI stable!
CEGUI In Practice 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..