Thread: Multiple class instances

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    106

    Multiple class instances

    This question has been bothering me for quite a while... If you have a soldier then an enemy class. With only one instance of the soldier class and multiple instances of the enemy class, and you wanted to check for conditions between the soldier and all the enemies wouldn't the best method be to have all of your enemies checked for certain conditions against the soldier class...

    And all of a sudden about 20 minutes ago I came up with the idea of having a vector array of your enemy class so that way you an add instances if needed and just put the conditionals a for loop, looping through the array and using the array size to tell it when to stop. Is this the correct method(I hope you can understand what I'm trying to explain)

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your logic is quite difficult to follow. I'm not sure what the difference between method 1 & 2 are. Perhaps if you posted pseudo code?
    Method 2 sounds like a valid approach, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    106

    Thumbs up

    its one method.. I made some typos.. the second paragraph is the actual method to be used... And some semi pseudo code


    assuming we have two classes enemy and soldier
    Code:
    enemy opfor[10];
    soldier fred;
    
    for (int loopNumber; loopNumber <= opfor.size(); loopNumber++){
           if (fred.location == opfor[loopNumber].location){
                    someThingHappens();
           }
    }
    I'm not sure what I was going to need to do this for, but this would be the best way to do this, especially if the array was a vector right?... Thanks for the help

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Not sure that code even works. opfor looks like a std::vector but is not declared as one.

    You could derive all soldiers and enemies from a base interface and store them in a vector of the base type.

    Code:
    class IEntity
    {
    };
    
    class Soldier : public IEntity
    {
    };
    
    class Enemy : public IEntity
    {
    };
    
    std::vector<IEntity *> Entities;
    As long as Soldier and Enemy (this name is far too vague) implement the IEntity interface and adhere to it this system should work for your needs.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are enemies derived from soldiers?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Are enemies derived from soldiers?
    They could be but that would limit all enemies to soldiers. This is why I chose an abstract IEntity class to derive from. The name sucks big time but it was only an example.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I wasn't sure if he just had a base type soldier, and the player was a soldier (or whatever he's doing), and then enemies were just soldiers with extras. Or if they're two completely unrelated classes, or what exactly.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    All of this should be a clue to the OP that the design is at best vague and at worst confusing. Perhaps a bit of re-work is in order.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    106
    Hi, sorry for taking so long to back to this, These are just example classes no meant to be detailed. I'm just trying to figure out how you would detect interactions when theres a large amout of objects so you don't have to type every piece out. I'm guessing that the best method would be an array with a loop to check all the objects against a certain one... and yall are right about the class ideas they were poor design. I meant for the soldier class to be the protagonist and the enemy class to antagonist, but i should have them inherit from a class and just make modifications need for each when i get to the point of doing something with this

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    One common way is to employ a design pattern known as the Observer Pattern.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'm just trying to figure out how you would detect interactions when theres a large amout of objects so you don't have to type every piece out.
    o_O

    Can you define "large amount"? Is it ten or so? (The example you posted had ten enemies.)
    Can you define "interactions"? Are we only talking entities attempting to "share the same space"?

    The "Observer Pattern" may not be a bad idea if you only have a few common interactions and few dozen entities, but the overhead could add up quickly when you have a lot of possible "collisions".

    Can you give a better example of what you are trying to do?

    Soma

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    106
    It looks like the observer pattern is what i was loooking for... Any suggestions for general game progamming books to get me "thinking like a game programmer"

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The pattern is one suggestion since its not clear what you want to do. In any case, you shouldn't have your enemy classes talking directly with your player class and vice versa (lets call them both "agents"). Your agents talk to a game engine you need to develop and this engine processes and notifies the others. The Observer pattern (and variations) can serve that purpose. But it's a no-no to have your enemies directly notifying your player class or the other way around.

    As for game development books... others may come with their suggestions. I'm not versed in that type of literature.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    o_O

    I can't think of any game development books I would recommend right now, but I can list a few you shouldn't get just in case you are tempted. (The list has no particular order.)

    Beginning C++ Through Game Programming
    Tricks of the Windows Game Programming Gurus
    Tricks of the 3D Game Programming Gurus
    Introduction to 3D Game Programming with DirectX
    Programming Role Playing Games with DirectX
    3D Game Programming All in One
    Game Programming Gems 1
    Game Programming Gems 2
    Game Programming Gems 3
    Game Programming Gems 4
    Game Programming All in One
    Data Structures and Algorithms for Game Developers
    C++ For Game Programmers
    Beginning Game Programming
    Sams Teach Yourself Game Programming with DirectX
    Game Coding Complete
    Game Programming for Teens

    Actually, it is probably best to avoid any book with "All in One", "Teach Yourself", "Complete", "Teen", "Wisdom", "Gem", "Paid", or "Ultimate" in the title and any "Sams" book.

    I bought all the "Game Programming Gems" books I have as part of a large acquisitions from a friend. I would not have bought the second one after having read the first.

    Yes. I have read several thousand books on programming, and I still read introductory books as part of a review board. Thanks for asking.

    Soma

  15. #15
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    [Edit]
    I decided to start another thread.
    [/Edit]

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Queues, Nodes and passing instances of a class
    By xshapirox in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2004, 01:06 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM