Advertisement

Easy? Struct question

Started by November 27, 2001 02:14 PM
10 comments, last by Peon 22 years, 11 months ago
Well I feel that it is good to know what kind of advance technqiues exist, so that you''ll have an idea of what to look into when you get there But anyway, I actually got a simple linked list running in class today... I could call a CreateUnit function that would create a new node on the list and then move the current pointer to the end of the list. It seems like a pretty good way to do things, but I''ll have to play with it a bit still. There are always arrays I guess, they''re a lot more straightforward

Peon
Peon, it sounds like you are trying to do this using C++(object oriented programming). If you are not, then just ignore this message. If you are, then I have my 2 cents to add ;-).

First, I agree that to start, just stick to arrays to make things easier until you learn more complex topics.

Second, objects are supposed to be self contained. That is, any direct manipulation of the object data should be performed by the objects methods(functions), not directly by your program.
Also, any class you define acts just like another variable definition, like ints or floats. It does not do anything by itself, until a variable is assigned to its type.
When you define a class, it should be contained within its own set of files, ie Tank.h/Tank.cpp. This may seem like a lot of work to start, but when you make one change to a single class, you can then just compile the one file and then link them all. It also makes it easier to track down bugs.
Any way, I hope I didn't confuse you too much, but I will leave an example of how I would structure the tank class:

//filename=Tank.h
//only the definition goes in this file
#ifndef TANK_H //this just prevents multiple inclusion
#define TANK_H

class Tank {
public:
Tank(void); //constructor (has no return type)
Tank(int); //alternate constructor
~Tank(); //destructor (there is only one)
void loseHitPoints(int);
void gainHitPoints(int);
void setHitPoints(int);
void setAlive(bool);
bool getAlive(void);
void drawTank();
private:
int hitPoints;
int speed;
int power;
bool isAlive;
}
#endif
//end of file Tank.h

//filename=Tank.cpp
//all method definitions go in here
//(method is just a fancy name for a funtion)

//first constructor
Tank::Tank() {
hitPoints=50;
power=20;
speed=5;
isAlive=true;
}

//second constructor
Tank::Tank(int hp) {
hitPoints=hp;
power=20;
speed=5;
isAlive=true;
}

//destructor
Tank::~Tank() {
//this is where you would deallocate memory if needed
//it is not needed in this simple example
//this function is automatically called by the compiler
//when an object of this type is deleted
}

void Tank::loseHitPoints(int lose) {
hitpoints-=lost;
}

void Tank::gainHitPoints(int gain) {
hitpoints-=gain;
}

void Tank::setHitPoints(int set) {
hitpoints=set;
}

void Tank::setAlive(bool alive) {
isAlive=alive;
}

bool Tank::getAlive() {
return isAlive;
}

void Tank::drawTank() {
//use this to let the tank draw it self
//I left out a lot of detail, like the
//tank can keep track of its own postion
//then when you draw it, you don't have to know
//where it is, just tell it what part of the
//screen is visible and it will draw itself if
//it needs to
}
//the '::' is called the scope operator
//scope is just limiting the functions to use within the
//object called Tank. so just put 'Tank::' before
//every funtion and it tells the compiler that this function
//belongs to tank
//end of file Tank.cpp

//filename main.cpp
#include "Tank.h"
const int MAX_TANKS=10; //C++ way instead of #define
Tank gTankGroup[];

void CreateTanks(void);
void DestroyTanks(void);

int main() {
CreateTanks();
for(int i=0;i if(gTankGroup.isAlive()) {
gTankGroup.drawTank();<br> }<br> }<br> DestroyTanks();<br>}<br><br>void CreateTanks() {<br> gTankGroup=new Tank[MAX_TANKS*sizeof(Tank)];<br> //using new automatically calls the constructor<br>}<br><br>void DestroyTank() {<br> delete []gTankGroup;<br> //using delete automatically calls the destructor<br>}<br>//end of file main.cpp<br><br>I would like to give more detail, but I think I have rambled on long enough already. I didn't even include stuff to have the tank move itself. Plus you could build AI right into the class.<br><br>Anyway, I hope I didn't confuse you too much. If you have any questions, feel free to ask.<br><br>PS. Remember any class you create is treated almost the same as any built in data type, like ints or floats. </i> <br><br>Edited by - CaptainJester on November 30, 2001 11:30:48 AM<br><br>Edited by - CaptainJester on November 30, 2001 11:33:57 AM
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement