#include <iostream>
usingnamespace std;
void Barbariana()
{
short level=1;
int strenght=25;
int dextery=10;
int vitality=20;
int mana=15;
int damage=strenght*3;
int att=dextery*10;
int block=dextery-level;
int defence=dextery*9;
int life=(vitality*3)+60;
int manapoints=mana*2;
}
int main()
{
Barbarian();
cout<<"your level is"<<level;
cin.get();
}
This is only to check if it would work.It didn't.
Is there a way in which i can declare these variables inside my function?
This was ment to be the first of my characters and i would prefer if the same-name variables would be used for every other function and applied when the user decides what character he wants to play.
Any ideas?
struct character
{
short level;
int strenght;
int dextery;
int vitality;
int mana;
int damage;
int att;
int block;
int defence;
int life;
int manapoints;
}
int main()
{
character Barbarian; //create a struct called "Barbarian"
//access variables with dot notation ( . )
cout<<"your level is"<< Barbarian.level;
cin.get();
}
You'll need to set all the values before use: Barbarian.level = 1;
I've only ever used structs about 2 times, and I never knew you could do that with them.
I'm guessing you'd have a normal struct, as above, and where you have the "// ..." you'd have your struct variables. And then you'd call the constructor right after the creation of the struct?
"struct" and "class" are exactly the same in C++ except that struct defaults to public and class defaults to private. Unions can also do most of the same things as classes and structs.
#include <iostream>
usingnamespace std;
///***************************************************************************************************
short level;
int strenght;
int dextery;
int vitality;
int mana;
int damage1;
int damage2;
int att;
int block;
int defence;
int life;
int manapoints;
int weaponblock;
int magicblock;
///***************************************************************************************************
void Barbarian()
{
level=1;
strenght=25;
dextery=10;
vitality=20;
mana=15;
damage1=3*strenght;
damage2=3*(dextery);
att=10*dextery;
block=dextery-level;
defence=9*dextery;
life=60+(3*vitality);
manapoints=2*mana;
}
int main()
{
Barbarian();
cout<<level<<endl<<strenght<<endl<<dextery<<endl<<vitality<<endl<<mana<<endl<<damage1<<endl<<damage2<<endl<<att<<endl<<weaponblock<<endl;
cout<<defence<<endl<<life<<endl<<manapoints;
cin.get();}
why didnt ye tell me that before??Cuz it took me a while to figure thhat out.
and i had to say that :P
i will only have one character per game sesion.
shoul i make mobs in the same way just with different variables??
and then just set the variables i used to nothing after the mob is defeated??
Whats going on with these structure and class things btw?I tried to use them but it said that you cant assign variables to numbers.Why is that??
struct character
{
short level;
int strenght;
int dextery;
int vitality;
int mana;
int damage;
int att;
int block;
int defence;
int life;
int manapoints;
}
int main()
{
character Barbarian; //create a struct called "Barbarian"
//access variables with dot notation ( . )
cout<<"your level is"<< Barbarian.level;
cin.get();
}
You'll need to set all the values before use: Barbarian.level = 1;
Barbarian.level = 1; would be within main, or where ever you create the object.
Here's code, using a struct and using a function to get the values for it:
#include <iostream> // cout
#include <conio.h> // _kbhit()
struct character
{
short level;
int strenght;
int dextery;
int vitality;
int mana;
int damage;
int att;
int block;
int defence;
int life;
int manapoints;
};
//pass the character struct by reference
//so that the value set are the same in the function that called it,
//in this case, main()
void createCharacter( character &c, constint i )
{
switch( i )
{
//player
case 1:
c.level = 20;
/* all other stats */
break;
//enemy
case 2:
/* stats for enemies */
break;
}
}
int main()
{
character Barbarian; //create a struct called "Barbarian"
//get values
//if you were creating an enemy, you would pass
// 2 instead of 1 to the function
createCharacter( Barbarian, 1 );
//access variables with dot notation ( . )
std::cout << "your level is " << Barbarian.level;
//press any key to exit...
std::cout << "\n\nPress any key to exit...";
while( ! _kbhit() )
{ /* do nothing - wait for key press */ }
return 0;
}