Sign in to follow this  
studentTeacher

An enum made of structs

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

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[i].attackPoints = 15;
      MoveArray[i].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[i].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

Why not just declare objects?

 

// Move.h

extern const Move
  TACKLE,
  GROWL,
  HYPER_BEAM,
  ...;

// Move.cpp

const Move TACKLE(...);
const Move GROWL(...);
const Move HYPER_BEAM(...);

 

If you need these in a table (e.g., to refer to them by integer ID), then you can build that separately.

Share this post


Link to post
Share on other sites

I did a little shell for you to look at and fill in yourself. Consider using tinyXML to load XML. (No idea if any of this compiles, as it was only to show you what to do)

 

 

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;};
enum 
Moves{   TACKLE,   GROWL,   HYPER_BEAM};
class Moves{
public: bool LoadMoves(std::string filename) {  
//LoadXML file  
//As you load them you can tell in the XML I do <Move type = "1">  
//this means that this move is tackle because in the enums its the first value  
//Load in all of the related data to the move.  
return true; 
} 
Move* GetMove(int type) {  
//checking and return the move  
return nullptr; 
}
private:  
std::vector<Move*> m_listOfMoves;
};

?

?

<?xml version = "1.0"?>
<Moves>
 <Move type = "1">
  <Name>Tackle<Name/>
  <ID>1</ID>
  <PP>3</PP>
 </Move>
</Moves>


 

Edited by jellyfishchris

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this