question about classes... maybee

This is a discussion on question about classes... maybee within the C++ Programming forums, part of the General Programming Boards category; ok this is hard for me to explain... so im gonna break it up if i can... (sorry for the ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    Question question about classes... maybee

    ok this is hard for me to explain... so im gonna break it up if i can...
    (sorry for the long post)

    im trying to make a game where data is loaded into mem for each different character
    this memory i would like to have stored into an array of classes (Character[])

    then i would like to be able to make an array of instance classes or something similar to an array of child classes based on one index of each Character[] class, so this array of classes would probably have to be 2d (Player[ , ])

    the player[ , ] class should be able to use the data loaded by the Character[] class like below:

    player[0,0] and Player[0,1] should have access to the data loaded by Character[0]

    player[1,0] and Player[1,1] should have access to the data loaded by Character[1]

    so basically:

    Player[A,I] has access to Character[A]'s loaded data

    SUMMARY:

    Character[ ] - used to load and store the character data (model, texture, ect...)

    Player[ , ] - keeps track of player values (x,y,z coordinates and weapon)

    I Have the Character[ ] class made and working

    Im basically trying to figure out how i could set this sort of thing up in c++

    WHAT I WAS THINKING OF DOING... BUT AM NOT SURE IF IT WILL WORK:

    1:
    have the loader put the data into Character[ ] and keep track of how many
    characters have been loaded

    2:
    wait for players to join... and wait for game to begin initialization

    3:
    create 2d array Player[A,B] with 'A' being the number of characters loaded
    and 'B' being the number of players in the game

    4:
    create a bunch of pointers to the data in Character[ ] for each Player[ , ] index

    THE PROBLEM I SEE WITH THAT:

    I will almost always end up with a bunch of empty Player classes


    This would be alot easier to do if i knew how many different characters there were going to be... but sadly there is no way of me knowing...

    Can anyone Think of a more efficient way to do this???

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If I follow you correctly, you are saying you want a Character class, and actual character objects would be the different kinds of characters (wolfman, shegirl, godface, etc) that players could choose from. So in that sense, character would me a trait of a player object, right?

    Sounds fairly straightforward. I think the 2D array idea is a bad one. You would want two separate 1D array (players, and characters). You just include a pointer to a character object in the player class:
    Code:
    Character *archetype; // or whatchamacallit
    This points to a pre-existing instance (object) of class Character (such as the shegirl or the godface). You do not need more than one pointer for this.

    However, if the character object contains data that can change (eg, how many slimebuckets this godface player has left), then that model needs a slight tweak: rather than using a pointer to the one instance of the specific character archetype, you should initialize the player with a copy of such an object. This way Bobo the Godface can spend slimebuckets without it affecting the number of slimebuckets available to other godface players.

    General rule: KEEP IT SIMPLE. As simple as possible. Make simplicity a priority. Do not get unnecessarily complex because you think that will be better. It won't.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Well, I agree to what MK27 has said. Character is better of as a member of Player that can be initialized inside a customized ctor of the Player class.

    i.e:
    Code:
    class Player
    {
    private:
      Character archetype;
    
    public:
      Player(Character &chara): 
          archetype(chara)
      {
      }
    };
    Also by doing it this way, you're actually using a dependency injection that is actually a good programming practice.

    For the Character objects itself, maybe you should consider using std::vector so you can add items indefinitely.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    13
    both ideas sound good to me... but i dont see how making Player the parent to Character will work... i would think it would be the other way around... having Player be a child of Character... I need to only load the data for each character once... and if im understanding the above properly, it seems like i would have to load it for each character...

    if i load the character data once for every player i will run out of memory really fast, i cant have that...

    how would i go about using std::vector with my classes?

    to get a little more complicated in explaining my situation the Character class holds mesh data and the default animation controller for the mesh data (directx9) and i need each instance of the Player class to have its own clone of the default animation controller... this i dont need help with... i just have to figure out the best way to make the Player class... I have to set it up something like this but im not sure how to go about it

    Character Class:
    Vertex and material data
    A function to load from an x file
    Any addition functions loading might require
    An unused animation controller
    Player Class:
    A copy of the animation controller
    A function to render the model instance
    Any player specific data like ammo or position

    The only way I see this of working is if in some way Character acts as a base class and Player acts as an instance class... But since im going to have an array of Character classes and an array of Player classes for each index of the Character array, im having trouble determining the best way to set this up
    Last edited by gizmo3; 06-08-2010 at 04:00 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Have you looked up std::vector?
    For the second statement, it shouldn't cause problems since it's part of Character and Character would be part of Player.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    13
    so u r saying that the way they have told me to do it above Character is both child and parent to Player and Player is both child and parent to Character... I think that is impossible... but my C++ isnt that great...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Quote Originally Posted by gizmo3
    so u r saying that the way they have told me to do it above Character is both child and parent to Player and Player is both child and parent to Character...
    No, no one told you to do that. What was recommended to you was to make it such that a Player has-a Character. Character would be an abstract base class, but you derive the actual Character subclasses from it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    13
    ok... so i guess im having trouble understanding "archetype" i tried google but i couldnt find anything that really explains how/what "archetype" works/is...

    can i has a link plz?

    or an explanation?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Where did you get archetype from???
    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.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    13
    Quote Originally Posted by MK27 View Post
    Code:
    Character *archetype; // or whatchamacallit
    This points to a pre-existing instance (object) of class Character (such as the shegirl or the godface). You do not need more than one pointer for this.

    and...

    Quote Originally Posted by g4j31a5 View Post
    Well, I agree to what MK27 has said. Character is better of as a member of Player that can be initialized inside a customized ctor of the Player class.

    i.e:
    Code:
    class Player
    {
    private:
      Character archetype;
    
    public:
      Player(Character &chara): 
          archetype(chara)
      {
      }
    };
    Last edited by gizmo3; 06-08-2010 at 04:27 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    It's simply a name. You can call it whatever you will.
    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.

  12. #12
    Registered User
    Join Date
    Jun 2010
    Posts
    13
    DAMN...

    now i feel like more of a noob than i did before...

    I must be either REALLY tired or REALLY confused...

    it seems to me like Player Is-A Character makes more sense than Player Has-A Character
    Last edited by gizmo3; 06-08-2010 at 04:33 AM.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's because you're confused about your abstractions. A "Character", in your game, is a template, containing stuff like "how does this thing look". A "Player", on the other hand, is a concrete instance. It contains stuff like "where am I".
    A Player will typically be an instance of Character, in that there can be many players with the same character, and they have shared properties in the character. However, going from here to "Player is-a Character" is the same fallacy as, given a class Rectangle and an instance myRect, that "myRect is-a Rectangle". It's true in the English language semantics, but in object-oriented thinking, those two things exist on different levels of abstraction.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Jun 2010
    Posts
    13
    so if i did something similar to the third post i would only have each different Character loaded once?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    The idea here is that each player contains some data. Textures or whatever it is/was.
    From this Character class, create instances of it for as many players you need.
    If you need different kinds of characters, then derive subclasses from them with specialized properties. Then create instances of those.
    Each instance should hold the data it requires to function, put simply.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes question...
    By Raigne in forum C++ Programming
    Replies: 24
    Last Post: 09-12-2006, 02:46 AM
  2. Replies: 2
    Last Post: 07-28-2006, 03:59 PM
  3. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 03:40 AM
  4. Classes and Win32 API, and another Question
    By philvaira in forum Windows Programming
    Replies: 10
    Last Post: 04-10-2004, 08:21 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 11:17 PM

Tags for this Thread


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