Jump to content
  • Advertisement
Sign in to follow this  
studentTeacher

An enum made of structs

This topic is 1894 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I am trying to make an enum of structs, and was wondering how I could do that? for

example:

struct foo {
   int i;
   int j;
   Bar b;
   char c;
   //other stuff
};

enum fooEnum {
   //here, I want to make an enum of 'foo structs'. Is this possible?
}; 

 

Please help!

Share this post


Link to post
Share on other sites
Advertisement

No you cant do that.

 

I'd also be curious to know what you're trying to accomplish with this idea.

Share this post


Link to post
Share on other sites

Instead of putting the data in the actual enum, you can place it in an array and index it via the enum values.

struct Foo
{
   int i;
   int j;
   Bar b;
   char c;
   //other stuff
};

enum Biz
{
	Biz_1,
	Biz_2,
	Biz_3,
	Biz_Count
};

Foo elements[Biz_Count];
elements[Biz_1].i = 10;

I believe it is as close as you can get to what you're asking for in C++.

Share this post


Link to post
Share on other sites
When asking this type of question [and when writing real code], it is much better to use identifiers that give a clue as to what the code is trying to do. As it is, the only valid answer is "no". The suggested workarounds are wild guesses, because we don't know enough about the situation.

Share this post


Link to post
Share on other sites

Honestly, I'm doing this for a LONG list of attacks (like in pokemon) and I want an easier way to identify them, rather than just an index in an array or something. So for example, I have:

 

struct Move {
    char*       m_cpName;
    int         m_iID;
    MoveClass   m_class;
    Target      m_target;
    int         m_iPP;
    int         m_iAccuracy;
    int         m_iBasePower;
    int         m_iPriority;
    Type        m_type;
    int         m_iFlags;
};

 

Now, I would like to make an enum of the attacks, to make for easier lookup:

 

//create an enum of attacks, of type Move
enum Moves {
   TACKLE,
   GROWL,
   HYPER_BEAM
};

 

So when adding an attack to a creature, I'd like to just say something like this:

class Man : public Creature {
private:
//code...
   Move m1 = TACKLE;
//code...
};

 

It looks like things just won't be this easy for me though . . .

 

EDIT: By the way, I'm sorry for using general code. I guess at the time I was looking for a general answer that I might use in the future . . . but I'm now more sure that specific code might do that job better.

Edited by studentTeacher

Share this post


Link to post
Share on other sites
If your list of attacks is really that long, you'd probably be better off storing them as data to be loaded into structures at run-time, rather than hard-coding them into the executable. That way when it comes time to tweak and balance you're not constantly re-compiling the exe every time you make a change. Look into something like YAML, JSON or XML for an idea how to do this.

Share this post


Link to post
Share on other sites

Even if we ignore whether they're hardcoded or not... I'd just make an array of structs and then make the enum just a list of indices to that array (bonus because that can end up saving lots of space if you need to store these values).

 

Also the idea of not hardcoding it would break the use of enums, since at that point you'll probably need to start all other data in those files too to make it even remotely sane =P (unless you want to hardcode the moves themselves and only allow tweaking their values, rather than allowing an arbitrary amount of moves) Yes, one could argue that's the best idea, but if a large chunk of code is written already that may not be as nice.

Share this post


Link to post
Share on other sites

Hmm... I think I understand what you want. I do a similar thing with animations, if I'm getting you right. Make an enum:

 

enum AttackTypes{Tackle, Growl, HyperBeam};

 

And then make an array of Move objects and use the enum values as the indices. I feel like I might be repeating what everyone else said, or what you said you were trying to avoid, but then part of me wonders if you might've not tried that solution. Since you're using structs, you'll need a function that basically works as a constructor for each object, no? It'd look like:

 

 

void ConstructMoves(Move[]){
   for(int i = 0; i < MAX_NUM_OF_MOVES; i++){
      MoveArray.attackPoints = 15;
      MoveArray.pp = 20;
      //etc, etc
   }//end of for()
}

That would be a generic way to construct every move, though. What I would do is write an XML file with the values I need, load up the file before I start the for-loop, and then cycle through the <Move> elements, assigning the values in the element to the Move object in the array based on the index. I mean, the Move element would be something like:

<Move number = "1" attackPoints = "15" pp = "20" />


Since enums are integral values, the "number" attribute represents the value of the particular enumeration you're referring to. Since in my above example, Growl is the value 1, the above <Move> element would represent Growl. In the for-loop, you'd just check that the number value of the element and the number value of the index line up. It'd look like:

 

void ConstructMoves(Move[]){
xmlObject = loadXMLFile("Moves.xml");
  for(int i = 0; i < MAX_NUM_OF_MOVES; i++){
    while(xmlObject.CurrentElement() != NULL){
        if(xmlObject.CurrentElement().attribute("number") == i){//If this is true, we know the element matches the Move we want
           MoveArray.attackPoints = xmlObject.CurrentElement().attribute("attackPoints");
           //and so on
        }
        xmlObject.MoveToNextElement();//This will return NULL when there is no next element in the XML document
    }//end of while()
  }//end of for()
xmlObject.CloseFile();
}

 

Even if you don't go this route, I thought it'd be useful for future readers who might be interested in this sort of solution to your problem. I use pugixml to load and manipulate XML documents, by the way.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!