Advertisement

Data Structures, Classes, and all that Funk(tm)

Started by July 08, 2000 01:59 AM
21 comments, last by Goblin 24 years, 5 months ago
I am a relatively new person to C/C++ programming. I started programming BASIC in the 5th grade, I''m now going into 9th, and I have a very good understanding of programming (I think), creating most efficient code, etc. Unfortunately, I''m still a bit stuck on old BASIC-ish routines, though I''m getting more and more accustomed to C. My question, is that I read all this jazz about data structures and classes, and think what it would be brilliant for, but just sort of loose sight about how I would go about actually using it/initializing it. Instead, I end up relying on a HUGE (and I mean HUGE) array that has countless dimensions and slots for spells, spell names, spell effects, spell flags, spell stats, monster names, monster stats, monster flags... etc. (For now, I do text-based game programming, though I''m easing myself into graphics). My question is what would be the most efficient use of data structures/classes for these types of things? And which would I want to use -- data structures, or classes? I see so far that the difference is basically some strange "typedef" always preceding a data structure, and that classes have functions and con/de-structors associated with them. I''d like to get into it, I''m just a bit lost. Is there any way I could turn "if(stat[loop][temp][3]!=1)" into something more usefull... and legible. Thanks in advance for any help you have to offer! "Death to all who oppose me."
- The Goblin (madgob@aol.com)
In a C++ compiler, struct and class are exactly the same with one difference: the default data access mode in a struct is public, while in a class it''s private.
If you''re not sure about what I''m talking about then you should pick up a good book on the language.

-RWarden (roberte@maui.net)
Advertisement
I know exactly what you are talking about.

What I want to know is:

I have a combat engine that compares attack from the attacker, and defense from the defender; since the player is stored as entity ''0'' in the array, and monsters go up from there, we can do this quite easily... (obviously, the actual function is more complex

void combat(int attacker, int defender)
{
stat[defender][1]-=stat[attacker][2];
// subtracts attackers strength from defenders health
}

Is there any way that this could be solved by substiting the attacker''s stats into "entity.attacker" and defenders stats into "entity.defender" and have "entity.player" and "entity.[monstername/number]" so that I could do:

entity.defender.health-=entity.attacker.strength;

Would I need the entity class? Would this be readily possible? What would be an example of the things to set this up? Would I use data structures or classes for this?

Well, thanks...


"Death to all who oppose me."
- The Goblin (madgob@aol.com)
Anyone? Anyone at all?




"Death to all who oppose me."
- The Goblin (madgob@aol.com)

Classes and structs are exactly the same, so you can use either. For your specific purpose, you would have something like this:

    class entity{public:int attack;int defense;int health;};[/source]The simplest way to access them would be to have an array of entities. For example:[source]entity Monsters[50];entity Player;// combat function..Monsters[x].health -= Player.attack;    


Obviously, this is way oversimplified. Again, I have to strongly recommend picking up a good book on C++ to get the hang of the usage and syntax of classes.

-RWarden (roberte@maui.net)
oops, quirky forum bug =) but you can still see what I meant.


-RWarden (roberte@maui.net)
Advertisement
the usefull thing about them is creating an array with them to keep track of lots of data. like bullets, or enemies, or whatever.

JoeMont001@aol.com | www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
Then you gotta make a class like that RWarden made and probably a entity linked list. Unfortunately, I''m ''till learning, so I can''t show ''ya howta do it.

Thanks, Arthur(rockslave)
import money.*;#include "cas.h"uses bucks;
Thanks, thanks.. that saves me a bit of headache.

I was just a bit confused...

So, is there any difference in terms of efficiency or what''s more appropriate in _this_ particular situation between a data structure and a class? I''m probably going to need it all publicly available anyway, so a data structure, in theory, would be cleaner, right?

Just wondering !




"Death to all who oppose me."
- The Goblin (madgob@aol.com)
quote: Original post by Goblin

So, is there any difference in terms of efficiency or what''s more appropriate in _this_ particular situation between a data structure and a class? I''m probably going to need it all publicly available anyway, so a data structure, in theory, would be cleaner, right?



I guess a structure would be allright if you only wants to do "raw" read/write operations on your data. But if more processing needs to be done when adjusting the values stored in your structure a class would be prefereable.

Take for example the fighting example. With a structure with private data fields you would just substract some health from the monster like this:

    monster.health =- player.attack;    


But the catch is that your game would typically need a lot more processing to follow this every time you adjusted the health of a monster. For example you might need to check if monster.health is zero (the monster died), update some status panels somewhere on the screen, reward experience points to the player or whatever.

By using a class you could encapsulate all this processing into a single method (function of the class), for example monster.setHealth(int x). Then you could make the health field of that class private to prevent that you by accident wrote dirtectly to the health field somewhere in your code without remembering to update the status bar and so on.

Also if you have a more complicated formula for determinding the damage taken by a monster under attack, you could encapsulate the details of this calculations within another method. You could make a monster.attacked(int strength) function that would take into account the type of armor the monster was wearing and so on, and then finally call the setHealth method to set the health.

Regards

nicba

This topic is closed to new replies.

Advertisement