Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / New & Delete

Author
Message
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 16th Jul 2010 03:31
So I have been using new a lot for my game because I am declaring pointers to some of my game objects and then initializing them later like so:



Now my question is do I call delete after the game is over like lets say the player quits out to the main menu is that when I should call delete?

Also I have a lot of enemies that I do that with should I call delete whenever they die or just when the level is done?

One last thing, if I were to call delete based off if the enemy is alive would I have to make a bool to make sure that it only tries to delete the memory once for example:


If I ran that and then the next time it looped through my game loop it would come to this line and try and delete it again. I would really only use this if you are supposed to call delete right when an enemy dies.

Anyway I really appreciate any help that can be given. =)
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 16th Jul 2010 15:51
You should delete it when you no longer need it - that just depends on the structure of your program.

If you have a lot of enemies, you may want to reuse them. It's safe to just delete them when they die though and spawn new ones separately.

Try this code for not deleting something twice:


"everyone forgets a semi-colon sometimes." - Phaelax
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 16th Jul 2010 19:44
Cool thanks I just wanted to make sure that I understood when to delete things. Seems like I am on the right track from what you said.
Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 16th Jul 2010 22:56
I have a question on header files. I have a header file that includes all of my games header files, looks like this:



Now for lets say my individual classes like Box.h would it be inefficient to just include this header file into that class knowing that I wont be using a good majority of these include files in that class. Or does it really not matter and I can just add any new include files that I have to IncludeAll.h and just use that for all my new files?
Dar13
16
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 18th Jul 2010 05:06
Generally, you only want to include what you will use in that class and forward declare classes as much as possible. Kinda like this:



Greg_C
14
Years of Service
User Offline
Joined: 22nd May 2010
Location:
Posted: 18th Jul 2010 20:33 Edited at: 18th Jul 2010 22:56
Yeah I am going to have to go through all of my files and re work them a bit cause my includes are all over the place. Thanks for the response.

Login to post a reply

Server time is: 2024-11-19 20:26:11
Your offset time is: 2024-11-19 20:26:11