Where to use classes, when creating games (newbie here)?
Moderator: PC Supremacists
- fryz
- Chaos Rift Newbie
- Posts: 18
- Joined: Thu Jul 22, 2010 8:27 am
- Current Project: Ping Pong.
- Favorite Gaming Platforms: PC.
- Programming Language of Choice: C++, Allegro.
- Location: Lithuania
Where to use classes, when creating games (newbie here)?
I was reading about classes and reading... They are like defining variables. Like:
class var
{
int armor = 100;
int hp = 100;
};
is equivalent to:
int main()
{
int armor = 100;
int hp = 100;
}
So what's the point of using classes at all? Or there's some another usage or something to make the code easier to read and better, which i don't know of?
I've read 2 tutorials and 3 forum threads about classes (big threads), and there was only such things, as i wrote above... So i don't get it, is there some "special usage", they didn't mentioned, or it's just some fancy way to declare variables?
class var
{
int armor = 100;
int hp = 100;
};
is equivalent to:
int main()
{
int armor = 100;
int hp = 100;
}
So what's the point of using classes at all? Or there's some another usage or something to make the code easier to read and better, which i don't know of?
I've read 2 tutorials and 3 forum threads about classes (big threads), and there was only such things, as i wrote above... So i don't get it, is there some "special usage", they didn't mentioned, or it's just some fancy way to declare variables?
Learning Allegro. Hope i will learn it and make my ideas live. I have a lot of them!
Your help is greatly appreciated!
Your help is greatly appreciated!
- ismetteren
- Chaos Rift Junior
- Posts: 276
- Joined: Mon Jul 21, 2008 4:13 pm
Re: Where to use classes, when creating games (newbie here)?
Programming whit classes is a whole other way of programming, i.e. another paradigm. It's called Object Oriented Programming. It's kinda hard to explain, since it is a very broad topic, but in some languages (Java for example) every thing you write must be a class.
Unfortunately i don't know any resources explaining OOP for people who don't know anything about it. One important concept i can tell you thought, is that classes can not only have variables, but also methods (functions). So if i have a class, called NumberIncreaser that looks like this:
i can use it like this in the main method
This would print the number 19 (i know that C++ probably wouldn't convert the integer returned by getN() to a string for you, but it's aged since i've written anything in C++ and i cant remember how to do it, so just pretend that getN() gets converted).
To answer yout question: No they are not just some fancy way of declaring variable, they are a whole other way of programming. I started out with Java where everything is a class, and when i first saw C i was like "How can you even program without classes?!". To this day i probably wouldn't do a good job of programming without classes.
Unfortunately i don't know any resources explaining OOP for people who don't know anything about it. One important concept i can tell you thought, is that classes can not only have variables, but also methods (functions). So if i have a class, called NumberIncreaser that looks like this:
Code: Select all
public Class NumberIncreaser {
int n = 0;
void increaseBy(int x) {
n = n + x;
}
int getN() {
return n
}
}
Code: Select all
main() {
NumberIncreaser ni;
ni.increaseBy(14);
ni.increaseBy(5);
std::cout << ni.getN() << std::endl
}
To answer yout question: No they are not just some fancy way of declaring variable, they are a whole other way of programming. I started out with Java where everything is a class, and when i first saw C i was like "How can you even program without classes?!". To this day i probably wouldn't do a good job of programming without classes.
Last edited by ismetteren on Sun Aug 29, 2010 10:58 am, edited 1 time in total.
Re: Where to use classes, when creating games (newbie here)?
Well not only do they make having more than one of the same thing easier... They also make it so you can have functions specifically for instances of that class, rather than having global functions that serve no other purpose than for an object. Back to making it easier to have more than one of the same object.
say you have an NPCs in your game. It's easier to just make instances of the class like this assuming you have an NPC class... that would probably hold things like health, dialogue, functions for movement, animation, and maybe attack
these now automatically have default settings for the NPCs, they also have functions that pertain to these NPCs.
but if you didn't use a class you'd have to pull one of these numbers...
and so on for each NPC you have.... As you can imagine this method would get annoying and messy. There in lies the effectiveness of classes...
if you didn't understand that sorry, I'm in a hurry gotta go shopping... i got a request for glue sticks in the middle of thinking out this explanation xD
Edit: damn you beat me to it
say you have an NPCs in your game. It's easier to just make instances of the class like this assuming you have an NPC class... that would probably hold things like health, dialogue, functions for movement, animation, and maybe attack
Code: Select all
NPC npc1;
NPC npc2;
these now automatically have default settings for the NPCs, they also have functions that pertain to these NPCs.
but if you didn't use a class you'd have to pull one of these numbers...
Code: Select all
SDL_Image npc1_image = Loadimg();
int npc1_health = 100;
string npc1_dialogue = "I'm an npc... I love you";
SDL_Image npc2_image = Loadimg();
int npc2_health = 100;
string npc2_dialogue = "I'm an npc... I love you";[...]
if you didn't understand that sorry, I'm in a hurry gotta go shopping... i got a request for glue sticks in the middle of thinking out this explanation xD
Edit: damn you beat me to it
- fryz
- Chaos Rift Newbie
- Posts: 18
- Joined: Thu Jul 22, 2010 8:27 am
- Current Project: Ping Pong.
- Favorite Gaming Platforms: PC.
- Programming Language of Choice: C++, Allegro.
- Location: Lithuania
Re: Where to use classes, when creating games (newbie here)?
Okay, so basically you can write functions (declaring cariables and doing calculations) and simple variables to classes. If you'd write them to a class, you could use it just at some specific points, and easier, multiple times. That sounds nice.
However, i have a question about those public and private. As i understand it now, it seems to me, that public declares variables like global variables, like C style, or something like that, and those private things, declares variables only for particular cases. And protected members, will only be changed from the functions inside the class, which means those variables won't be able to change from outside the class, from other functions outside the class (although, i know no reason to create them outside now, after those explanations), and from the main() function. Am i right about this? And thanks for explanations.
However, i have a question about those public and private. As i understand it now, it seems to me, that public declares variables like global variables, like C style, or something like that, and those private things, declares variables only for particular cases. And protected members, will only be changed from the functions inside the class, which means those variables won't be able to change from outside the class, from other functions outside the class (although, i know no reason to create them outside now, after those explanations), and from the main() function. Am i right about this? And thanks for explanations.
Learning Allegro. Hope i will learn it and make my ideas live. I have a lot of them!
Your help is greatly appreciated!
Your help is greatly appreciated!
Re: Where to use classes, when creating games (newbie here)?
Ok so first off, I still haven't gotten protected and private down :P I know them well enough to use them, but quite frankly I'm not confident about explaining them. But yeah public can be accessed by anything. So if you made the npc class with a public variable for health anything could change it. these are examples of what you could do
you could use it like a regular variable. but if its protected or private, it can pretty much only be accessed by a member of the class (usually a public function) or by a friend class (I believe).
so you would do this to change health if it was private
Edit: also you can have any type of variable in a class. You could have a string in a class, or an SDL_Surface, or an array/vector
Code: Select all
npc1.health = 10;
int var = 7;
npc1.health = var;
var = npc1.health;
so you would do this to change health if it was private
Code: Select all
class NPC{
private:
health;
public:
void SetHealth(int new_health){ health = new_health; }
};
//main
NPC npc1;
npc1.SetHealth(20);
- mv2112
- Chaos Rift Junior
- Posts: 240
- Joined: Sat Feb 20, 2010 4:15 am
- Current Project: Java Tower Defence Game
- Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
- Programming Language of Choice: C/++, Java
- Location: /usr/home/mv2112
- Contact:
Re: Where to use classes, when creating games (newbie here)?
Public variables or functions can be accessed outside of the class, private variables or functions can only be accessed by the class itself, protected functions or variables are exactly like private functions and variables except that they are inherited by child classes:
Code: Select all
class parentnoob
{
public:
int GetLevel() {return level;}
private:
char*name;
protected:
int level;
}
class childnoob:public parentnoob
{
public:
//has int GetLevel();
protected:
//has int level;
private:
//does NOT inherit char*name;
}
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Where to use classes, when creating games (newbie here)?
you've got a wrong idea of what a class is. A class is basically creating a new type, sort of like an int or char. Each variable of this type contains all the member variables, and for a variable x with member variable y you can access it via x.y. functions can be accessed similarly, but it's x.y(). functions outside of the class can access public variables/functions. Only functions inside the class can access private functions/variables. Protected variables have to do with inheritance, which is just a bitch, and you don't have to worry about it yet.fryz wrote:Okay, so basically you can write functions (declaring cariables and doing calculations) and simple variables to classes. If you'd write them to a class, you could use it just at some specific points, and easier, multiple times. That sounds nice.
However, i have a question about those public and private. As i understand it now, it seems to me, that public declares variables like global variables, like C style, or something like that, and those private things, declares variables only for particular cases. And protected members, will only be changed from the functions inside the class, which means those variables won't be able to change from outside the class, from other functions outside the class (although, i know no reason to create them outside now, after those explanations), and from the main() function. Am i right about this? And thanks for explanations.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
-
- Chaos Rift Newbie
- Posts: 25
- Joined: Sat Apr 18, 2009 11:24 pm
- Favorite Gaming Platforms: SNES,PS1
Re: Where to use classes, when creating games (newbie here)?
I'll just throw in my 2 cents.
I think the best way to understand what a class is and how to use it, is to relate it to a blueprint in the real world. Basically, almost all real world man made objects consist of a common blueprint or design. Those blueprints are only written once, but used over and over again to make multiple instances of those objects. I.e. you have a recipe (class) for cookies, and you have a plate of cookies (objects). You could write a recipe (variables and methods) for each cookie, or you can write one recipe (class) and make as many cookies from that recipe.
So the best way to look at it when you are building a game is that everything you have in your game is an object. And all the objects in your game should have their own blueprints. Once you start realizing that some of the objects in your game like monsters, weapons, items, etc. all have similar variables and methods that they use, you'll see that you can write a few blueprints and build a lot of different objects from them.
For example, lets say you are building a room.
That's great. But now if you want to make another room.
Etc, etc, etc. This will go on forever until you have as many rooms as you need.
Instead build a blueprint for a room. Containing all the variables and functions that you'll need. And create an instance of that class instead.
To make this class even better you could have a string variable that holds a name for the room instead of coming up with a variable like 'myRoom'. And then you could create a loop to create rooms from a text file and hold them all into a list. Then if you want to reference a certain room you could look through that list to find the room with the name 'myRoom' or 'anotherRoom' etc. You just have to think creatively and how you could use classes to your benefit. Anything that you have in your game can be condensed down into a class and be made into an object. The player, camera, monsters, items, levels, rooms, tiles, environment objects, NPCs, etc.
I think the best way to understand what a class is and how to use it, is to relate it to a blueprint in the real world. Basically, almost all real world man made objects consist of a common blueprint or design. Those blueprints are only written once, but used over and over again to make multiple instances of those objects. I.e. you have a recipe (class) for cookies, and you have a plate of cookies (objects). You could write a recipe (variables and methods) for each cookie, or you can write one recipe (class) and make as many cookies from that recipe.
So the best way to look at it when you are building a game is that everything you have in your game is an object. And all the objects in your game should have their own blueprints. Once you start realizing that some of the objects in your game like monsters, weapons, items, etc. all have similar variables and methods that they use, you'll see that you can write a few blueprints and build a lot of different objects from them.
For example, lets say you are building a room.
Code: Select all
int myRoomWidth = 100;
int myRoomHeight = 100;
int myRoomLength = 100;
Code: Select all
int myRoomWidth = 100;
int myRoomHeight = 100;
int myRoomLength = 100;
int anotherRoomWidth = 50;
int anotherRoomHeight = 50;
int anotherRoomLength = 50;
Instead build a blueprint for a room. Containing all the variables and functions that you'll need. And create an instance of that class instead.
Code: Select all
class Room
{
int width;
int height;
int length;
}
Code: Select all
Room myRoom;
Room anotherRoom;
- Lord Pingas
- Chaos Rift Regular
- Posts: 178
- Joined: Thu Dec 31, 2009 9:33 am
- Favorite Gaming Platforms: NES, SNES, Nintendo 64, Dreamcast, Wii
- Programming Language of Choice: C++
- Location: Hiding In My Mum's Basement With My Pokemon Cards
Re: Where to use classes, when creating games (newbie here)?
With classes there is a whole world of possibilities!
Let's say you create a class named Entity, the Entity holds a Point struct which will be the x and y positions, like say for instance.
Now you have all your standerd get and set functions and your Constructer which, when you create object instance of, automatically defines the members in the Point struct.
Now when you create an instance of an Entity class, with a constructor you can set its attributes automatically.
Let's say you create a class named Entity, the Entity holds a Point struct which will be the x and y positions, like say for instance.
Code: Select all
Struct Point {
int xPos;
int yPos;
};
Code: Select all
class Entity() {
private:
Point position;
public:
Entity(int x, int y) {
position.xPos = x;
position.yPos = y;
}
//get and set functions here
};
Code: Select all
Entity* entity = new Entity(20, 20); //creates an instance of a new Entity object, setting its xPos and yPos to 20
delete entity; //remember to free the memory of your instance pointer to avoid memory leaks
entity = NULL;