[SOLVED] Trouble managing resources

Problems building or running the engine, queries about how to use features etc.
Post Reply
IFMaster_2005
Halfling
Posts: 40
Joined: Thu Aug 09, 2007 10:45 am

[SOLVED] Trouble managing resources

Post by IFMaster_2005 » Sat Dec 26, 2009 7:39 pm

Hello everyone,

I have some trouble trying to manage the load and unload of resources in Ogre, i'll explain what i'm trying to make.
I'm developing an application that must have several logic states, as an example, the main menu, level 1, level 2, ...
In the initialization of the "main menu state", i want to load all the resources that the main menu needs, and when the application goes to "level 1 state", all the resources (meshes, textures, ...) the main menu uses must be unload, in order to begin the loading of the resources of the level 1 state.
I see the ResourceGroupManager make groups of resources, and its very interesting for me, cause i can link all the resources of the main menu to a resource group called "MainMenu" and load or unload only this one when i will needed.
But what i want is, when the main menu state must be unload, ogre unloads all the resources and its definitions, and its own resourceGroup, and with this if in the future the application must return to the main menu state, can create again the "MainMenu" resource group manager (without using the reload method), add the resource file directories and tell ogre to load all the resource to be used for the application (meshes, textures, ...).

I try to use the functions Ogre::ResourceGroupManager::getSingleton().unloadResourceGroup("General") and Ogre::ResourceGroupManager::getSingleton().loadResourceGroup("General"), but after use the unload function, the application throws an error when i try to use the load function.

Someone can help me with this?

And i want to know how to get a resourceGroup and see a list of all the resources of that group, cause i want to make something like a profiler in order to see in real time all loaded resources.

Thank you very much in advance and sorry very much about my poor english.
Last edited by IFMaster_2005 on Tue Jan 05, 2010 5:05 pm, edited 1 time in total.
0 x

User avatar
DanielSefton
Ogre Magi
Posts: 1235
Joined: Fri Oct 26, 2007 12:36 am
Location: Mountain View, CA
Contact:

Re: Trouble managing resources

Post by DanielSefton » Sat Dec 26, 2009 7:45 pm

Mmm, you don't do it like that. :wink:

Do this:

To unload:

Code: Select all

Ogre::ResourceGroupManager& resource = Ogre::ResourceGroupManager::getSingleton();
resource.unloadResourceGroup("General");
resource.destroyResourceGroup("General");
To create/recreate:

Code: Select all

