How do i use a variable outside a function

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.
Using the variable in another function? Or Classes?
Last edited on
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.
the only problem is that if i made a data structure i can't use inheritance, could i use friendship to make the pokemon have access to the attacks?

would you know how to create a turn based battle system? i know how to program but i dont have much experience so this is more of a resume project :D.
Last edited on
Is this going to be a console game?

I don't see how a structure would stop you from using inheritance. Moreover, it is quite likely that you don't need inheritance at all.

Here's a few ways you could structure your pokemons
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Pokemon{
   std::string name;
   int health;
   virtual void attack( Pokemon* target ) = 0;
};

struct Pikachu : Pokemon{
   Pikachu(){
      name = "Pikachu";
      health = 50;
   }
   virtual void attack( Pokemon* target ){
      std::string at;
      std::getline( std::cin, at );
      if( at == "electric shock" ) target->health -= 20;
   }
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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..
Topic archived. No new replies allowed.