Amiga.org

Coffee House => Coffee House Boards => CH / Science and Technology => Topic started by: motorollin on August 27, 2007, 04:19:38 PM

Title: A C++ Vectors question
Post by: motorollin on August 27, 2007, 04:19:38 PM
I'm writing a GP2X game in C++. I have created a class called "Enemy", and then created a vector of Enemy called "enemies". When an enemy is killed, the enemy is removed from the vector using "enemies.erase(enemies.begin()+i)". This works, but when the last enemy is killed (enemies becomes empty?) the game quits. Any ideas why this would happen?

--
moto
Title: Re: A C++ Vectors question
Post by: motorollin on August 27, 2007, 04:53:04 PM
Ok I coded around my problem with the game quitting. I just changed my for loops from 0 -> enemies.size()-1 to 1 -> enemies.size()-1. So enemies[0] never gets moved, shown or checked for collision. Because it can't be seen or destroyed, it remains in the vector, so all of the (visible) enemies can be destroyed and the game doesn't quit. I still want to understand this problem, but hey, it works now  :)  I will look in to lists when the time comes to optimize my game, but for now I want to get back to writing my game :D

--
moto
Title: Re: A C++ Vectors question
Post by: Karlos on August 27, 2007, 07:14:16 PM
Out of curiosity, are you sure a vector is the right container for this application?

If you can kill off any enemy at any time, this implies that you will be randomly removing entries. A vector is not the best candidate for this type of use. A list based container like a queue or deque might be better.

-edit-

:lol: just read your second post...

With regard to the bug, have you tried creating a test case where you simply create a vector, remove all the elements then destroy the vector? Perhaps there's a bug in the implementation leading to a double-free or some other such oddity. This is for the GP2x, right?

At the risk of sounding heretical, I'd not use the STL for game type applications unless I knew the system was a beast and the compiler optimal...
Title: Re: A C++ Vectors question
Post by: mdwh2 on September 02, 2007, 01:02:59 AM
Quote
At the risk of sounding heretical, I'd not use the STL for game type applications unless I knew the system was a beast and the compiler optimal...
The STL seems to be controversial among some game developers, but it's not really clear why. Why should code which has been developed by many programmers, and used and tested by thousands of developers, be slower than something you quickly code up yourself?
Title: Re: A C++ Vectors question
Post by: Karlos on September 02, 2007, 11:55:49 AM
@mdwh2

I'm not knocking the STL, it's very powerful and the containers and algorithms are usually highly optimised and I do use it.

However, I'm also realistic about things. The STL is a genereral purpose, template based library and is not the most efficient solution for everything. As with all things, it comes down to common sense. I'd never use a std::vector for a bitmask, I'd never use a std::string to hold an invarying string where a C style const char* is just as useful. I don't use C++ streams for basic IO (although if I need some complex tokenisation etc, then of course I would).

The reasons to use or not to use STL amongst game devlopers varies. Although I'm not a game developer, in my case, it comes down to many hours of examining the compiler output and profiling code. Wherever cycle-efficiency has been important I've managed to find faster solutions to just about everything it provides that I would otherwise use, but of course that is at the expense of generality.