Problem understanding Sample_ImportAnimationsShareSkeletonInstance. Topic is solved

Problems building or running the engine, queries about how to use features etc.
Post Reply
Cerdus
Gnoblar
Posts: 2
Joined: Thu Jul 19, 2018 9:50 pm

Problem understanding Sample_ImportAnimationsShareSkeletonInstance.

Post by Cerdus »

Ogre Version: :2.1:
Operating System: :Windows 10:
Render System: :Any:

I'm assessing the ogre engine to see if suits my needs, by I'm having problems with some samples.

In the "Sample_ImportAnimationsShareSkeletonInstance" sample I'm trying to spawn multiple animated characters. It works fine for the most part, but when I add the following lines:

Code: Select all

mAnyAnimation[t] = skeletonInstance->getAnimation("char_mining");//This one specifically, the other one does not matter.
mAnyAnimation[t]->setEnabled(true);
The game crashes/closes. My C++ is really rusty, so could someone point my mistake or is this a problem on the engine side?

The code.

Sample_ImportAnimationsShareSkeletonInstance.h

Code: Select all

    class ImportAnimationsShareSkeletonInstanceGameState : public TutorialGameState
    {
	int size = 3000;
        Ogre::SkeletonAnimation* mAnyAnimation[];

    public:
        ImportAnimationsShareSkeletonInstanceGameState(const Ogre::String &helpDescription);

        virtual void createScene01(void);
        virtual void update(float timeSinceLast);
    };
Sample_ImportAnimationsShareSkeletonInstance.cpp

Code: Select all

    void ImportAnimationsShareSkeletonInstanceGameState::createScene01(void)
    {
		...
		
		// Prepare parts
		for (size_t i = 0; i < numParts; ++i)
		{
			v1Mesh = Ogre::v1::MeshManager::getSingleton().load(
				parts[i], Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME,
				Ogre::v1::HardwareBuffer::HBU_STATIC, Ogre::v1::HardwareBuffer::HBU_STATIC);
			v2Mesh = Ogre::MeshManager::getSingleton().createManual(
				parts[i], Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
			v2Mesh->importV1(v1Mesh.get(), halfPosition, halfUVs, useQtangents);
			v1Mesh->unload();
		}

		for (int t = 0; t < size; t++)
		{
			Ogre::SceneNode *sceneNode = sceneManager->getRootSceneNode(Ogre::SCENE_DYNAMIC)->
				createChildSceneNode(Ogre::SCENE_DYNAMIC);

			//Create the master.
			Ogre::Item *charItem = sceneManager->createItem("char_reference.mesh",
				Ogre::ResourceGroupManager::
				AUTODETECT_RESOURCE_GROUP_NAME,
				Ogre::SCENE_DYNAMIC);
			sceneNode->attachObject(charItem);
			sceneNode->setPosition(Ogre::Vector3((t / 100)*0.25, 0, -(t % 100) * 0.25f));

			for (size_t i = 0; i < numParts; ++i)
			{
				//Create the slaves, and make them use the skeleton from the master.
				Ogre::Item* charPart = sceneManager->createItem(parts[i],
					Ogre::ResourceGroupManager::
					AUTODETECT_RESOURCE_GROUP_NAME,
					Ogre::SCENE_DYNAMIC);
				sceneNode->attachObject(charPart);
				charPart->useSkeletonInstanceFrom(charItem);
			}

			{
				//Import animation from char_mining.skeleton
				Ogre::SkeletonInstance *skeletonInstance = charItem->getSkeletonInstance();
				skeletonInstance->addAnimationsFromSkeleton("char_mining.skeleton",
					Ogre::ResourceGroupManager::
					AUTODETECT_RESOURCE_GROUP_NAME);
				mAnyAnimation[t] = skeletonInstance->getAnimation("char_mining");
				mAnyAnimation[t]->setEnabled(true);
			}
		}
		
		...
    }
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5296
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: Problem understanding Sample_ImportAnimationsShareSkeletonInstance.

Post by dark_sylinc »

In the code you posted, at no point you allocate mAnyAnimation.

I think that instead of:

Code: Select all

int size = 3000;
Ogre::SkeletonAnimation* mAnyAnimation[];
you meant:

Code: Select all

const int size = 3000;
Ogre::SkeletonAnimation* mAnyAnimation[size];
Or alternatively in the header file:

Code: Select all

Ogre::SkeletonAnimation **mAnyAnimation;
and in the cpp file:

Code: Select all

mAnyAnimation = new Ogre::SkeletonAnimation*[size];

//Then at shutdown:
delete [] mAnyAnimation;
Otherwise when you try to store to mAnyAnimation[t] you're storing the pointers into invalid memory regions, hence the crash.
Cheers
Cerdus
Gnoblar
Posts: 2
Joined: Thu Jul 19, 2018 9:50 pm

Re: Problem understanding Sample_ImportAnimationsShareSkeletonInstance.

Post by Cerdus »

This is correct. I was "initializing" in the constructor, but it was obviously wrong. Thanks for the answer.
Post Reply