Node List GameObject Fehler

DuffCola

Mitglied
Warum zum Teufel gibt der in der GameLoop immer nur einmal GameObject Update! und GameObject Render! aus****

Code:
#include <iostream>
using namespace std;

class GameObject
{
public:
	GameObject *nextGameObject;

	GameObject()
	{
		cout<<"GameObject Constructor!\n";
		nextGameObject = nullptr;
	}
	~GameObject()
	{
		cout<<"GameObject Destructor\n";
		if(nextGameObject != nullptr)
		{
			delete nextGameObject;
		}
	}

	virtual void Update()
	{
		cout<<"GameObject Update!\n";
	}
	virtual void Render()
	{
		cout<<"GameObject Render!\n";
	}
};

class GameObjectManager
{
private:
	GameObject *firstGameObject;
public:
	GameObjectManager()
	{
		firstGameObject = nullptr;
	}
	~GameObjectManager()
	{
		if(firstGameObject != nullptr)
		{
			delete firstGameObject;
		}
	}

	void Update()
	{
		if(firstGameObject != nullptr)
		{
			GameObject *helpGameObject = firstGameObject;
			while(helpGameObject != nullptr)
			{
				helpGameObject->Update();
				helpGameObject = helpGameObject->nextGameObject;
			}
		}
	}
	void Render()
	{
		if(firstGameObject != nullptr)
		{
			GameObject *helpGameObject = firstGameObject;
			while(helpGameObject != nullptr)
			{
				helpGameObject->Render();
				helpGameObject = helpGameObject->nextGameObject;
			}
		}
	}

	void Add(GameObject *newGameObject)
	{
		if(firstGameObject == nullptr)
		{
			firstGameObject = newGameObject;
		}
		else
		{
			GameObject *helpGameObject = firstGameObject;
			while(helpGameObject != nullptr)
			{
				helpGameObject = helpGameObject->nextGameObject;
			}
			helpGameObject = newGameObject;
		}
	}
};

int main()
{
	GameObjectManager gom;
	bool run = true;

	gom.Add(new GameObject);
	gom.Add(new GameObject);
	gom.Add(new GameObject);
	gom.Add(new GameObject);

	while(run)
	{
		cout<<"GameLoop Start\n";
		gom.Update();
		gom.Render();
		cout<<"GameLoop End\n";
		cin.get();
	}

	return 0;
}
 
Hallo DuffCola

C++:
        else
        {
            GameObject *helpGameObject = firstGameObject;
            while(helpGameObject != nullptr)
            {
                helpGameObject = helpGameObject->nextGameObject;
            }
            helpGameObject = newGameObject;
        }

Ein Mal mehr, du weist hier der temporären Variable helpGameObject einen Wert zu, diese temporäre Variable hat überhaupt keine Zusammenhang zum vorherigen Objekt.

Viele Grüsse
Cromon
 
Zurück