Ogre::ResourceGroupManager& resource = Ogre::ResourceGroupManager::getSingleton();
resource.createResourceGroup("General");
resource.addResourceLocation(...
resource.initialiseResourceGroup("General");
Last edited by DanielSefton on Sat Dec 26, 2009 9:42 pm, edited 1 time in total.
0 x

IFMaster_2005
Halfling
Posts: 40
Joined: Thu Aug 09, 2007 10:45 am

Re: Trouble managing resources

Post by IFMaster_2005 » Sat Dec 26, 2009 9:16 pm

Thank you for your fast reply, i try what you say but it doesn't works:

In the function where I create the "level 1 state":

Code: Select all

Ogre::ResourceGroupManager &resourceGroup = Ogre::ResourceGroupManager::getSingleton();
resourceGroup.createResourceGroup("Level1");
resourceGroup.addResourceLocation("../Master/Data/Temp/Textures", "FileSystem", "Level1");
resourceGroup.initialiseResourceGroup("Level1");
//resourceGroup.loadResourceGroup("Level1");

Ogre::ResourceGroupManager::ResourceDeclarationList list = resourceGroup.getResourceDeclarationList("Level1");

bool init = resourceGroup.isResourceGroupInitialised("Level1");
bool load = resourceGroup.isResourceGroupLoaded("Level1");

Ogre::StringVector vec = resourceGroup.getResourceGroups();
	
entity = C_OgreResources::getMe()->getSceneManager()->createEntity("test", "ninja.mesh");
entityNode = C_OgreResources::getMe()->getSceneManager()->getRootSceneNode()->createChildSceneNode("testNode");
entityNode->attachObject(entity);
entity->setVisible(true);
In the function where I destroy the "level 1 state":

Code: Select all

Ogre::ResourceGroupManager &resourceGroup = Ogre::ResourceGroupManager::getSingleton();
resourceGroup.unloadResourceGroup("Level1");
resourceGroup.destroyResourceGroup("Level1");
The resource declaration list is empty, the string vector vec, return 5 strings (including Level1), init is true and load is false (if I uncomment resourceGroup.loadResourceGroup("Level1"), load is true), but when i call to create the entity test with the ninja.mesh the applications thorw an error: OGRE EXCEPTION(6:FileNotFoundException): Cannot locate resource ninja.mesh in resource group Autodetect or any other group. in ResourceGroupManager::openResource (line 763).
Note the line resourceGroup.initialiseResourceGroup("Level1");, you say to use initialiseResources, but in the last version of Ogre this method doesn't exists.

I dont know if i'm doing something wrong, or maybe i need more calls to make it work :P

Thank you again.
0 x

IFMaster_2005
Halfling
Posts: 40
Joined: Thu Aug 09, 2007 10:45 am

Re: Trouble managing resources

Post by IFMaster_2005 » Sat Dec 26, 2009 9:31 pm

I'm really sorry the error was mine, i add the wrong resource path.
Now it works!, but the second time i create level 1, Ogre tells me the entity (ninja.mesh) already exists, so the question is, when i destroy all the resources, i must destroy too all created entites (sceneNodes, ...) that uses those resources?

And, i expect the line Ogre::ResourceGroupManager::ResourceDeclarationList list = resourceGroup.getResourceDeclarationList("Level1"), will return a list with all the resources in the group level1, but this list is empty and the method gerResourceGroup("Level1") is protected. There are any way to get the information of a ResourceGroup?

Thank you very much again!
0 x

User avatar
DanielSefton
Ogre Magi
Posts: 1235
Joined: Fri Oct 26, 2007 12:36 am
Location: Mountain View, CA
Contact:

Re: Trouble managing resources

Post by DanielSefton » Sat Dec 26, 2009 9:35 pm

Bleh, sorry about that, it's initialiseResources in my engine :oops:

Change that to initialiseResourceGroup() :wink:

And yes, entities and scene nodes should be destroyed before you recreate them, otherwise you'll get exceptions. This is how:

Code: Select all

mNode->detachObject(mEntity);
mSceneMgr->destroySceneNode(mNode);
mSceneMgr->destroyEntity(mEntity);
0 x

IFMaster_2005
Halfling
Posts: 40
Joined: Thu Aug 09, 2007 10:45 am

Re: Trouble managing resources

Post by IFMaster_2005 » Sat Dec 26, 2009 10:04 pm

Thanks DanielSefton, you help me a lot!

The last what i need is a way to get a ResoruceGroup in order the see what are the resource that it contains.

Again, thank you very much!
0 x

IFMaster_2005
Halfling
Posts: 40
Joined: Thu Aug 09, 2007 10:45 am

Re: Trouble managing resources

Post by IFMaster_2005 » Sun Dec 27, 2009 6:40 pm

I didn't put this post as resolved bacause now i have the same problem with Overlays, i'll explain:

In the MainMenu state class, i have an overlay, i want to load it in the constructor, and unload in the destructor (when the application leaves the mainmenu state to go other state), this is the code of the constructor:

Code: Select all

Ogre::ResourceGroupManager &resourceGroup = Ogre::ResourceGroupManager::getSingleton();
resourceGroup.createResourceGroup("MainMenu");

resourceGroup.addResourceLocation("../Master/Data/Temp/textures", "FileSystem", "MainMenu"); //In textures i have, .overlay, .material and the .tga texture

resourceGroup.initialiseResourceGroup("MainMenu");
resourceGroup.loadResourceGroup("MainMenu");

bool init = resourceGroup.isResourceGroupInitialised("MainMenu");
bool load = resourceGroup.isResourceGroupLoaded("MainMenu");
	
m_BackGround = Ogre::OverlayManager::getSingleton().getByName("Engine/MM_Background");
m_BackGround->show();
This is the code of the destructor:

Code: Select all

m_BackGround->hide();
m_BackGround = NULL;
			
Ogre::ResourceGroupManager &resourceGroup = Ogre::ResourceGroupManager::getSingleton();
resourceGroup.unloadResourceGroup("MainMenu");
resourceGroup.destroyResourceGroup("MainMenu");
This is the code of the other state, level1:

Code: Select all

Ogre::ResourceGroupManager &resourceGroup = Ogre::ResourceGroupManager::getSingleton();
resourceGroup.createResourceGroup("Level1");
resourceGroup.addResourceLocation("../Master/Data/Temp/Models", "FileSystem", "Level1");
resourceGroup.initialiseResourceGroup("Level1");
resourceGroup.loadResourceGroup("Level1");

bool init = resourceGroup.isResourceGroupInitialised("Level1");
bool load = resourceGroup.isResourceGroupLoaded("Level1");

entity = C_OgreResources::getMe()->getSceneManager()->createEntity("test", "ninja.mesh");
entityNode = C_OgreResources::getMe()->getSceneManager()->getRootSceneNode()->createChildSceneNode("testNode");
entityNode->attachObject(entity);
entity->setVisible(true);
And the destructor code of level1:

Code: Select all

entityNode->detachAllObjects();
C_OgreResources::getMe()->getSceneManager()->destroySceneNode(entityNode);
C_OgreResources::getMe()->getSceneManager()->destroyEntity(entity);

entity = NULL;
entityNode = NULL;
	
Ogre::ResourceGroupManager &resourceGroup = Ogre::ResourceGroupManager::getSingleton();
resourceGroup.unloadResourceGroup("Level1");
resourceGroup.destroyResourceGroup("Level1");
With this code, in the MainMenu state i can see the overlay correctly with its texture, and when i go to level1, apears the ninja correctly too, and if i return to mainmenu, all works well.
But the problem is, the definition of the overlay remains in memory, cause if i add this code to the constructor of level1:

Code: Select all

Ogre::OverlayManager::getSingleton().getByName("Engine/MM_Background")->show();
When I go from MainMenu to Level1, the overlay without texture is displayed.

If in the destructor of the MainMenu i add:

Code: Select all

Ogre::OverlayManager::getSingleton().destroy("Engine/MM_Background");
When i return to MainMenu from Level1, the object m_BackGround (the overlay) is NULL, if i add this code to the constructor of MainMenu:

Code: Select all

Ogre::OverlayManager::getSingleton().create("Engine/MM_Background");
Ogre throws the following exception: OGRE EXCEPTION(4:ItemIdentifyException): Overlay with name 'Engine/MM_Background' already exists!

Iven i'm thinking in create all overlays of the game in a default ResourceGroup of the engine, in the initialization of Ogre (in the same place where Ogre loads the deubug overlays), leaving the texture of the overlay in the resourceGroup MainMenu (its loaded in the constructor of MainMenu), but when i run the application, the overlay is seen without the texture.

I'm a bit frustrated with this, and i really don't know how to load and unload data from scripts like the overlay (i think i will have the same problem with other script resources like .material in the future), and i don't want to continue codeing the engine if this doesn't work properly at this moment.

Someone can help me with this, please?
Thank you very much!!
0 x

IFMaster_2005
Halfling
Posts: 40
Joined: Thu Aug 09, 2007 10:45 am

Re: Trouble managing resources

Post by IFMaster_2005 » Tue Jan 05, 2010 5:04 pm

I finally discovered how OverlayManager works thanks to this post http://www.ogre3d.org/forums/viewtopic.php?p=278368
So as far as i undestand what i trying to do is imposible, because the second time of the initalization of the ResourceGroup, the overlay script is already loaded, so Ogre exclude it.
The only way is destroy all overlays (OverlayManager::getSingleton().destroyAll(), clears the loadedScripts vector) or, don't use scripts and define the overlays using code.
I put my conclusions here hoping help someone. Thanks again and sorry about my poor english.
0 x

Post Reply