Computer Science Canada

(RPG) Adding Items to the Inventory - Arrays

Author:  [Gandalf] [ Fri May 20, 2005 1:29 pm ]
Post subject:  (RPG) Adding Items to the Inventory - Arrays

Alright, what I'm trying to do is add items to an RPG game. The problem is, I can't find a way to increase the size of the array so that there is room to add another item later.

Here is what I have so far, but when a new item is added it just overwrites the last item in the array.

code:
var inventory : array 1 .. * of string := init ("Dirt", "Food")
var item : string
put "Items in your inventory."
loop
    for i : 1 .. upper(inventory)
        put i, ". ", inventory (i)
    end for
    put "Enter the new item to add"
    get item
    inventory (upper(inventory)) := item
    cls
end loop


Am I doing something wrong, or is there some point I still need to learn? Should I be using a dynamic array somehow instead? Any help would be appreciated, I'm getting confused...

*Edit* Wait, is it flexible arrays? I'll try looking it up, but some help would still be appreciated.

*Edit 2* Ohh, I was right. I finally got it - proud of myself Smile.

code:
var inv : flexible array 1 .. 2 of string
var item : string
var limit : int := 2
inv(1) := "Dirt"
inv(2) := "Apple"
put "Items in your inventory:"
loop
    for i : 1 .. upper(inv)
        put i, ". ", inv(i)
    end for
    put "Enter the new item to add."
    limit += 1
    get item
    new inv, limit
    inv (upper(inv)) := item
    cls
end loop


: