|
Enemies as classes |
Bainemo
Member #11,031
June 2009
|
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. |
Trent Gamblin
Member #261
April 2000
|
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).
|
bamccaig
Member #7,536
July 2006
|
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. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Bainemo
Member #11,031
June 2009
|
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. |
Albin Engström
Member #8,110
December 2006
|
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. |
Bainemo
Member #11,031
June 2009
|
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. |
bamccaig
Member #7,536
July 2006
|
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. References
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Malinus
Member #11,332
September 2009
|
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. |
Bainemo
Member #11,031
June 2009
|
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. |
LennyLen
Member #5,313
December 2004
|
Bainemo said: 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.
|
decepto
Member #7,102
April 2006
|
Bainemo said: 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;
-------------------------------------------------- |
OnlineCop
Member #7,919
October 2006
|
nick carlson said:
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.
|
GClaudiu
Member #10,728
February 2009
|
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) |
weapon_S
Member #7,859
October 2006
|
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. References
|
GClaudiu
Member #10,728
February 2009
|
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 |
OnlineCop
Member #7,919
October 2006
|
My favorite STLs:
|
Audric
Member #907
January 2001
|
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.) |
bamccaig
Member #7,536
July 2006
|
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. References
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|