Sign in to follow this  

Cannot override virtual methods

This topic is 1443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I have two classes: NXGameObject & NXSceneObject.

class NXGameObject
		{
			public:
				LPCWSTR Name;
				bool RenderToTexture = true;
				bool IsActive = false;
				virtual void NEXENGINE_API Initialize() = 0;
				virtual void NEXENGINE_API Update(NXGameTime*) = 0;
				virtual void NEXENGINE_API Render(NXGameTime*) = 0;
				virtual void NEXENGINE_API RenderDeferred(NXGameTime*){}
				virtual void NEXENGINE_API Dispose() = 0;
		};
	class NXSceneObject : public NXGameObject
		{
			public:
				void Initialize(){}
				void Render(NXGameTime* gameTime){}

				virtual void ReadData(NXBinaryReader* reader){}
				virtual void Update(NXGameTime* gameTime){}
				virtual void Render(NXCamera3D* camera, NXGameTime* gameTime){}
				virtual void Dispose(){}

				NXGameObject* ParentScene;
				map<string, NXSceneObject*> Chlidren;

				bool IsVisible;

				XMMATRIX WorldMatrix() { return XMMatrixScaling(Size.x, Size.y, Size.z) * XMMatrixTranslation(Position.x, Position.y, Position.z) * XMMatrixRotationRollPitchYaw(Rotation.x, Rotation.y, Rotation.z); }
				XMFLOAT3 Position;
			    XMFLOAT3 Rotation;
				XMFLOAT3 Size;
				NXBoundingBox Bounds;

				NXSceneObject(){ }
				NXSceneObject(NXBinaryReader* reader)
				{ //Read Main Data
					IsVisible = reader->ReadBoolean();
					Position = reader->ReadVector3(false);
					Rotation = reader->ReadVector3(false);
					Size = reader->ReadVector3(false);
					Bounds = reader->ReadBoundingBox(false);
//Call the type of scene object being created's ReadData method and Read Custom Object Specific Data
					ReadData(reader);
				}
		};

Now I have a bunch of different classes (player markers, enemy spawn points, etc.) derived from NXSceneObject.  Each class has overrides ReadData(NXBinaryReader*).  Here's an example.

class NXMarker : virtual public NXSceneObject
		{
			public:
				NXMarker(NXBinaryReader* reader) : NXSceneObject(reader)
				{
				}
		};

		class NXPlayerStartMarker : public NXMarker
		{
			public:
				float Health;
				float MoveSpeed;
				bool Realistic;

				NXPlayerStartMarker(NXBinaryReader* reader) : NXMarker(reader)
				{
				}

				void ReadData(NXBinaryReader* reader) override
				{
					Realistic = reader->ReadBoolean();
					Health = reader->ReadFloat();
					MoveSpeed = reader->ReadFloat();
				}
		};

My problem is that, no matter what I try, the classes don't seem to be overriding ReadData and the ReadData method in the created class is not called.  I put breakpoints inside the PlayerStartMarker's ReadData method and it is not called.   I am trying to override the method so that after the main data is read in the NXSceneObject constructed, it'll call whatever type of class is being created's ReadData method.  Sorry if I wasn't clear.  Any help? Thanks! :)

Share this post


Link to post
Share on other sites

Don't ever do complicated work inside a constructor.  The constructor's job is to put the object into a valid state.  That does _not_ mean that the object needs to be ready to put into the game world; just that you can call further methods on it without garbage pointers or other bad data causing crashes or the like.  Methods like ReadData().  The constructor needs to just make the necessary data member non-garbage, nothing else.

 

It's very common for complex objects to have an explicit Initialize() method or the like that is called at some point after construction.  A common component-based initialization method would be:

go = new GameObject()

xml = ReadArchetypeData()
for desc in xml.children['components']:
  // simple, basic, constructor
  comp = ConstructComponentByType(desc.type)
  // read data, but don't do anything with it
  comp.ReadData(desc)

  go.AddComponent(comp)

for comp in go.components:
  // meaty initialization, can grab references to other components
  comp.Initialize()

go.FinalizeInitialization()

Share this post


Link to post
Share on other sites

This topic is 1443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this