Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Game Inventory

kaskodekaskode Member Posts: 4
I've started making a small RPG (DirectX), and am having trouble making an inventory system for the game. I'm am intermediate at delphi programming. I've tried various things to making an inventory, but always failed. Any ideas on how I would go about making an inventory system?

(I've experimented with using lists and records to store all the items and the items data, and what the player is holding, but just can't seem to get it to work)
(I would like all the item data stored in an external file, and allow the player to be able to buy equiptment,ect)

Comments

  • zibadianzibadian Member Posts: 6,349
    : I've started making a small RPG (DirectX), and am having trouble making an inventory system for the game. I'm am intermediate at delphi programming. I've tried various things to making an inventory, but always failed. Any ideas on how I would go about making an inventory system?
    :
    : (I've experimented with using lists and records to store all the items and the items data, and what the player is holding, but just can't seem to get it to work)
    : (I would like all the item data stored in an external file, and allow the player to be able to buy equiptment,ect)
    :
    I've created an RPG system a while back, and I used the following method of inventories.
    First I created the master list of equipment for the game. At that time I used a linked-list, but now I would use a TList and records with. The data was stored in a typed file. Each item has a code, which uniquely identifies the item within the game (for example an integer). I advise you not to use the item's index in the list, since that might change during the game or in sequels. The item-code is purely for the internal mechanism of the game. You can even include in the code, if an item is unique or not.
    The inventory list for characters, treasure piles, etc. is then made up of an array (or list) of those numbers. If the player looks at his character's inventory (or at a treasure pile), you just look-up the item description from the master list.
    Buying items is very simple using this method, since it just means that you add the code to the character's inventory, and if necessary remove it from the shop's inventory. The same holds true for treasure piles, or selling items.

    This method isn't too difficult to code, and still provides you with a solid method of handling multiple inventories.
  • kaskodekaskode Member Posts: 4
    : I've created an RPG system a while back, and I used the following method of inventories.
    : First I created the master list of equipment for the game. At that time I used a linked-list, but now I would use a TList and records with. The data was stored in a typed file. Each item has a code, which uniquely identifies the item within the game (for example an integer). I advise you not to use the item's index in the list, since that might change during the game or in sequels. The item-code is purely for the internal mechanism of the game. You can even include in the code, if an item is unique or not.
    : The inventory list for characters, treasure piles, etc. is then made up of an array (or list) of those numbers. If the player looks at his character's inventory (or at a treasure pile), you just look-up the item description from the master list.
    : Buying items is very simple using this method, since it just means that you add the code to the character's inventory, and if necessary remove it from the shop's inventory. The same holds true for treasure piles, or selling items.
    :
    : This method isn't too difficult to code, and still provides you with a solid method of handling multiple inventories.
    :


    I understand what you are talking about, could you start me off with a little code?
  • zibadianzibadian Member Posts: 6,349
    : : I've created an RPG system a while back, and I used the following method of inventories.
    : : First I created the master list of equipment for the game. At that time I used a linked-list, but now I would use a TList and records with. The data was stored in a typed file. Each item has a code, which uniquely identifies the item within the game (for example an integer). I advise you not to use the item's index in the list, since that might change during the game or in sequels. The item-code is purely for the internal mechanism of the game. You can even include in the code, if an item is unique or not.
    : : The inventory list for characters, treasure piles, etc. is then made up of an array (or list) of those numbers. If the player looks at his character's inventory (or at a treasure pile), you just look-up the item description from the master list.
    : : Buying items is very simple using this method, since it just means that you add the code to the character's inventory, and if necessary remove it from the shop's inventory. The same holds true for treasure piles, or selling items.
    : :
    : : This method isn't too difficult to code, and still provides you with a solid method of handling multiple inventories.
    : :
    :
    :
    : I understand what you are talking about, could you start me off with a little code?
    :
    Sure:
    [code]
    type
    PEquipmentItem = ^TEquipmentItem;
    TEquipmentItem = record
    Code: integer;
    Name: string[20];
    Description: shortstring;
    Cost: integer;
    end;

    TEquipmentfile = file of TEquipment;

    TEquipmentList = class(TList)
    private
    function GetItem(index: integer): TEquipmentItem;
    function SetItem(index: integer; Value: TEquipmentItem);
    public
    property Items[index: integer]: TEquipmentItem read GetItems Write SetItems; default;
    // Override the default TList.Items property to make it a list of TEquipmentItem
    function Find(Code: integer): TEquipmentItem
    // A search routine for the item-codes.
    end;

    TCharacter = record
    Name: string;
    Equipment: array of integer; // list of equipment codes
    Money: integer;
    end;

    var
    EquipmentList: TEquipmentList;

    procedure BuyItem(Char: TCharacter; ItemCode: integer);
    begin
    with EquipmentList.Find(ItemCode) do
    begin
    if Cost < 0 then
    ShowMessage('not for sale!')
    else if Cost > Char.Money then
    ShowMessage('Not enough money!')
    else with Char do
    begin
    Money := Money - Cost; // Pay the cost
    SetLength(Equipment, Length(Equipment)+1); // Grow the inventory
    Equipment[High(Equipment)] := Code; // Add the Item to the inventory
    end;
    end;

    procedure ShowEquipment(Char: TCharacter);
    var
    i: integer;
    begin
    Form1.Memo1.Text := '';
    if Length(Char.Equipment) = 0 then
    Form1.Memo1.Lines.Add('No Equipment')
    else
    for i := 0 to High(Char.Equipment) do
    Form1.Memo1.Lines.Add(EquipmentList.Find(Char.Equipment[i]).Name);
    // loop through all equipment of the character and add the names of the items to the memo
    end;

    function TEquipmentList.GetItem(index: integer): TEquipmentItem;
    begin
    Result := PEquipmentItem(inherited Items[index])^;
    end;

    function TEquipmentList.SetItem(index: integer; Value: TEquipmentItem);
    begin
    inherited Items[index] := @Value;
    end;

    function TEquipmentList.Find(Code: integer): TEquipmentItem;
    var
    i: integer;
    begin
    i := Count-1;
    while i > -1 do
    begin
    Result := PEquipmentItem(inherited Items[index])^;
    if Result.Code = Code then
    Exit;
    dec(i);
    end;
    ShowMessage('Error: Item code not recognized');
    // Show Error
    end;
    [/code]
    I've created a few procedures, but not all. I also created a descendant of the TList, which automatically converts the untyped pointers into TEquipmentItem records. It can also find equipment based on the internal code of the equipment (Find() method).
    Buying equipment is quite simple, just find the correct code. Then find out if the item can be sold in the first place (many magical items in a fantasy setting can not be sold), and if the character has enough money. If both checks are passed, then the money is deducted from the character and he gains the equipment. If you remove the money part of this procedure, you have the procedure for adding equipment to the character.
    The ShowEquipment() procedure is an example of how to access the character's inventory. This should be easy for you to understand.

  • kaskodekaskode Member Posts: 4
    Thanx so much zibadian, exactly what i needed, already started adapting it to my project. I searched all over the net and couldn't find much about making an inventory for games.

    Thanx again.
  • drudgedrudge Member Posts: 6
    I'm having trouble using your code zibadian. could you explain it in more detial. Can you email me a working project sample in a .zip please?

    thanks

    trying desperetly to make an inventory for my game.
  • zibadianzibadian Member Posts: 6,349
    : I'm having trouble using your code zibadian. could you explain it in more detial. Can you email me a working project sample in a .zip please?
    :
    : thanks
    :
    : trying desperetly to make an inventory for my game.
    :
    I don't have and are going to make a working example, because I wrote that code from scratch based on the memory of a program I wrote about 6 years back in TP.
    As for the detailed explanation, let me know what you don't understand. Nearly all procedures and functions are quite easy to understand from the code itself.
  • drudgedrudge Member Posts: 6
    : I don't have and are going to make a working example, because I wrote that code from scratch based on the memory of a program I wrote about 6 years back in TP.
    : As for the detailed explanation, let me know what you don't understand. Nearly all procedures and functions are quite easy to understand from the code itself.
    :

    where do i put the data for the items.

    i also get an error here

    property Items[index: integer]: TEquipmentItem read GetItem Write SetItem; default;

    Incompatible types

    How do i fix this?

  • zibadianzibadian Member Posts: 6,349
    : : I don't have and are going to make a working example, because I wrote that code from scratch based on the memory of a program I wrote about 6 years back in TP.
    : : As for the detailed explanation, let me know what you don't understand. Nearly all procedures and functions are quite easy to understand from the code itself.
    : :
    :
    : where do i put the data for the items.
    :
    : i also get an error here
    :
    : property Items[index: integer]: TEquipmentItem read GetItem Write SetItem; default;
    :
    : Incompatible types
    :
    : How do i fix this?
    :
    :
    The incompatible types is easily fixed. Just change the following code
    [code]
    function SetItem(
    [/code]
    into
    [code]
    procedure SetItem(
    [/code]
    The data for the items is stored in the EquipmentList variable. This is a direct descendant oof the TList class, so I suggest that you first read the TList description in the help files. Here is a sample code to add an item to the list:
    [code]
    var
    Item: PEquipmentItem;
    begin
    New(Item)
    Item^.Code := 5;
    Item^.Name := 'hat';
    Item^.Cost := 1;
    EquipementList.Add(Item);
    end;
    [/code]
    Note: Remember to first Create the Equipmentlist, otherwise you'll get an access violation error.
Sign In or Register to comment.