• Post Reply Bookmark Topic Watch Topic
  • New Topic

One object, one instance variable, different values?  RSS feed

 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fellow Ranchers,

It might be a matter of seconds for you to answer the question that's been boggling me. But then, I'm new and there are still loads of things I haven't ever given a thought. First, let me explain the situation.

For the sake of easy explanation, let's pretend I'm working on an RPG. Like in all RPGs, there are items found all throughout the imaginary world. Each player and NPC can obtain an item. My question will concern those items.

In other words, I'd like to use instances of a class in multiple places of the code. Each instance will have its own, individual values of instance variables, with one obvious exception: itemQuantity should have a different value in playerInventory, npcInventory, etc. Also, I'd like a list of all items that can be found in the game. This list doesn't need itemQuantity at all.




The question is: should I really make itemQuantity an instance variable of the Item class? It seems as though for each copy of the Item class I should create a separate copy with different value of itemQuantity, but that's not very efficient. Where is the error in my logic?

What's important is that there may be plenty items in a game and a player may be given power to create new items during the course of the game.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing that come to my mind is that each Player/NPC "has-a" set of Items.

When you talk about quantity, I reckon you talking about inventory of that item AND NOT how many items the Player/NPC has.

In the latter case, the quantity is irrelevant.

Hence in your game logic, whenever a player/NPC creates/uses an item, that particular item's quantity should be modified (eg decrement) accordingly.

Now if you have players/NPC create an item "every time" the item is needed, then this sound awkward. If the quantity is indeed inventory, you are really working a pool of X items, therefore that quantity can be decremented when player/NPC uses it.

If this is the case, a singleton for X item, Y item should do the trick.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is: should I really make itemQuantity an instance variable of the Item class?


Only if there aren't an infinite number of items available in the entire game. Do all the players have to vie for say 100 axes? Probably not.

But if a player can have two axes and three swords, then an item quantity should be mapped to each item of the set in the Player.

 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for quick replies.

K. Tsang wrote:If this is the case, a singleton for X item, Y item should do the trick.

I'll have to remember this. I might use it elsewhere.

Knute Snortum wrote:

This hits the spot, at least for now.

The thing is that I'd like the user to build the database of available items. Later the user will create collections that will each contain their choice of available items. One collection may contain 3 × item A and 1 × item B, while another collection may contain 2 × item A and 4 × item C.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!