Thread: structure question

  1. #1
    Unregistered
    Guest

    structure question

    I am making a role playing game and instead of having a bunch of variable to have to assign line by line when a character is chosen
    ex. hp = 30;
    mp = 30;
    etc.

    I am trying to do it as a structure. I believe i have seen it done before but I can't remember where. Is there a way I can, instead of having to do

    stats.classname = "Warrior";
    stats.hp = 30;
    stats.mp = 30;
    etc.

    just go:

    stats{"Warrior"; 30; 30};

    I tried this but it didn't work, is the sytax wrong, or is this not possible?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    struct foo {
      int a,b;
    };
    
    int main() {
      foo bar = { 1, 2 };
    
      bar = (foo){ 3, 4 };  // warning: ISO C++ forbids compound literals
      return 0;
    }
    Works with the GNU compiler, but it seems non-standard.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    you may want to take this question to the c board, as your current posting is to the c++ board. One might suggest, c and c++ are virtually the same thing, well I believe good ol' mr. stroustrup would have to say no, c++ has this nifty little hand bag called classes, which make structs obsolete and make exactly what you're asking for possible. I will show you two ways to do what you would like to with a class but first I'll show you an example class you might use.

    const int MAX_NAME_LEN = 40;
    /* With a name longer than this, the NPCs will have a tough time pronouncing it, and there's nothing worse than an NPC seg fault.

    note: please excuse the lame sense of humor, it's really early, or really late, depending on how you're looking at it, and I'm still a little groggy.
    */

    class Character {

    public:

    /* Default Constructor */
    Character();
    /* Stats Constructor */
    Character(int newHP, int newMP, char newName[]);
    /* Constructors - initialize your character with either the default values, or with newHP, newMP, and newName. */

    setStats(int newHP, int newMP, char newName[]);
    /* Set the characters stats to these new and improved ones. */

    private:

    int hp, // The character's hps
    mp; // The character's mps
    char name[MAX_NAME_LEN]; // The character's name
    };

    And now, 2 ways to create or update a character's stats in one swift blow.

    int main() {

    /* 1. Create the character, using the stats constructor. */

    Character bob(30, 30, "Bob");

    /* or...

    2. Create a character (with its default values), and then set all their stats with the setStats() member function. */

    Character tom;
    tom.setStats(30, 30, "Tom");

    return 0;
    }

    Hope this helps some. 1 other thing, it's good to set a relatively difficult goal for yourself, and work towards it as you learn more. However, judging by your ineptitude with a struct, I would say read a little more, and consider learning C++, since classes would greatly simplify programming a rpg - believe me, I know personally.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    woops, forgot to write the code for the Character class. I guess I'll give you a little bit more, but you have to promise to only let this wet your appetite, so you go out and discover how to use classes fully.

    ok, here's the code for the constructors and setStats() member function of the class Character...

    /* Specify your own default values for the hp, mp, and name. */
    Character::Character() {setStats(0, 0, "player");}
    Character::Character(int newHP, int newMP,
    char newName[]) {
    setStats(newHP, newMP, newName);
    }

    void Character::setStats(int newHP, int newMP,
    char newName[]) {
    if(strlen(newName > MAX_NAME_LEN) {
    cerr << "ERROR: specified character name too long, terminating your life for being a jackass\n"; // Or whatever...
    exit(1);
    }

    if(newName[0] == '\0') {
    cerr << "ERROR: you're a communist, aren't you? take your erros elsewhere!\n";
    //system("logout");
    exit(1);
    }

    hp = newHP;
    mp = newMP;
    strcpy(name, newName);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. Simple C# structure question
    By sketch in forum C# Programming
    Replies: 4
    Last Post: 09-14-2007, 04:29 PM
  3. data structure question
    By miami_victor in forum C++ Programming
    Replies: 13
    Last Post: 12-31-2004, 12:56 AM
  4. Array and Structure Question
    By loopshot in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2004, 05:10 PM
  5. structure question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 09:04 PM