Need to create Items for a game.

  • Thread starter Thread starter mb
  • Start date Start date
M

mb

what is the best way to do this:

In a game I want to a class called "Items". This class will have the game
items

public class Items
{
public int Chair
public int Table
. . .and so on . . .
}

Then I want each item in the Items class to have properties like X and Y for
location.

The reason I want it like this is so I can instantiate a Items type,
randomly pick one of it's items then place it on the board (or get its name,
color, or material etc.).

I thought C# would give me some advantages over VB6 when I just used a
multi-demensional array MyItems(20,20), where the first number is the
item, and the second number was its list of properties. I would make
constants like CHAIR, and NAME:

Example
MyItems(CHAIR, NAME) = "Red Chair"

When I saw C# and its ability to make objects, I though this would eliminate
this need. However, it seems that I am going to have to make a
multidemensional array again. Isn't there an easier way with C#?
 
mb said:
what is the best way to do this:

In a game I want to a class called "Items". This class will have the game
items

public class Items
{
public int Chair
public int Table
. . .and so on . . .
}

Then I want each item in the Items class to have properties like X and Y
for
location.

The reason I want it like this is so I can instantiate a Items type,
randomly pick one of it's items then place it on the board (or get its
name,
color, or material etc.).

I thought C# would give me some advantages over VB6 when I just used a
multi-demensional array MyItems(20,20), where the first number is the
item, and the second number was its list of properties. I would make
constants like CHAIR, and NAME:

Example
MyItems(CHAIR, NAME) = "Red Chair"


Whats wrong with defining, say,

public class Item
{
public string Name;
public string Location;
]

public class Items
{
public static readonly Item[] Items;
}

and then
Items.Items[0].Name = "Red Chair";

?


That is certainly the approach I would take, instead of using an array.
 
Hmmm... Don't be too upset if I'm way off here, but sounds sort of like you
want to create a gameboard with an Item object in each square?

How about a two-dimensional array of Items?

Items [] gameboard = new Items [20, 20];

Does that make sense for what you're doing? Then you populate each square
with a particular Item that you've instantiated and assigned values to
(i.e., Items item1 = new Items; item1.itemtype = chair; item1.color = red;
gameboard [10, 10] = item1; etc.)

Your itemtypes would be an enumeration in this instance. Or you could work
it by creating subclasses of the Items, which would make sense if they each
have varying properties.

Just a thought - the array of Items might be a little easier to program than
storing the X and Y in each item, especially since it's a small array...

Thanks,
Michael C., MCDBA
 
Daniel O'Connell said:
mb said:
what is the best way to do this:

In a game I want to a class called "Items". This class will have the game
items

public class Items
{
public int Chair
public int Table
. . .and so on . . .
}

Then I want each item in the Items class to have properties like X and Y
for
location.

The reason I want it like this is so I can instantiate a Items type,
randomly pick one of it's items then place it on the board (or get its
name,
color, or material etc.).

I thought C# would give me some advantages over VB6 when I just used a
multi-demensional array MyItems(20,20), where the first number is the
item, and the second number was its list of properties. I would make
constants like CHAIR, and NAME:

Example
MyItems(CHAIR, NAME) = "Red Chair"


Whats wrong with defining, say,

public class Item
{
public string Name;
public string Location;
]

public class Items
{
public static readonly Item[] Items;
}

and then
Items.Items[0].Name = "Red Chair";

?


That is certainly the approach I would take, instead of using an array.

I can't get this to work. It says I can't use the "Items" twice in:

public class Items
{
public static readonly Item[] Items;
}

1) Also, how and where do I instantiate this?

2) What I am trying to do is create a self contained .cs file that has the
definitions for the 24 items I will use (hopefully setup so I could easily
use this class file in another game).
Where would I define the items if I wanted it to be a self contained file
for portability?

3) One last question. Why does "public static readonly Item[] Items;" work?
Is this an example of a indexer? I am trying to understand indexers, and
haven't quite got a grasp on them.

THANKS!
 
I can't get this to work. It says I can't use the "Items" twice in:

public class Items
{
public static readonly Item[] Items;
}


Oops, I wasn't thinking. A class cannot have a member with the same name.

maybe add the static items field to the Item class.
1) Also, how and where do I instantiate this?
Since its readonly, you will need to instantiate it either directly
public static Item[] Items = new Item[20];

or in a static constructor.

2) What I am trying to do is create a self contained .cs file that has the
definitions for the 24 items I will use (hopefully setup so I could easily
use this class file in another game).
Where would I define the items if I wanted it to be a self contained file
for portability?

Personally, I would use a non-code file of some sort which your code loads
and generates items instead of writing them directly into source, but it
depends on what you want to do.
3) One last question. Why does "public static readonly Item[] Items;"
work?
Is this an example of a indexer? I am trying to understand indexers, and
haven't quite got a grasp on them.

Its an example of a readonly static field. You cannot have a static indexer,
but the Array class *does* have an indexer. So by writing Items[a] you are
accesing the indexer of the return value of the Items field.

Hope that makies sense, -_-
 
Back
Top