Problem creating an item in Text Game

Started by
15 comments, last by pulpfist 12 years, 6 months ago
Thanks pulpfist!

Now the only problem is that it says "class CHRCTR Player already defined", since I'm calling the Player.setHGBA() method both in the Func1 and Main functions (or at least I think that's why). I want to be able to change the HGBA variable at any point of the program, so is there a different way I should be doing this or is there some code I'm missing/doing wrong?

Advertisement
How and where is your definition of Player?

How and where is your definition of Player?

I create Player in Func1(), CHRCTR Player;

I then also call methods of Player in the Func1 Function, and in Main after Func1 is over.
If Player is defined locally in Func1 how can you access it from main?

Thanks pulpfist!

Now the only problem is that it says "class CHRCTR Player already defined", since I'm calling the Player.setHGBA() method both in the Func1 and Main functions (or at least I think that's why). I want to be able to change the HGBA variable at any point of the program, so is there a different way I should be doing this or is there some code I'm missing/doing wrong?


If you need access to Player in both main and Func1, I would suggest you create the player it in main and send it as a reference parameter to Func1

main.cpp


#include "Class.h"
#include "Func1.h"

int main()
{
CHRCTR Player;

Func1(Player);

Player.setHGBA(20);

...
}



Func1.h


...
#include "Class.h"

void Func1(CHRCTR& player);
...


Func1.cpp


#include "Func1.h"

void Func1(CHRCTR& player)
{
player.setHGBA(10); // start the player's amount at 10
cout << player.getHGBA() << endl; // test to see if I can see that the HGBA amount is 10
}


Something like that.
As you can see I have placed the Func1 prototype in a file Func1.h, and included that in main.
And the Func1 now takes a reference to a CHRCTR as parameter.
I have also left out a lot of code like the inclusion guards. I guess you can fix that yourself.


I'm not sure exactly how you want things but maybe that example can help a bit
Thanks a lot for the suggestions guys! I tried declaring it in main instead of Func1, and I tried passing Player as an argument. It still comes up with the same error "already defined".

Outside of the code, how would you guys go about doing this? On a high level, I mean. If you had an integer you need to be able to access inside every function in your entire program (ie. in an RPG, you need to be able to see your HP in battle and in your menu and adjust your HP, during battle and using items in your menu etc.), how would you go about implementing that in your program? Maybe I'm trying to do it in a wrong way and there's a better solution?

Thanks a lot for the suggestions guys! I tried declaring it in main instead of Func1, and I tried passing Player as an argument. It still comes up with the same error "already defined".

Outside of the code, how would you guys go about doing this? On a high level, I mean. If you had an integer you need to be able to access inside every function in your entire program (ie. in an RPG, you need to be able to see your HP in battle and in your menu and adjust your HP, during battle and using items in your menu etc.), how would you go about implementing that in your program? Maybe I'm trying to do it in a wrong way and there's a better solution?


It depends on a lot of things, like what kind of game, how big is the project, is it a one man or a team effort, what are the skills of the coders, etc.
Basically, different circumstances requires different strategies, and as always there is many ways to go about it.

Thats pretty much all I can say with my limited experience with large projects.

PS.
If you paste the whole error message its easier for us to give you tips about what could be wrong.

This topic is closed to new replies.

Advertisement