Jump to content
  • Advertisement
Sign in to follow this  
Terra_Nova

Struggling with an items database

This topic is 2451 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

I'm attempting to do an items database but Im stuck. Tbh I think im looking at this the wrong way, but I'm totally unsure how to proceed.

Basic structure:

#include <iostream>
#include <vector>

using namespace std;

class A
{
public:
int x;
};

class B : public A
{
public:
int y;
};

class Manager
{
public:
vector<A*>listofstuff;

void AddThing(A* thing)
{
listofstuff.push_back(thing);
}
};


void main()
{
Manager manager;

B* object1 = new B();
object1->y = 1;

manager.AddThing(object1);

cout <<manager.listofstuff.at(0)->y << endl; // ---------> Does not work

system("PAUSE");


}


I know I'm thinking about this the wrong way. I'm trying to access a base class which just cant see the elements of the child classes. But how would I do this for an items database.

If i had:

vector<CWeapon> weapons;

class Weapon
{
int attack;
}

with 2 child classes

class MeleeWeapon:public Weapon
{
}

class RangedWeapon:public Weapon
{
int ammo;
}

How can I store a gun with ammo?

Share this post


Link to post
Share on other sites
Advertisement
You query the Weapon of its capabilities through its interface:

class Weapon {
public:
virtual ~Weapon();
virtual bool usesAmmo() const = 0;
virtual int countAmmo() const {
assert( false && "countAmmo() called on a weapon that does not have ammo??" );
return numeric_limits<int>::max();
}
};

class MeleeWeapon : public Weapon {
bool usesAmmo() const { return false;}
};

class RangedWeapon : public Weapon {
public:
bool usesAmmo() const { return true; }
int countAmmo() const { return ammo; }

private:
int ammo;
};

int main() {
RangedWeapon crossbow;
MeleeWeapon sword;
Weapon* items[] = { &crossbow, &sword };

Weapon& selected = *items[rand()&2];

cout << selected.usesAmmo() << endl;
if ( selected.usesAmmo() )
cout << selected.countAmmo() << '\n';
}

</int>

Share this post


Link to post
Share on other sites
oh, I was trying not to have ammo declared in weapons since not all weapons have ammo.

I guess having it like this is easier, makes sense to me. Maybe my brain is just a bit fried today, been programming for about 12 hours. Cheers for the help! :)

Share this post


Link to post
Share on other sites
Despite both being weapons, if the behaviour is sufficiently different think hard before trying to shoehorn them into the same container.

If you end up promoting so much of the interface to the parent, then you haven't gained much at all. At this point, you might consider making a single Weapon class, and making Melee weapons acting like a ranged weapon but having a short range and infinite ammunition (that is, turning the behavioural differences into data). This might be preferable to implementing the object specific logic in the class that uses the object.

Share this post


Link to post
Share on other sites
Melee and ranged weapons are not only unlikely to need separate classes, as rip-off suggests: they should also be easy to segregate into different containers if there is a reason, since you can query the weapon instances when they are created and place them in the appropriate "index" container.
Then you can, for example, iterate through all ranged weapons and shoot them early in the turn before iterating through all melee weapons to attack.

Share this post


Link to post
Share on other sites
I would remove this class and replace it with standard EntityItem class, which describes every possible object. It can be done by setting proper flags or string:var/prop system which is sufficient to describe almost all properties. Later you will obtain one class with no conflicts, it'll be easy for containers and with help of scripting language or callbacks you can achieve better results in less time. I give that point of view cause there were moments in my Cpp-life that multi-inheritance limited me hardly.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!