Jump to content
  • Advertisement
Sign in to follow this  
SonicD007

Global scope

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I use global scopes? I want to use functions from one class in another class. I am creating my Battle class, but it needs access to Player. Can I allow Battle class to use Player functions without deriving it? Scopes always confuse me. Can someone explain static, virtual, and Constructors/Destructors initialize variables and delete variables, correct? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
tatic, virtual, and Constructors/Destructors: these are all very different things, and I don't think you want any of them in this case.

I think what you really want to do is pass in your player object as a parameter to a method of your Battle class:

void Battle::doStuff(Player ply)
{
ply.funcInsidePlayer();
}


If not we can talk more about static methods (which is most likely what you would need).

Matt

Share this post


Link to post
Share on other sites
Yea, those things aren't import at the moment. They were just a few things that were annoying me because I don't know what they are used for or how they are used.

I just tried what you said, and yes that is what I want, but it gives errors.

Here are the errors:

battle.cpp:18: error: variable or field `LevelUp' declared void
battle.cpp:18: error: `int cBattle::LevelUp' is not a static member of `class cBattle'
battle.cpp:18: error: `cPlayer' was not declared in this scope
battle.cpp:19: error: expected `,' or `;' before '{' token

and heres the code for the player.h and the battle class

Player.h
#ifndef CPLAYER_CLASS
#define CPLAYER_CLASS
//--------------------------------------------------
// Include Files
//--------------------------------------------------
#include <windows.h>
#include <string>

//--------------------------------------------------
// Player.h class
//--------------------------------------------------


class cPlayer
{
private:

std::string P_Name;

int P_Powerlevel;
int P_MaxPowerlevel;
int P_Attack;
int P_MaxAttack;
int P_Defense;
int P_MaxDefense;
int P_Ki;
int P_MaxKi;
int P_Level;
int P_Experience;
int P_MaxExperience;
int P_Zenni;

public:

std::string GetName();

int GetPowerlevel();
int GetMaxPowerlevel();
int GetAttack();
int GetMaxAttack();
int GetDefense();
int GetMaxDefense();
int GetKi();
int GetMaxKi();
int GetLevel();
int GetExperience();
int GetMaxExperience();
int GetZenni();

void SetName(std::string newname);
void SetPowerlevel(int newpowerlvl);
void SetMaxPowerlevel(int newmaxpowerlvl);
void SetAttack(int newattack);
void SetMaxAttack(int newmaxattack);
void SetDefense(int newdefense);
void SetMaxDefense(int newmaxdefense);
void SetKi(int newki);
void SetMaxKi(int newmaxki);
void SetLevel(int newlevel);
void SetExperience(int newexperience);
void SetMaxExperience(int newmaxexperience);
void SetZenni(int newzenni);

void ChangePowerlevel(int amount);
void ChangeMaxPowerlevel(int amount);
void ChangeAttack(int amount);
void ChangeMaxAttack(int amount);
void ChangeDefense(int amount);
void ChangeMaxDefense(int amount);
void ChangeKi(int amount);
void ChangeMaxKi(int amount);
void ChangeLevel(int amount);
void ChangeExperience(int amount);
void ChangeMaxExperience(int amount);
void ChangeZenni(int amount);
//To lower values, use changew/e and just use a negative number
};
#endif



Battle.h

#include "battle.h"

cBattle::cBattle()
{
//ctor
}

cBattle::~cBattle()
{
//dtor
}

void cBattle::DeathCheck()
{

}

void cBattle::LevelUp(cPlayer player)
{
if (cPlayer.GetExperience >= cPlayer.GetMaxExperience)
cPlayer.SetLevel(cPlayer.GetLevel + 1);

}




Guess I do need to know about static members. :-

Share this post


Link to post
Share on other sites
Ignore everything. I fixed it. I forgot to include the class I wa susing, and I missed closing a bracket for the function. Thanks for your help.

Share this post


Link to post
Share on other sites
This function is broken. It doesn't do what you think it does.

void cBattle::LevelUp(cPlayer player)
{
if (cPlayer.GetExperience >= cPlayer.GetMaxExperience)
cPlayer.SetLevel(cPlayer.GetLevel + 1);
}
I'll give you a hint. This function has the same problem:

void SetToZero( int x )
{
x = 0;
}

Share this post


