Using and ObjectManager to render and update subclasses in a list

Hello, I'm completely new to this so if my code is completely wrong then I apologise.

I am creating a simple Asteroids game engine with an ObjectManager class that can add new objects via a list which then tells the list to render and update them (Each object has its own update and render function in its class and I need to add them to a list which the object manager then tells the list to update and render if that makes sense) I have a GameObject Superclass that points to a subclass Spaceship. I also have a main file that handles everything such as adding my spaceship to the game. All my code compiles and doesn't come up with any errors. However, I'm not sure it's working as nothing is appearing on my screen when in the game. I think I may be completely wrong in what I'm doing but this is the first time doing it and I've has little guidance.

ObjectManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
ObjectManager::ObjectManager() 
{
}
std::list < GameObject * > pObjectList;

void ObjectManager::AddObject(GameObject* pNewObject) {
	
	pObjectList.push_back(pS); //add spaceship to list 
	 
	
}

ObjectManager::~ObjectManager()
{
}

void ObjectManager::updateAll(float mdFrametime) //update all the game objects
{

	theTimer.mark();

	{
		std::list < GameObject* > pObjectList;
		for (GameObject * pNextObject : pObjectList) //run through all the objects in the object list and then update them 
		{
			pNextObject-> update(theTimer.mdFrameTime);
		}

	}

}

void ObjectManager::Initialise()
{

}

void ObjectManager::RenderAll()
{
	for (GameObject* pNextObject : pObjectList) //run through all the objects in the object list and then render them  
	{
		pNextObject-> Render();
	}

}


gamecode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
ErrorType Game::StartOfGame()
{
	// Code to set up your game *********************************************
	// **********************************************************************
	theTimer.mark();
	{
		ObjectManager pNewObject;

		ObjectManager* pObjectManager = new ObjectManager();
		pObjectManager-> Initialise();

		Spaceship* pSpaceship = new Spaceship(); //create spaceship
		pSpaceship-> Initialise(Vector2D(300.0f, 300.0f)); //initalise spaceship 

		Spaceship pS; 
		pS.Initialise(Vector2D(0.0f,0.0f)); 
		pNewObject.AddObject(&pS); // Add Object to the Object Manager 'ObjectList' 

		
	}

	return ErrorType();
}


ErrorType Game::Update()

{


	// Check for entry to pause menu
	static bool escapepressed = true;
	if (KEYPRESSED(VK_ESCAPE))
	{
		if (!escapepressed)
			ChangeState(PAUSED);
		escapepressed = true;
	}
	else
		escapepressed = false;


	// Your code goes here *************************************************

	theTimer.mark(); //call the timer mark 

	ObjectManager pObjectManager; 

	pObjectManager.updateAll(theTimer.mdFrameTime); //ObjectManager run Update all function 
	pObjectManager.RenderAll(); //ObjectManager run render all function 		
	
				


	return SUCCESS;
}


If you need more code please let me know and if you need more to understand what I'm doing please just ask!

thank you
There's nothing connecting the ObjectManager created by Game::StartOfGame() on line 7 (which is destructed before the function even returns) and the one created by Game::Update() on line 47.

You need an ObjectManager that exists persistently across cycles of the game loop. Where do you think you should put this instance?
Do you know what the differences are between local, global and member variables?

In StartOfGame() you create two instances of ObjectManager and Spaceship. All of them are local variables and abandoned when leaving the scope {}. Those instances created with new will leave a memory leak. Nothing more happens.
Topic archived. No new replies allowed.