Public Group

# Making an in game shop?

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

## Recommended Posts

I am currently making a fishing game and I wanted to know what would the best approach to making a shop. In the store it will sell boats (to increase speed in the ocean), hooks (increase the number of fish that can be caught), rods (increase how far the hook can go), and buckets (increases the inventory space). Should I be making classes for each individual item? Or have a list of stats that change the starting items?

Thanks for any help!

##### Share on other sites

The more I learn about polymorphism, the more I want to use it. I definitely recommend making a class for each item. It will be much easier to manage and understand down the road, not to mention easier to expand when you want to add more items to your game. If I may ask, which language are you using?

Consider the following example (it does compile, if you want to test it yourself):

// Base class - Define all properties of boats here. Ex. topSpeed

class Boat
{
protected:
int topSpeed;

public:
};

// Derived classes - Types of boats in your shop - All you have to do is define stats here, if you like

class StarterBoat : public Boat
{
public:
StarterBoat(){ topSpeed = 15; }
};

class MegaBoat : public Boat
{
public:
MegaBoat(){ topSpeed = 30; }
};

int main()
{
// It works something like this.
StarterBoat oStarterBoat;
MegaBoat    oMegaBoat;
Boat       *oPlayerBoat = &oStarterBoat;

// When you want the top speed of the boat,
oPlayerBoat->Get_TopSpeed(); // returns 15

// When the player wants to upgrade,
oPlayerBoat = &oMegaBoat;

// When you want the top speed again,
oPlayerBoat->Get_TopSpeed(); // returns 30...pretty cool, huh?

// Close
return 0;
}



Edit:

This example uses C++

Edited by Onigiri Flash

##### Share on other sites

Thanks for the reply! Wow I haven't thought about polymorphism in a while, I think I'm going to need to refresh myself on it.

I use java, but I don't really understand your example that well. Are StarterBoat and MegaBoat two objects that extend the class boat?

##### Share on other sites

Thank you Onigiri Flash for the illustrations, they really helped! I like both ideas, but Onigiri's seems more practical in my case because controlling the boat might change. For example, I'm going to have a row boat that requires a constant press to keep moving and speed boat that can move by holding a button.

But I might do both if I'm going to have many boats in different subclasses.

##### Share on other sites

Thank you Onigiri Flash for the illustrations, they really helped! I like both ideas, but Onigiri's seems more practical in my case because controlling the boat might change. For example, I'm going to have a row boat that requires a constant press to keep moving and speed boat that can move by holding a button.

But I might do both if I'm going to have many boats in different subclasses.

While that can work if your game is small, as it grows it becomes much more difficult to control.

You've got Boat, StarterBoat, MegaBoat.  Seems reasonable from your view.

Let's say you build a FlyingBoat, and a TurboBoat.  Those would make good subclasses.

Then lets say you want a bigger cannon on a MegaBoat, so you make MegaBoatBigCannon, MegaBoatSmallCannon, MegaBoatMediumCannon.

Sooner or later you're going to want a combination of them.  A StarterTurboBoat.  or a FlyingMegaBoat.  Or a FlyingMegaBoatSmallCannon.

When you end up wanting to share different combinations of features between a different distribution of objects, suddenly the inheritance tree breaks down terribly.  You end up duplicating code between classes since you cannot easily pull from multiple base classes.  Everything breaks down, and you have some really hard days trying to make your system work.

Inheritance is something beginners tend to overuse when they discover it.  It's good that you've discovered it, but just be careful.  You've discovered how to use hammer, don't let everything become a nail.

It is far better to create an interface that will work for all boats, much as you described.  Then whenever you use the object, refer to it by the interface it implements rather than the concrete type.

.

Based on the information provided by the OP, it appears that these features are not currently part of the game design (fishing game).

The OP mentions that a row boat and a speed boat are part of the design. Polymorphism is a good solution for that, as well. You can certainly change behavior as well, not just the properties of a class. The reason why I suggested polymorphism is because the interface doesn't have to change.

.

class Boat
{
public:
void Move_Boat(){/* replace keyboard input handler with derived function */}
};

class RowBoat : public Boat
{
public:
void Move_Boat(){ /* track multiple key presses for movement here */ }
};

class SpeedBoat : public Boat
{
public:
void Move_Boat(){ /* track single key press for movement here*/ }
};

int main()
{
RowBoat   oRowBoat;
SpeedBoat oSpeedBoat;
Boat     *oPlayerBoat = &oRowBoat;

PlayerBoat->Move_Boat(); // automatically chooses the appropriate keyboard handler

// ...
}


##### Share on other sites
Everyone, I can appreciate a difference of opinion, but why am I being downvoted even though the original poster found my input helpful (and also stated this).

When you do that, you are telling new members of the community that it's not ok to share their experiences or attempt to help someone else on this forum. My posts were on topic and relevant to the needs of the original post. Please do not misuse the system.

There are many different ways to approach a problem, and we should encourage each other to provide multiple solutions. Let the OP decide which method he or she would like to use.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631790
• Total Posts
3002364
×