Link to post
Share on other sites
Huh? What's wrong with the SetToZero function you have there??? It looks fine to me. Your getting an integer which you then set to 0. How is it wrong? Unless, your using the wrong variable? Actually yea. int blah SetToZero(blah) it would make blah go into x but not change the value of blah o.O Am I right?

For my function though, can you help me out a little more because I've never done anything like this (cPlayer player) I want to make the function able to take any object made from the player class and be able to change all of it's statistics.

Hope you understood all that lol. Thanks.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the problem with setZero is that you are passing the variable by value.
so:


void SetToZero( int x ) {
x = 0;
}



will create a new variable named 'x' that is only used within the function that has the same value as whatever you pass to it.

so someobject.SetToZero(myCoolVariable);
would do exactly the same as someobject.SetToZero(5);
(if myCoolVariable contains the value 5)

in order to allow SetToZero to modify the variable you pass to it you need to pass the address of the variable,

either by using

void SetToZero(int *x) {
(*x)=0;
}
somecrap.SetToZero(&somevariable)



or the better and easier option

void SetToZero (int &x) {
x=0;
}
somecrap.SetToZero(somevariable);

Share this post


Link to post
Share on other sites
Ohhh. I haven't used this before '&'. I knew it had to do with addresses, but didn't know much about. Pointers are another thing that confuse me. I think this should work.


void cBattle::LevelUp(cPlayer &player)
{
if (player.GetExperience() >= player.GetMaxExperience())
player.ChangeLevel(1);
player.ChangeMaxAttack(2);
player.ChangeMaxDefense(2);
player.ChangeMaxExperience(200);
player.ChangeMaxKi(5);
player.ChangeMaxPowerlevel(10);
player.ChangeZenni(3);
}



Where into the function, all those player.blah are now w/e player has the address of. So now it can work on say Chicken, Dog, Cat and still not bother the other animals so to speak?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by SonicD007
Ohhh. I haven't used this before '&'. I knew it had to do with addresses, but didn't know much about. Pointers are another thing that confuse me. I think this should work.

*** Source Snippet Removed ***

Where into the function, all those player.blah are now w/e player has the address of. So now it can work on say Chicken, Dog, Cat and still not bother the other animals so to speak?


yup pretty much, (if Chicken, Dog, Cat, etc are players that is)

basically a pointer contains a memory address

so
int a = 5;
int *b = &a;

would create something like this. (crappy ascii picture)
--- ---
|b| ----> |a|
|*| |5|
--- ---

a is a variable with the value 5,
b is a pointer that "points" at a. (b:s value is the address of a, or &a)

given an address you can access the value stored by using a dereference.
(*b) is 5.

Share this post


Link to post
Share on other sites
Quote:
Original post by SonicD007
So now it can work on say Chicken, Dog, Cat and still not bother the other animals so to speak?

No, presuming that by referring to Chicken, Dog and Cat you are referencing the common (and stupid) inheritance examples of creating a base class named Animal and derived classes as previously mentioned.

A reference, as created using the address-of operator (&) in a parameter list, contains the address of an object of that type, and of that type only. To enable polymorphism (the ability to treat an object of one type as another, related type), you must use pointers in C++.

The Anonymous Poster above conflates a pair of uses of the address-of operator:
  1. It can be used to obtain the address of an existing variable, as in the example:
    int a = 5;
    int * addr = &a; // addr contains the address of a
    *addr = 4; // we _dereference_ addr to access and modify the value
    cout << a << endl; // prints 4, because that's the new value of a


  2. It can be used to create a reference variable:
    int a = 5;
    int & b = a; // note the placement of the &, and the absence of *
    b = 4;
    cout << a << endl; // prints 4, as b is effectively an alias for a
    int c = 6;
    b = c; // has the effect of setting the value of a to the value of c
    cout << a << endl; // prints 6

The use of references is the second example, allowing you to pass an object to a function and obtain an alias that modifies the original object. C++ however, thanks to its C legacy, is a little confusing, so there's one more use of the & symbol that is completely unrelated to the above:
int a = 5;         // in binary, this is 00000101
int b = 12; // binary: 00001100
int c = a & b; // this performs a bitwise AND. result: 00000100
cout << c << endl; // prints 4

There's also &&, which is a logical AND, similar to the bitwise AND above but operating on boolean variables.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!