Jump to content

  • Log In with Google      Sign In   
  • Create Account

c++, make child class know about parent class and be able to use its data


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1246

Like
0Likes
Like

Posted 19 August 2013 - 05:26 AM

Hello.

I am just programing the project i am working on.

The issue is that i seperated code like this:

In AI::ChaseUnit(...) i need to use Unit::unitDataID

I feel bad about passing it since it is one object?

Cant i do something like this->unitDataID from the AI::Chase(...) to access data from Unit:: ?

I can just pass the data to the function but as i said... i am looking for something other.

Hello.
 

//////////////////////// THE RAW CODE ////////////////////

//Unit.h

namespace en
{
enum UnitTypes
{
Player = 1,
Sheep = 2,
Wolf = 3,
};
};

struct unitData
{
std::string name;
en::UnitTypes type;
int hp;
int sightRange;
}

const unitDataT [] =
{
{"Player", en::Player, 999, 5},
{"Sheep", en::Sheep, 4, 4},
{"Wolf", en::Wolf, 6, 5},
};

class Unit : public AI
{
public:
Unit(Map & objMap, Textures & objTex, int unitType = en::UnitTypes::Sheep);
Unit(Map & objMap, Textures & objTex, int PosX, int PosY, en::UnitTypes unitType = en::UnitTypes::Sheep);

void LoopUnit();

int unitDataID;
int posX, posY;
sf::Sprite spr;
};

//AI.h
class AI
{
public:
AI(Map & ObjMap);

void DecideAction(int unitType);
void Execute(Unit & unit);

bool isDone;
private: //Functions
void Walk();
void Chase(UnitHolder objUniHol);
void Rest();
private:

int moveAmountX, moveAmountY;
int actionType;
int actTime;
sf::Clock clockLastActed;
//Clock how long // how long is the unit doing current action

Map & objMap;
};

Here is the raw code so sorry


Edited by BaneTrapper, 20 August 2013 - 04:26 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5435

Like
1Likes
Like

Posted 19 August 2013 - 05:30 AM

I can't see any code. Did you use [ code ] [ /code ] (without spaces) or is the gamedev editor bummed in the gob again?


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 BaneTrapper   Members   -  Reputation: 1246

Like
0Likes
Like

Posted 19 August 2013 - 05:32 AM

I can't see any code. Did you use [ code ] [ /code ] (without spaces) or is the gamedev editor bummed in the gob again?

Yea i am trying to get the code to post... 7th time fail i tried so much


Edited by BaneTrapper, 19 August 2013 - 05:34 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#4 Paradigm Shifter   Crossbones+   -  Reputation: 5435

Like
1Likes
Like

Posted 19 August 2013 - 05:41 AM

Are you trying to access members of class Unit (derived class) from AI::Chase (base class)? Because that is the opposite of what the title suggests ;)

 

You can make Chase virtual and override it in class Unit, then you know that the this pointer passed is a Unit* or more derived object.


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#5 BaneTrapper   Members   -  Reputation: 1246

Like
0Likes
Like

Posted 19 August 2013 - 05:59 AM

Are you trying to access members of class Unit (derived class) from AI::Chase (base class)? Because that is the opposite of what the title suggests ;)

 

 

Mhm, if in class A i include class B. A = base class or B = base class? i may have bad knowledge here.

You can make Chase virtual and override it in class Unit, then you know that the this pointer passed is a Unit* or more derived object.

The AI class is just container to be included in Unit class, i just want it to be separated for better visual overview.

But still then the AI code is gonna be in the Unit instead of "AI" i am trying to keep this at chunks as much as possible. Unit is for everything regarding Unit. But if there is AI, then everything from Unit that has AI should be placed in AI. At least that is what i was doing so far.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#6 Paradigm Shifter   Crossbones+   -  Reputation: 5435

Like
1Likes
Like

Posted 19 August 2013 - 06:03 AM

class Base

{

// stuff

};

 

class Derived : public Base

{

// stuff

};

 

Base is also known as a superclass/parent class and Derived as a subclass/child class.


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 BaneTrapper   Members   -  Reputation: 1246

Like
0Likes
Like

Posted 19 August 2013 - 10:04 AM

Thank you,


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#8 andy1984   Members   -  Reputation: 153

Like
0Likes
Like

Posted 19 August 2013 - 03:58 PM

...

 

public and protected members are accesible in derived classes. private members are not.

you used private vars in AI, so they are unaccesible in Unit.

 

inheritance:

class Unit
{
protected:
    // vars
   
    Action intendedAction;
   
    // constructor/desctuctor
 
    // protected methods  
 
    // virtual means you will override it in derived classes, = 0 means you wont implement it here
    // you could implement a default version here if you wanted
    virtual void decideAction() = 0;
    void performAction()
    {
        // do stuff
    }
 
public:
    void tick()
    {
        decideAction();
        performAction();
    }
 
    virtual void draw() = 0;
}
class Wolf : public Unit
{
protected:
    void decideAction()
    {
        Sheep* sheep = nearestSheep();
        if (distanceToUnit(nearestSheep) <= sightRange)
        {
            intendedAction.type = chase;
            intendedAction.target = nearestSheep;
        }
    }
public:
    void draw()
    {
        // draw a wolf
    }
};
class Sheep : public Unit
{
protected:
    void decideAction()
    {
        Wolf* wolf = nearestWolf();
        if (distanceToUnit(nearestWolf) <= sightRange)
        {
            intendedAction.type = flee;
            intendedAction.target = nearestWolf;
        }
    }
public:
    void draw()
    {
        // draw a sheep
    }
}
class Player : public Unit
{
    // etc
};
 
int main()
{
    Unit* units[3];
    units[0] = new Wolf(...);
    units[1] = new Player(...);
    units[2] = new Sheep(...);
 
    for (int i = 0; i < 3;i++)
    {
        units[i]->tick();
        units[i]->draw();
    }

    // etc

    for (int i = 0; i < 3;i++)
    {
        delete units[i];
    }
}

Edited by andy1984, 19 August 2013 - 04:23 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS