Jump to content
  • Advertisement
Sign in to follow this  
darcmagik

Getting Objects To Not Lose Scope?

This topic is 876 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 again everybody on GameDev.net forums you guys are always so helpful in my quest to learn and complete the game that I'm working on.  So here is another problem I'm having that I'm hoping you guys can assist me with.

 

So let me give you a little run down on how my engine works, I have a class that builds the window and once its done doing that it passes control basically over to a game class that does most of the work or delegates the work to other systems.  This game class instantiates the Render Manager class object, and an input manager object and a game state manager object as well.  The render manager, and input manager objects are passed into the game state manager class so that the various states of the game can render and access input.

 

Now this system has worked just great since I started using it the first couple of states in the system work great I have the company logo's being displayed on a timed setting and they transition between each one easily and with no problems.  Then the system changes the state over to the Main Menu and that state works great it changes the input scope over to that system and accepts input specific for that state and everything is happy.  When that state accepts input to move on to the New Game it changes the current state over to the New Game and things seem to be fine.  Now the New Game state is not complete I'm using it to test out various other systems like dialog boxes as well as my map system for displaying the main game play of the game on screen.

 

Hope your still with me on this so far... Now I should point out that before I started using the game state system I had created a temporary variable inside of the main Game class for the Map system and had tested out how it works made sure that everything gets loaded properly and that it accepted input and was drawing things to the screen just fine.  So when I created the New Game State I simply copied over how I was creating the map to the New Game State I used the same variable, copied the lines that I used for setting it up and everything.

 

The Update call is called inside of the states update method which is called by the State Manager which is called by the game class, the draw call is setup the same way as well.  Now when I build everything and start running the game the engine switches between the states just fine and when it gets to the switch over to the New Game state it does so just fine but by the time it reaches the point of doing an update and doing a draw call the map class's rendering object has become NULL again as if it was never received and before I'm asked I have verified that it is getting passed in and verified that it was received correctly in fact I stepped through every line of the Initialize process of the map system and up until the point where the initialize map method returns back to the state all objects that the map contains are in great shape and everything is happy.

 

But by the time it reaches the first update and the first draw call it loses the graphics object as well as a few other object variables now I realize this has something to do with how I'm holding onto the pointers for these variables what I'm trying to figure out is what would be the best course of action for this system should I be holding onto a smart_pointer or shared_pointer or what is your suggestion.  I've been looking at this code now for a couple of days and honestly I'm just not seeing the answer right now.  I know its going to be something simple that I'm missing here but that is why I'm here hoping that a fresh brain can point out what I'm missing.

 

So as always thank you in advance for any and all assistance you guys can provide.

Share this post


Link to post
Share on other sites
Advertisement

Huh just make sure that the objects are not destroyed. In my case, there is Framework class with many components (input, rendering, loading etc), and Game interface. The framework calls UpdateGame() and RenderGame() on the object and thats it. Everything lives inside Framework and concrete Game, and so nothing is lost. The main function looks like that:

int WINAPI WinMain(HINSTANCE hInstance,	HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	try
	{
		return Framework(hInstance, L"My Window", 1920, 1080).Run();
	}
	catch (std::exception& e)
	{
		MessageBoxA(0, e.what(), "Exception", 0);
	}
        return 0;
}

Did you create the objects on stack locally, or on the heap?

Edited by QQemka

Share this post


Link to post
Share on other sites

Ok So after I posted this I started following my code a little more and found where I screwed up.  This really was a simple mistake I forgot to move the stuff over to the state where the actual graphics are loaded into memory.  The Graphics Object is there the Input Object is there hell every bit of data is there accept I forgot to load the textures into memory for the map which I was doing originally in the Game class temporarily while I was working on the state system.

 

Ignore this post sorry about that.

Share this post


Link to post
Share on other sites

I just want to point out, if you are debugging a problem and a raw pointer is NULL, that doesn't mean the object has been destroyed or has gone out of scope. A pointer to something keeps its value until you overwrite it. It isn't going to change just because the object it's pointing to isn't valid any more. It'll keep pointing to the same memory address.

So, if you're working on a problem and you see a NULL pointer, look for places in your code that modify the pointer itself, not the value it references.

Share this post


Link to post
Share on other sites

The funny thing is that today when I tried to step through it again I couldn't get the null value to re appear.  I think that something that I did last night in attempting to fix the problem fixed that part and as I posted earlier I forgot to load the textures into the engine when I moved code around.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!