Question!

This is a discussion on Question! within the Game Programming forums, part of the General Programming Boards category; Okay, so this class defines a Cross Object, a Cross is a Mobile, is an Object.. Code: #ifndef C_CROSS_H #define ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Question!

    Okay, so this class defines a Cross Object, a Cross is a Mobile, is an Object..

    Code:
    #ifndef C_CROSS_H
    #define C_CROSS_H
    
    
    #include "Mobiles.h"
    
    class Cross : public Mobiles
    {
    public:
    
    
    	MS3DModel *Model1; // pointer to the model all cross instances use
    };
    
    #endif

    What happens if we want more than one Cross Object on the screen at the same time? I don't want to have to hard code another instance of the cross object, how can I make it so the user can create an instance of this object, (say we have a peasant object that has a build function, that can click build->cross, we place another cross on the screen, at a given location)


    Is this what template classes are used for? I'm sure someone here can enlighten me
    Last edited by Shamino; 12-13-2005 at 06:45 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Man you guys are slacking, gamedev is so much quicker!

    I can create a vector of scene objects

    then under certain conditions I can go

    scene_objects.pushback( new Cross );
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Something you might want to note is that vectors model a dynamic array. The internal structure is not specified by the standard AFAIK, but since they provide for random access, adding or deleting elements anywhere except at the end you'll suffer performance loss to some extent. I'm sure there's other considerations as well.

    The reason I say this is because I often see people say, "just use a vector" as if there are no other containers. Sure, a vector is simple and flexible, but it might not be the best container to use (especially in a game where performance is very important)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    So when I add things to the Scene_Objects vector, I can't exactly delete things out of it without more than likely coming across problems....

    Thats not good, I need something I can add to AND remove from with just a few calls...

    Couldn't I just clear out the whole vector and rewrite it? Or would this be horridly inefficient?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I recommend vectors for editor programming where you don't know how many objects will be present.

    I recommend static arrays for games when you do know how many objects will be present.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Depending on what functionality you need, you can use a queue or a stack.

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'll probably end up using a stack of information, it sounds like it may not be the easiest to work with, although it does support everything I need to do, like Bubba has explained to me in other posts, I could use a vector and just clear it/rewrite every time I update the screen, but ultimately, if the game ever got large at all, it would be slow and inefficient...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Premature optimisation folks... really, just go with a vector. If you experience performance problems and you can trace them back to the vector, you will need to optimize it. You'll probably not write the next Doom this way, but you will get your game running. And your performance problem will likely not come from a vector, but from a misused/misunderstood algorithm anyway.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    MS3DModel *Model1;
    If all instances use the same model, make it static.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Thanks for the tip nvoigt.

    I'll use a vector for my engine, if it does get slow, which I don't really expect it to I'll rework it later on..

    Make it static, is the optimization in this just the fact that its now has a Static nametag? Are there technical reasons for making a pointer static?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I would use a vector until you get your game resource system up to snuff. A vector is very easy to use and I've gotten good framerates while using them. A bad algo will definitely hurt your code more than a vector will.

    And since vectors can be treated like arrays in the code, it's not that much of a change to later change the code to use an array.

  12. #12
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Shamino
    Thanks for the tip nvoigt.

    I'll use a vector for my engine, if it does get slow, which I don't really expect it to I'll rework it later on..

    Make it static, is the optimization in this just the fact that its now has a Static nametag? Are there technical reasons for making a pointer static?
    Less pointers (one for the class, not one for every instance), and initialization (once for the class, and never again), and it clears up your intentions (if those are to use only that model for all instances of each type: Ship1's or Cross or Tank2's for example).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #13
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Thanks for clearing that up Dae...

    My problem now is figuring out how to create instances of my objects on the fly during runtime....

    I found a way, but it isn't very OO, I'll show you

    Code:
    #include "World.h"
    
    std::vector<Object*> Scene_Objects;
    std::vector<Object*>::iterator Iter;
    
    
    void World::AddObject(Object * Obj)
    {
    	Scene_Objects.push_back(Obj);
    };
    
    int World::AddObjectToWorld(int type)
    {
        Object * Obj = NULL;
    
        switch (type)
        {
        case OBJ_CROSS:
            Obj = new Cross();
            break;
        }
    
        if (Obj != NULL)
        {
    		World::AddObject(Obj);
        }
    
    	return 0;
    };
    Even with this I have to database an enum with every model, then run a switch for every object..

    I think this is what I need to use: http://www.gamedev.net/reference/art...rticle1415.asp

    I think that is the key to creating instances of the object in an OO way during runtime
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  14. #14
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Well, creating Object's on the fly at runtime isn't a problem. The problem is that World will probably want more information than Object can provide. So passing say Ship1 to World::AddObject(Object * Obj) would mean you would lose velocity, mass, etc. variables in Ship1 (or Cross, etc.). Your code would have the same effect if you just directly sent a Cross object to World::AddObject(Object * Obj). I couldn't think of a solution to this though, so I got lazy and made a few temp vectors to keep track of all Objects. Pretty much the same idea, and it doesn't well IMO.

    Code:
    class LLObjectSystem
    {
    private:
        static unsigned int m_uiObjectCount;
        static unsigned int m_uiTreeCount;
        static unsigned int m_uiMobileCount;
    
        static std::vector<NObject*> m_vecObjects;
        static std::vector<CTree*> m_vecTrees;
        static std::vector<CMobile*> m_vecMobiles;
    
    public:
        LLObjectSystem() {}
    
        static bool Add(CTree* p_Tree) 
        { 
            m_vecTrees[m_uiTreeCount++] = p_Tree; 
            std::cout << "- Tree Added." << std::endl; 
        }
    
        static bool Add(CMobile* p_Mobile) 
        {
            m_vecMobiles[m_uiMobileCount++] = p_Mobile; 
            std::cout << "- Mobile Added." << std::endl; 
        }
    
        static unsigned int GetMobileCount() { return m_uiMobileCount; }
        static unsigned int GetTreeCount() { return m_uiTreeCount; }
    };
    That design pattern looks interesting. I've only read 50 pages of Design Patterns - Elements of Reusable Object-Oriented Software (and they were very useful), so I can't tell if this allows you to use any class. If it would let you use Ship1, Tank2, Ship2, Cross, and Objects, etc. with World::AddObject(), without losing any data, that would be great.
    Last edited by Dae; 12-15-2005 at 01:55 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  15. #15
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    http://en.wikipedia.org/wiki/Factory_method_pattern

    Found that on Wikipedia, Seems like a much more, dumbed down version of the Reflective Factory method, just what I need
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21