Global Vars, Classes and Seperate Files

I've been learning C++ for a few weeks and over and over I've heard that global variables are bad, but as i try to learn classes and seperating things across files, i don't know what the alternative is. Below is a test chassis for a fighting game where (theoretically) the various potential enemies will come from classes.

It doesn't work right now because the orcTest() function can't see the mstats[] array. If i make mstats global, it still doesn't seem to load the array properly. Any help you can provide would be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//npc.h - declares NPC classes
#include <string>
#include <iostream>

using namespace std;

class Orc {
	
public:
	string orc;
	int mlvl;
	int mstats[5];

	Orc(string aOrc);	//constructor
	~Orc();		//destructor

	//methods
	void melee();
};	

//pchar.h - declares player character variables and functions
int php;		

//ArenaChassis.cpp - main program
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "npc.h"
#include "pchar.h"

using namespace std;

//int mstats[5];
int mhp;
int mobmelee1;

int d6();
void orcTest();
string aOrc;

int main(void)
{
	srand(unsigned int(time(0)));
	php = 100;	//player hit points
	orcTest();
	return 0;
}

void orcTest()
{
	Orc whelp("whelp");
	cout << "Orc " << aOrc << " summoned\n";
	cout << "Orc strength is " << mstats[0] << endl;
	cout << "Orc agility is " << mstats[1] << endl;
	cout << "Orc constitution is " << mstats[2] << endl;
	whelp.melee();
	php -= mobmelee1;
	cout << "The orc hits for " << mobmelee1 << endl;
	cout << "The player is reduced to " << php << endl;
}

int d6()
{
	return rand() % 6 + 1;		//standard D6 roll
}

Orc::Orc(string aOrc)
{
	orc = aOrc;
	mlvl = (d6() + 1)/2;
	mstats[5] = {14, 14, 14, 14, 14};
	mhp = 100;			//basic mob hp
}

Orc::~Orc() {}

void Orc::melee()
{
	mobmelee1 = d6();
}
I think you're missing quite a few things. I suspect the stuff that you're representing as global variables are mostly properties of the player. php, for example, is an attribute of the current player. What if you want to add multiple players to your game?

I also think you're missing a game object that maintains the game world and all the stuff in it. For example, where is the collection(s) of Orcs held?

It's more a matter of object design, that's really where this should be resolved.

Back to your immediate problem. Surely you can test your Orc class without worrying about the rest of the program.
Last edited on
Thanks for the feedback.

As far as multiple players and orcs, that's beyond my ambitions right now. Really all i'm trying to do is initialize one orc and list his stats on the screen and execute his one function.

The global variables are tied to the orc... mhp = monster hit points, mobmelee1 is his strike damage and aOrc is a single instance of Orc.
Last edited on
Just to be clear, are mhp and mobmelee1 used to initialise each instance Orc?
you made me think about mobmelee1 and i realized that by turning void melee() into int melee() i could just return a value and use the return rather than declaring a separate variable and storing it. one problem down! thanks.

Now on the rest, each instance of orc would need it's own mhp and mstats. Here is the updated code to the main file..

Issues are:
-still won't load the array
-mhp still has to be global

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//ArenaChassis.cpp - main program
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "npc.h"
#include "pchar.h"

using namespace std;

//int mstats[5];
int mhp;

int d6();
void orcTest();
string aOrc;

int main(void)
{
	srand(unsigned int(time(0)));
	php = 100;	//player hit points
	orcTest();
	return 0;
}

void orcTest()
{
	Orc whelp("whelp");
	cout << "Orc " << aOrc << " summoned\n";
	cout << "Orc strength is " << whelp.mstats[0] << endl;
	cout << "Orc agility is " << whelp.mstats[1] << endl;
	cout << "Orc constitution is " << whelp.mstats[2] << endl;
	whelp.melee();
	php -= whelp.melee();
	cout << "The orc hits for " << whelp.melee() << endl;
	cout << "The player is reduced to " << php << endl;
}

int d6()
{
	return rand() % 6 + 1;		//standard D6 roll
}

Orc::Orc(string aOrc)
{
	orc = aOrc;
	mlvl = (d6() + 1)/2;
	int mstats[5] = {14, 14, 14, 14, 14};
	mhp = 100;			//basic mob hp
}

Orc::~Orc() {}

int Orc::melee()
{
	return d6();
}
Last edited on
Pre C++11, initialisation is inconsistent. This hard codes the array size, just to show how it works.
1
2
3
4
5
6
7
8
Orc::Orc(string aOrc)
{
	orc = aOrc;
	mlvl = (d6() + 1)/2;
	for (size_t i = 0; i != 5; ++i)
		mstats[i] = 14;
	mhp = 100;			//basic mob hp
}
This is a variant that works with any array size.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T, size_t N>
inline size_t ArraySize(const T (&array)[N])
{
	return N;
}

Orc::Orc(string aOrc)
{
	orc = aOrc;
	mlvl = (d6() + 1)/2;
	for (size_t i = 0; i != ArraySize(mstats); ++i)
		mstats[i] = 14;
	mhp = 100;			//basic mob hp
}

Thanks for the suggestions. I've found that if i attempt to use the array inside the constructor, the variables are being set correctly so i am just having a problem understanding scrope. At least now i know what to research.

Topic archived. No new replies allowed.