Public Group

Archived

This topic is now archived and is closed to further replies.

enums and structs?

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

Recommended Posts

wat r enums and structs?

Share on other sites
all i can tell you friend is...

LOL!

p.s. either use the search feature, go through the Articles and Resources here at GameDev, or get a tutorial on C.

Share on other sites
Um, may I suggest you pick up a book on C or C++? They are very fundamental construct. In fact too fundamental for me to be able to explain, in the same way the I would have problem explaining the english word "is".

enum is a simple way to define sequential constants and to have them grouped as a type. If you have enum mode { mode_x, mode_y, mode_z };. mode will be a type, such as int or char, that will only be able to hold the values mode_x, mode_y and mode_z. You can then declare a variable to be of type mode.

structs are used to build structures of variables. Eg. struct { int age; char *name; };

...

Gah, I''ll have to explain the entire language in order to be able to explain this. Look for C/C++ tutorials; they are everywhere.

Share on other sites
An enum is basically a set of named integer constants defined in sequence. As an example:

enum DaysOfTheWeek {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

In this case, Monday is defined as 0, Tuesday as 1, Wednesday as 2, and so on. Enums always begin enumerating at 0 unless you specifically state otherwise:

enum DaysOfTheWeek {Monday=15, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

In this case, the enumeration begins with Monday==15, Tuesday==16, etc...

Enums are useful for syntactic clarity (the expression if (Day==Monday) is more readable at a glance than if (Day==0)), and are useful in most of the same situations in which people use constants or #defines. Enums can promote modularity, in that they can be defined inside a class, and thus are owned by that class.

A struct in the C-sense is merely an aggregate of data members. It is useful for treating a collection of data as a single entity, rather than scattered variables. For instance, a basic Monster could be defined as:

int Life;
int Damage;
int MovementSpeed
int XCoord;
int YCoord;

This gets tedious in a project of any complexity. Structs provide a natural next step toward defining a monster:

struct Monster
{
int Life;
int Damage;
int MovementSpeed;
int XCoord;
int YCoord;
};

Now, instead of having to juggle individual values, or keep track of separated data, the data have a communal tie. You can decalare monster:

Monster MyMonster;

and access that individual monster''s data:

MyMonster.Life=100;

Struct is a step toward object-oriented programming, in which all the data and functionality needed to deal with, for example, a Monster is all encapsulated in one place.

In the C++ sense, a struct is syntactic sugar for a class with all public members; in all other respects, it is treated as a class. C++ progresses the OOP paradigm, by allowing structs (and classes, of course) to include functions (or methods) as part of the definition. Now, not only does your monster have data, it also has associated behavior:

struct Monster
{
.
.
.
void TakeDamage(int Amount){Life -= Amount;};
};

You can easily cause damage to an individual Monster now by calling:

MyMonster.TakeDamage(10);

I''d recommend picking up a basic beginners book on C/C++, as it can answer your questions better than I can.

Good luck.

Josh
vertexnormal AT linuxmail DOT org

Check out Golem: Lands of Shadow, an isometrically rendered hack-and-slash inspired equally by Nethack and Diablo.

Share on other sites
And if for some reason you can''t get a book yet, go to:

www.cplusplus.com

and do their tutorials (I am still working through them). So far, I am learning a lot from them. A combination between a good c++ book and cplusplus.com tutorials would work well as it would be good to learn c++ from two authors to see their techniques.

Good luck and have fun!
When you get the hang of the basics it is fun to make little programs and stuff.

Share on other sites
quote:
Original post by CWizard
They are very fundamental construct. In fact too fundamental for me to be able to explain, in the same way the I would have problem explaining the english word "is".

I don''t think an enum or a struct is as general as the word "is".

Share on other sites

void TakeDamage(int Amount){Life -= Amount;};

You can easily cause damage to an individual Monster now by .

ok, i get it. but what does this operator mean in the above:
"-="

Share on other sites
It's shorthand for doing:
Life = Life - Amount;
So basically, it takes the original value of Life, subtracts Amount from it and stores the result back in Life.

Hope that helps.

-hellz

[edited by - hellz on October 21, 2003 6:53:08 PM]

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
11

• 12
• 16
• 9
• 14
• 10
• Forum Statistics

• Total Topics
632659
• Total Posts
3007689
• Who's Online (See full list)

There are no registered users currently online

×