I want to create enemies on the fly. Say, every time I press E, an enemy appears in a random place. Here is what I have right now.
class enemy{ public: int health; int attack; int range; int enemyx; int enemyy; };
Right? Fairly simple, I just made a class. Everything is public right now because I'm new to classes and I don't want to have to dick around with the tangled mess that is private/protected elements.
So right now, I could, for example, have a ton of lines that go something like
enemy *enemy1; enemy *enemy2; etc. etc.
and then a whole "if enemy1.health = 0 then enemy1 = new enemy blah blah blah", but either I'd have to declare a LOT of enemies (not fun) or limit myself to how many enemies are on the screen at a time. Limits are good, yes, but I don't like the idea of hitting a cap and having the game crash when it tries to make one more, regardless of how high the cap is.
So how would I go about doing this? Like I said, I'm new to classes so I might not even be doing THIS right, but hey.
Well I won't comment on the class you're using, but I'll give you a hint at how to contain your enemies. You're using C++, so google for std::list or possibly std::vector (though list is probably better for enemies).
There's another thread currently discussing the use of standard library containers. Essentially, there are containers in the standard library of C++ that will resize themselves automatically to fit your needs (examples are those mentioned by Trent Gamblin).
One thing not mentioned in the other thread (that I recall) is object pooling. Memory allocation and deallocation are relatively expensive operations so if your enemies will be spawned and destroyed rapidly then it might be something to consider.
Ah, see, I have no idea what a vector is. I just searched for "class", which obviously had too many results, and then "class enemy" which came up with nothing spectacular.
This does seem like pretty much what I asked though. But what exactly is the difference between a vector and a list? They seem virtually identical.
On the subject on "pooling" the way I like to do it is to create two vectors of pointers to objects, one vectors containing objects in use and in the other objects that aren't in use, when initializing the level or the application or what you choose to do, you can create a number of objects (pooling), and when a new enemy(or whatever) needs to be created you check if there are any objects in the unused objects vector and if there are you simply move the pointer to the used objects vector and edit it's values, if not you simply create a new object.
Some people use one vector with "flags" to check if the object is in use, I think that in general moving some values around once in a while is better than to check a value for each allocated object every time.
Well, right now each enemy is five ints. Even if I have a thousand enemies on screen at once (which I wouldn't), that's only 5,000 integers. Obviously if my enemies were more complex (AI, for example) resources would be an issue, but at the moment I'm trying to keep it nice and simple. I'll keep pooling in mind for when the project is nearing completion, assuming that ever happens, but I'm confident that I have enough memory to test it with no optimization whatsoever with no penalties.
Mandatory flowchart that's been posted on the forums a million times (I wonder if Allegro.cc identifies duplicates of uploads and optimizes them).[1]
{"name":"599657","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/c\/0c0f8b542e5ce053a092a917da542369.png","w":684,"h":707,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/c\/0c0f8b542e5ce053a092a917da542369"}
I'm just going to add a link to this post to my signature so I can easily find and link to it next time.
Wow the Mandatory flowchart is so nice, now I can quit throwing dices every time I have to decide which STL C. to use . Thank you.
Hooray, according to the flowchart I should use a vector. Of course I have no goddamn clue what "need to find element by key" means.
Of course I have no goddamn clue what "need to find element by key" means.
Which is usually a big clue that you don't need to.
Try looking up hash tables.
Of course I have no goddamn clue what "need to find element by key" means.
It means key as in Key/Value. In other words, each entry has a "key" and "value" associated with it.
For example, the key might be a person's name and the value might be their age. So in effect you'd get something like the following:
map<string, int> Ages; Ages['Nick'] = 25; Ages['John'] = 34; Ages['Mike'] = 17; cout << "John is " << Ages['John'] << " years old." << endl;
Ages['Nick'] = 25; Ages['John'] = 34; Ages['Mike'] = 17; cout << "John is " << Ages['John'] << " years old." << endl;
The key needs "double quotes", not 'single quotes'. They're strings.
Wouldn't linked lists to the job as well? I'm just curious, I find linked lists quite usefull in situations like these (I know I started using them for game objects since reading some article on gamedev.net, don't remember which one really)
std::list is a linked list. The whole idea of those C++ containers is that you can forget how it's implemented and just get "an object that holds any number of your variables[1]". They have similar interfaces so you can interchange them.
It is however very educational to make a linked list yourself (or vector??) to lift the voodoo of "infite size containers".
Hm, ok, that clears the fog for me. I'm stuck somewhere between C and C++ actually and don't know much about STL (just some theory). I don't know how bad that is, but for now I can't say I really need something from STL that I can't handle doing myself...
Linked lists are quite easy to implement actually, I suggest trying to build an example just to have an ideea on how they work
My favorite STLs:
std::string: hands down easier to use than memory-managing "char* array" or "char array[]" of characters yourself.
std::vector: While a little tedious to have to first push_back() all of the values/objects that you need, having all of the functions that comes with it as well as the ease of resizing an array pays for itself.
std::list:
Linked lists are quite easy to implement actually
I've implemented this for one of my early CS classes by hand, as they also wanted us to know how they worked under the hood. Never want to do THAT again. Ever.
std::map< key, value >: Because they handle all the magic for me. Very quick lookup times, and it's wonderful to use when using Class Factories. my_map["http://www.allegro.cc/forums/thread/602089"] = STATUS_VISITED;
Implementing linked-listing ability inside a class is basic (And it's educational, often the first teaching tool for pointers. I advise doing it at least once.)
Now implementing a generic container, it requires good grasp of implementing templates, and I guess it can be very obscure.
I wrote a singly-linked list in C, mostly for educational purposes, that I'm pretty proud of[1], but I bet performance-wise it's nowhere near std::list. It's also obviously not as safe to use, but it was written in C so that's to be expected. My attempt to write a circular buffer failed hard though[2] and I haven't done much with the library since[3]. I guess I just don't fully understand circular buffers yet.