7 Replies - 373 Views - Last Post: 17 February 2013 - 03:27 PM Rate Topic: -----

#1 hnefatl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-February 13

"Item" system in a game

Posted 16 February 2013 - 02:43 PM

Working on a medium-size console game (yup, console as in DOS, as opposed to Xbox, Playstation :D), I'm struggling to think of the best way to make an item system. I made one implementation, which worked, but when I came to doing some more complex stuff with it, the method broke.

What I was using was a class "Item", and then I made static variables for each Item. For example:
// Basic implementation of Item class
public class Item
{
    public string Name { public get; protected set; }

    public Item(string Name)
    {
        this.Name = Name;
    }
}

// In "ItemList.cs"
public static class ItemList
{
    public static Item ScrapMetal
    {
        get
        {
            return new Item("Scrap Metal");
        }
    }
}



You are probably asking why I returned a new Item, instead of just using one. When I came to writing up Weapons, which I treat as Items (inheritance), I found that due to references, everything in the game had exactly the same stats; everyone died when I only hit one guy, for example.

So, my question is: What is a good way to make a weapon/item/defence system? Of course, I'm not asking for the best way, as there will likely be many methods which are all useful in different situations. What I need is a system whereby I can basically say "use weapon X", and a new, non-referenced weapon is used. However, I also need to have, in effect, a list of all possible items; so as well as having the above way of accessing them, I can loop through them all.

I appreciate this post may be a bit confusing; I can't quite phrase this request as I can think it, so please reply with any and all questions that will probably arise after reading.

Hopefully,
Hnefatl

Is This A Good Question/Topic? 0
  • +

Replies To: "Item" system in a game

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: "Item" system in a game

Posted 16 February 2013 - 02:49 PM

static is a real problem. You realize that static means "this item shared across all other instances of this class", right?

You seem on the right track. -ish.

I think you just need to get in front of a white board and figure out the inheritance.

Item
Weapon : Item
BladedWeapon : Weapon
Sword : BladedWeapon

Wealth : Item
Currency : Wealth
Jewel : Wealth


And so on.

If you're not that comfortable with classes, there is a tutorial linked in my signature block
Was This Post Helpful? 1
  • +
  • -

#3 hnefatl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-February 13

Re: "Item" system in a game

Posted 16 February 2013 - 03:03 PM

Yeah, I thought that by using static I could just access the Items by using "ItemList.ScrapMetal", for example. With my original implementation, this worked a treat, but now I need to keep a record of every type of Item available it breaks somewhat. I would never create an object of class ItemList, as it is merely a "holder" for all my types of item.

I'm just not really sure on "proper practice" for this type of situation; I am fairly confident with classes, but I'm not sure how to get them into a system that works in this case :)/>

The whole idea behind me using static was that I would only need to define the data in the items once, then just reuse the object. It would make sure there was only ever one "type" of the object; every object that was meant to be a "BaseLaser" for example, would have exactly the same stats. I'm unsure whether to do this though:
// File 1
public class Item
{
    public string Name { get; protected set; }

    public Item(string Name)
    {
        this.Name = Name;
    }
}

// File 2
public class BaseLaser : Item
{
    public BaseLaser
        : base("Base Laser")
    {
         
    }
}



It seemed a bit bizarre; but maybe it is the correct way after all?

Thanks for the reply though!
Hnefatl
Was This Post Helpful? 0
  • +
  • -

#4 hnefatl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-February 13

Re: "Item" system in a game

Posted 16 February 2013 - 03:27 PM

Decided it might be easier to explain if I put it in context:

I'm working on a small space-themed game. I'm working on a trading system (specifically merchant stock) when I realize my current system just won't cut it.

I figure I have two choices:
// Choice 1
// A series of if statements

Random Rand=new Random();
int RandNum=Rand.Next(0, 2);
if(RandNum==0)
{
    // Add an Item to stock
}
else if (RandNum==1)
{
    // Ad a different Item to stock
}



That is clearly quite unwieldy; if I add more Items, I need to change the generator bounds, and add if statements.

// Option 2
// A list of possible Item
Random Rand=new Random();
Stock.Add(PossibleItems[Rand.Next(0, PossibleItems.Count)]);



Obviously far neater and cleaner; except I do not know how to load a sort of "Template" for each Item into a List<>.

Hopefully this makes it easier to understand?

Regards,
Hnefatl
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: "Item" system in a game

Posted 16 February 2013 - 03:45 PM

Quote

It would make sure there was only ever one "type" of the object

Very limiting. So someone can't carry two different keys, or two bags of gold?

Quote

every object that was meant to be a "BaseLaser" for example, would have exactly the same stats.

Again very limiting. If a base had 4 lasers to defend itself with (one in each corner) they couldn't receive a unique amount of damage or have a unique amount of ammunition.

If everything is static, and only one of each, and and and... Then it really isn't very Object Oriented is it?

I think you're going about this backwards. You're trying to force a game to fit the limited amount of coding you know. Instead of designing a good game concept, then pushing against your limits to learn more so you can create the game.

This post has been edited by tlhIn`toq: 16 February 2013 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 hnefatl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-February 13

Re: "Item" system in a game

Posted 17 February 2013 - 12:21 AM

Thanks again for your reply;

I appreciate that by using static variables, it would cause all objects to have the same data. I apologize, for I wasn't very clear when explaining it: each static variable, instead of returning itself, returns a new Item, with preset stats. This ensures that each item doesn't end up with referencing problems, and I can simply refer to a "template" (BaseLaser) and get a new object with "BaseLaser" statistics. I only need to define those statistics once, meaning I won't make a mistake typing them out elsewhere, and needn't remember all the statistics for all the Weapons in the game. The variables of an Item are instance ones; It's only the way I access the Items that is static.

I appreciate your continued help,
Hnefatl
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3629
  • View blog
  • Posts: 11,320
  • Joined: 05-May 12

Re: "Item" system in a game

Posted 17 February 2013 - 02:48 PM

It looks like you are looking to applying the GOF's Prototype pattern.
http://en.wikipedia....ototype_pattern
Was This Post Helpful? 0
  • +
  • -

#8 hnefatl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-February 13

Re: "Item" system in a game

Posted 17 February 2013 - 03:27 PM

Thank you everyone who replied; tlhIn`toq and Skydiver.

I have played around a bit and ended up using a load of inherited classes; for example:
class Item
class Weapon : Item
class WeaponPhaser : Weapon
class WeaponMissile : Weapon


This has, with a bit of tweaking, got the desired effect. It wasn't as neat as I had hoped, but I'm probably not good enough to make it beautiful yet :)/>

Skydiver: The prototype system looks similar to my first implementation; static member that returns a new "preset" object. I read the article and have logged it away :)/>

Thanks again,
Hnefatl
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1