c#: creating a dynamic textbased inventory for pnc adventure

Started by
3 comments, last by laztrezort 12 years ago
Hi!

been lurking on these boards for a while now and finally have my own question.

I recently started coding in c# and am making a mix of a text and graphic point and click best compared to japanese adventure games, where all interaction happens in the textbox at the bottom of the screen and the top part visualises parts of the text and who you are talking to.

I have the movement/walking and interaction parts in place, but have been having a very hard time in finding a good way to tackle inventories.

The setup in a nutshell:

spread out in the room are boxes with items in them. when you are close to one you can see the items it contains (max. 5) in a textual list at the bottom of the screen.
the items can be taken out (added to player inventory) and dropped into any of these boxes.

I've created this by using an ArrayList for the box you are close to and the player inventory.
I want it to load up the items based on their location(in which box they are). The player inventory funtions the same as a box too (except for the limitation of 5 items).

Now, besides picking up and dropping, each object has a number of other possible interactions: look, break, open, give, sell
some interactions are possible on one item, but not the other. (ex. you can break a glass, you can't open it. // you cannot break a safe, but you can open it).
So each item will have one or multiple of these actions available. Like the safe example, some objects will also be boxes for other items to be in.

When you select one of these items. I would like to show only the actions that are available to that item.

So what i need to so is save information about each item in the game (about 200).
name
location (which box it is in)
available actions. (look at, pick up)
result of these actions. (ex. giving a photo to a man causes him to cry and tell a story about his lost wife.)

I've come so far that I will need a seperate list for the location and for the other parts of the items, because the location will be dynamic/changing during the game, but the other things won't change.

Do you guys have any tips/ideas how I should get a system for this in place?
I'm not super experienced, but motivated enough to put a lot of time/effort/research into making it work.

Thanks so much!!
Advertisement
I think a first approach would be to create an object class with generic methods for the possible actions like break, open, etc.

Then you can create subclasses for the different type of objects and determine for all these subclasses whether items are breakable or not.

Finally you can create the actual different object (like safe1, safe2, etc.) and give them their place in the game world.

Is that what you are looking for? Or am i thinking completely in the wrong direction?

My personal blog on game development!

Black Wolf Game Development

Typically, here is how an item system is built:

First you have an Item class, and instance each object that will appear in the game, each of which contains all of the default (starting) data for an object of that type. Keep a collection of these Item objects somewhere, think of this as a minimal "database" of sorts.

If your game item data will never change during the course of play, then this is basically all you need. In your containers, you just keep a list of indexes corresponding to the item in the database (or keys if you are using a dictionary, or just a reference if you are using something like a HashSet).

If your game item data can change during the course of play, which is what your case sounds like, then the database can be considered a collection of item blueprints. Instance the item by copying from the database, and just keep the reference to it in the container.

For the Item class itself, it sounds like you already have a pretty good idea of what it will be. In the past, many games used inheritance as a way to define different items. Lately the trend seems to go more towards aggregation. It sounds like aggregation is more what you had in mind, and will probably be simpler, since inheritance tends to get messy pretty quickly.
Thanks for the support/thoughts. I'm happy you agree with my setup.

So how would I create a database that I could read off when creating these items?
I don't have any experience with creating these.
I've been looking into hashtables, dictionaries, arraylists, and have a good understanding of them, but have no idea which stracture would fit best.
Right now I'm leaning towards arraylists with this type of setup:
(http://w3mentor.com/learn/asp-dot-net-c-sharp/object-oriented-concepts-c-sharp/csharp-oops-examples/simple-inventory-example-class-interface/)

is it easier to hardcode the available actions per item in the database, ex:
(itemname, action#1, action#2, action #3, action #4, action #5)

or would i save each possible action as, possibly, a bool, in the instance of the object class, ex:
(openable = true
breakable = false
pick up = true)

or would i add each action as a method in the default item class and set them all to output "null". and if it isn't null, it is a possible action for that instanced item?


and then, besides the default (starting) data, each object has various statusses which are dynamic/can change during gameplay (and are things i need to save, when creating a savegame(that's not a question now, but I'm looking ahead a little), so for example:
-location (this changes a lot during a game)
-locked/unlocked status
and possibly others.

I believe it's smarter for these to be saved in a seperate database, or can everything be combined?

thanks so much!
Don't get intimidated by my (mis)use of the word "database", I use that term only as a concept. In this case, it would just be a List or something similar.

Here is a basic outline of the general idea. Here I am using inheritance, because it is perhaps easier to understand and shorter to write a quick example, but you don't have to. All the data for an item is stored in a single Item object, and more specific item types are sublassed and implement functions for performing actions on them.


abstract class Item
{
public int Location{get;set;}
}

class LightSource:Item
{
public int FuelAmount{get;set;}
public int Brightness{get;set;}

public void TurnOn() {...}
public void TurnOff() {...}
}

class Hat : ITem
{
public void PutOn() {...}
public void TakeOff() {...}
}
...
List<Item> Database; // here are all of the item blueprints, set this up at runtime or load defaults from
// a text/config file
....
class Container
{
List<Item> contents;
...
}


When you get to handling actions on items, you will need to check the item type and act appropriately. Again, this is based on the inheritance example above.


void TryTurnOnAction(Item theItem)
{
if(theItem is LightSource)
{
(theItem as LightSource).TurnOn();
}
else
{
// tell player that is an innappropriate action
...
}
}


You can also, as you suggested, keep bools for appropriate actions on the item class, instead of checking type and casting - the logic would nearly be the same.

This topic is closed to new replies.

Advertisement