i have a few questions but my main one is how do i use a variable outside a function?
im making a pokemon game. every pokemon has attacks they learn as they level up, but i dont know how i would go about making the attacks. any help would be appreciated.
You can declare and initialize variables in global scope, but you can't do anything else. And why would you want to?
As for attacks, you could write a function for each attack (which would be a lot of work) and have your pokemons hold pointers to them. A better way would be to find a set of common parameters the attacks have (name, damage, type, etc.) put those in a structure and have every pokemon hold such structure. Then you'd call a single function deal_damage( damage_struct, target_pokemon ) what would calculate resistances and decrease target's health.
struct Attack{
int damage;
};
struct Pokemon{
std::string name;
int health;
std::map<std::string, Attack> skill_set;
void attack( Pokemon* target ) {
std::string at;
std::getline( std::cin, at );
//some std::map magic that would find the element with key == at, if there is one
//and store it in 'iterator', which I'm too lazy to write
target->health -= iterator->second.damage;
}
};
Pokemon CreatePikachu(){
Pokemon pika;
pika.name = "Pikahu";
pika.health = 50;
Attack a;
a.damage = 20;
pika.skill_set.insert( std::pair<std::string, Attack>("electric shock", a) );
return pika;
}
Even though the second example is longer, it would save you code when you add more pokemons and attacks.
As for turn based game, have a state variable. Then put a big switch in your main loop to deal with each state separately. There are many ways to structure this. You may find that you need two states (my_turn, enemy_turn) or a gazillion states (I_choose_pokemon, I_choose_attack, I_choose_target, I_am_attacking, ...) especially if the game is not in console. You may be fine without any states at all, but that would involve some nested loops and breaks. States are easier to deal with.
By the way, note that I have no idea how a pokemon game works..