Creating multiple occurences of the same struct

Hey guys! So, here goes the code at first.

char.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef CHAR_H
#define CHAR_H

struct Character
{
int hp;
int attk;
};

extern Character goon;

#endif 


char.cpp
1
2
3
4
5
#include "char.h"

//declaring goon again
//because of extern
Character goon = {50 , 25};


Now here is the problem. In main.cpp, I want the player to be able to face multiple goon enemies, without declaring multiple enemies like Character goon , goon1 , goon2 , goonN.
Moreover, the player is going to face a random number of enemies. Even an array thingy can't work, because then I would have to hardcode all encounters as int goonEncounter () indifinite times. And I don't want to do that. I have heard that class have their own functions as a part, but I'm still not much into them. Constructors and pointers are like hell. What I want, apparently, is to automate the encounter process, so that Character goon; can exist as indifinite separate independent instances. Say the PRNG gives out 10, so I want that there be 10 Character goon; as declared above, but with independent hp (attk is same to each goon), such that they are able to provide independent encounters, having me code int goonEncounter () only once, and not a separate function for each. You got the picture right? I don't know, or can't brainstorm, how to do that all. It would be very nice if you guys could point me to the right direction. Please take a note again that I am still struggling at pointers, so providing advice that involves using them wouldn't help me much. But ,if what I wish can't be achieved in my current skillset, I'm willing to invest time in learning about pointers and classes. Thanks in advance!
Last edited on
Ok,
Your problem is not clear at all. Are you trying to create a number of characters that cannot be known at compile time? Characters can be made at any time in the program by simply:
 
Character chara;

You're going to want to add a constructor to the Character class or at least a member initialization list.
Did any of this answer your question? If not please state it clearly.
Yup. I'm trying to create a number of characters not known at compile time. I essentially want them to appear on the fly. In my program, the combat system works like:
 
goon.hp = goon.hp - player.attk;

but this only works while there is only one goon in existance. Idk how many goons will be generated. So I am in confusion at this thing too (combined with creating a number of goons at runtime), about how to adapt the combat system to this.
So create an array of goons.

1
2
3
4
5
6
7
8
9
Character * goons;
int num_goons;

//  Populate the goons
num_goons = rand () % 50;   // or however many you want
goons = new Character[num_goons];

//  Figure out which goons the player should attack.
  goons[i]->hp -= player.attk;


Don't forget to the delete the goons so you don't have a memory leak.

Last edited on
What if I tell you that the player has to fight through all of them, goon after goon? Now what can I do then?
Iterate through all the goons.
> Please take a note again that I am still struggling at pointers, so providing advice that involves
> using them wouldn't help me much. But ,if what I wish can't be achieved in my current skillset,
> I'm willing to invest time in learning about pointers

Invest in some time learning about std::vector<>. Avoid arrays and pointers for now.
https://cal-linux.com/tutorials/vectors.html

Prefer using STL array or vector instead of a C array

Reason

C arrays are less safe, and have no advantages over array and vector. For a fixed-length array, use std::array, which does not degenerate to a pointer when passed to a function and does know its size. For a variable-length array, use std::vector, which additionally can change its size and handles memory allocation.
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rsl-vector


1
2
3
4
5
6
7
8
9
10
11
// the player is going to face a random number of enemies
std::size_t num_goons = some_randomly_determined_number_of_enemies ;
std::vector<Character> my_goons( num_goons ) ; // collection of num_goons goons

const int goon_attk = 25 ; // attk is same for each goon
for( auto& goon : my_goons ) // for each goon
{
    goon.attk = goon_attk ;
    // determine the value of hp for this goon
    goon.hp = different_hp_for_different_goons ;
}


And then:
1
2
3
4
5
6
7
8
int goonEncounter( player& player, std::vector<Character>& enemy_goons ) 
{
    for( auto& goon : enemy_goons ) // for each enemy goon
    {
        goon.hp = goon.hp - player.attk ;
        // ...
    }
}
Hey JLBorges! That seems to be great! Infact it went over my head till I invested some time in vectors. Now I understand. Thank you very much! Vectors, here I come again for another bout!
Topic archived. No new replies allowed.