Jump to content


Arrays and Inventories.

  • Please log in to reply
2 replies to this topic

#1 Excerpt


    GMC Member

  • New Member
  • 63 posts
  • Version:GM8

Posted 15 November 2011 - 07:06 AM

Hey, first post.

To business then. Probably a few of you guys know what a "roguelike" dungeon crawler is. Well, I'm trying to recreate the inventory system from one of those games. I think I've got a handle on the theory of it, I'm just having some issues with the execution.

Here's my method:

I have three types of arrays. First, I have the 'item' array. It's a global 2D array, and it sets the definitions, attributes, etc., of every item in the game. It's used when an item is first created, and it's also used to check whether or not the item's function is known by the player character.

Secondly, there's the 'instance' array, which is a local, 1D array that each physical instance of an item has, specifying how it differs from the stock 'item'. As an example;

The 'item' table specifies that item ID "1" 'looks' like a Pinewood Staff, is actually a Staff of Menace, is not known by the player and has a maximum of ten uses.
The 'instance' table specifies that this particular Pinewood Staff is cursed, has six uses remaining and has been tentatively labelled "Useless?" by the player. However, the player does not know that the staff is cursed, nor that it has six uses remaining.

Then there's the inventory array. It's a local, 2D array that details everything the character or an NPC/Enemy owns.

Now, what I need to happen (but am having a little trouble with executing) is this: no matter what, the 'instance' array needs to transition in and out of the 'inventory' arrays seamlessly, because item manipulation is going to be happening a lot... and making sure the individual aspects of each item don't change in this process is very necessary. Let's say I drop the item. I need it to create an instance of obj_item whose 'instance' array is identical to the inventory slot it came out of. Then let's say I pick it back up again. Considering the 1D instance array as a 'column', Is there a fast way of transcribing the 'instance' column onto a free 'inventory' column (without, of course, individually setting each slot) and vice versa?

Any advice would be appreciated. ^.^
  • 0
"This is probably a good place to write something really interesting."
~ Excerpt

#2 Noele


    GMC Mentor

  • GMC Member
  • 4252 posts
  • Version:GM:Studio

Posted 15 November 2011 - 11:52 AM

Considering you can only pick up items which exist in a room you probably do not need an inventory slot for every single item. Conventionally there is a limit to the number of items a player can physically carry, thus the inventory index generally refers to one of these slots.

When storing instance IDs keep in mind these IDs can change. When picking up an item you should be adding its object type to your inventory and destroying it on the playfield. Thus when putting it down you can create a new instance of its object type before removing it from the inventory. The item you drop will be the same object type but will have a different instance ID. Using an inventory item rather than dropping it decreases its use count which, upon reaching zero it can be removed.

A common method of being able to display the inventory items as their sprites is to create an inventory object, making it persistent so it is preserved in all rooms. Each instance can be assigned the appropriate sprite for its contents and an inventory index (or slot number) assigned together with anything else you need, like use count, description, if it can be combined with other items etc.

You will still need to use arrays (or data structures) but as these are used mostly as look-ups to assign the inventory items to the inventory object instances, they will require less manipulation. Using arrays alone will mean you will have to individually set each array element each time an item is altered.
  • 0

#3 Excerpt


    GMC Member

  • New Member
  • 63 posts
  • Version:GM8

Posted 15 November 2011 - 12:52 PM

Thanks for the help! However, I'm after something a little specific here.

Points of clarity.

- The "ID" I'm referring to here is a variable that I have defined myself, and it is the first entry in any item's array. It pretty much just specifies the usage of a given item and helps me to play around with arrays easier.
- The reason I have an 'inventory slot' for each existing item (well, in truth, I don't) is because while I only have, say, 70 items, each individual instance of that item has differing states, etc. For instance, one Infinity Herb may be cursed. Another may be blessed. They are the same item and have the same function (hence the variable 'ID') but they are not identical. For that reason, I can't just create a generic instance of an item, as that would destroy its unique properties.

My current issue lies in that area - preserving those unique properties. I think I'm coming closer to working it out though. In addition to my inventory array, I also have a global, 1-D 'backup' array.

> Drop item
- All of the properties in a given inventory slot are added to the backup array
- The inventory slot is cleared
- A new instance "obj_dropped_item" is created, and the backup array is used to fill in its properties table.

...aaaand I just learned a thing or two about loops. I think this should be enough to transpose an array onto another:

//"x" specifies the slot to be modified.
for (k=0; k<7; k+=1) {

Maybe. We'll see what happens. XD

Edited by Excerpt, 15 November 2011 - 01:12 PM.

  • 0
"This is probably a good place to write something really interesting."
~ Excerpt