Inventory again

Blitz3D Forums/Blitz3D Programming/Inventory again

cash(Posted 2007) [#1]
I know there are some references in the forums and code archives but I need something pretty simple.

I needs 1 9 x 9 grid which when an item is selected gets added to or removed from the grid. This should free up the position and if possible realign the free spaces.

Each item needs a unique reference.

So

Pick up key
Key appears in grid (Inventory)
select key using cursor keys
Hit enter to use key
if okay to use remove key from grid and do action.

I am sure its been asked 100 times but I will only have a limited number of items per level and dont need anything too fancy.


D4NM4N(Posted 2007) [#2]
mabe something like this, it is using the mouse ATM but you could change it for keys with some imagination:

;Small "database" containing all possible game objects loaded using DATA or loaded from a file. Add fields as needed.
Type IG_ObjectData
	Field name$ ;item name
	Field thumbfilename$ ;filename of thumb 
        Field thumb   ; handle for thumbnail (if preloaded, this example doesnt use preloading))
	Field DeleteWhenUsed ;1/0
	Field Description$ ; ie, a gold shiny key with 666 on the handle
End Type

;A second "database" for your inventory.
Type IG_Inventory
	Field name$
	Field thumb ;image handle
	Field DeleteWhenUsed
	Field Description$
End Type

;function to add an object to your world, name must be unique as it is the "key field"
function addglobalobject(name$,filename$="missing.png",desc$="Mysterious Item",deletewhenused=0)
    O.ig_objectdata= new ig_objectdata
    o\name=name
    o\filename=filename
    ;if using preloading thumbs uncomment folowing line
    ;o\thumb=loadimage (o\filename)
    o\deletewhenused=deletewhenused
    o\description=desc
    
end function

;removes an object from the inventory
Function Inventory_remove(name$)
	;Erase item from inventory
	For i.IG_inventory=Each IG_inventory
                Freeimage i\thumb
		If Upper(i\name)=Upper(name$) Delete i
	Next
End Function

;Adds an object to the inventory from the world. Returns 1 if successful
;name must exactly match a name in your objectdata database.
Function Inventory_Pickup(name$)
	;check If Object is in database & If Not 
	;raise an error otherwise add To inventory
	success=0
	For o.IG_objectdata=Each IG_objectdata
		If Upper(o\name)=Upper(name$)
			;create New inventory item from item database
			i.IG_Inventory=New IG_inventory
			i\name=o\name
                        ;load thumbnail, if using preloading, change this to: i\thumb = copyimage (o\thumb)
			i\thumb=LoadImage(o\thumbfilename$)
			If Not i\thumb Then runtimeerror("Thumbnail Missing! "+o\thumbfilename,1)
			i\deletewhenused=o\deletewhenused
			i\description=o\description
			success=1
		EndIf
	Next
	If Not success Then debuglog("WARNING: Object "+name+" Is not in main database!",0)
End Function


;-------------the following may not work as i just made it up on the fly and may need tweaking (but you can see the idea)------ 
;to display and 'do' the inventory, returns the selected object name 
function doinventory$()
    picX=100 ; start position of grid
    PicY=100 
    PicSize=32+1 ; image size + any 'border' it needs
    columnmax=9  ; number of columns before going to next line. (could be re-aranged to work on rows)

    StepX=0
    StepY=0
    repeat
         ;To load eye candy for inventory screen and clear the backbuffer.
         ;if inventory screen used change following line to: drawimage inventoryscreen,0,0 
         cls ;or renderworld if to show 3d in background.
         selecteditem$=""
         for item.ig_inventory=each ig_inventory
             item\selected=0
             px=picX+(stepx*32)
             py=picY+(stepY*32)
             drawimage item\thumb, px,py
              if mouseX()>px and mousex()<px+PicSize and mouseY()>pY and mouseY()<py+PicSize 
                 selecteditem$=item\name
                 gx=px
                 gy=py
             endif
             stepx=stepx+1:
             if stepX=columnmax 
                  stepX=0:
                  stepY=StepY+1
             endif
         next

        drawimage glow,gx,gy ;you need a glow image for the selected item :)
        flip; 
        vwait()
        if mousehit(1) return selecteditem$
        if mousehit(2) or keykit(1) return "" 
    forever 

end function



;to use an object
function useobject(name$)
	;returns a 1 if successful (player has item)
	For i.IG_inventory=Each IG_inventory
                if Upper(i\name)=Upper(name$)
                     if i\deletewhenused Inventory_remove(name$)
                     return 1
                endif
	Next
end function

;check to see if player has an object
function hasobject(name$)
	;returns a 1 if successful (player has item)
	For i.IG_inventory=Each IG_inventory
                if Upper(i\name)=Upper(name$)
                     return 1
                endif
	Next
end function




EDIT!!!
Although the above is plety fast enough as is, Stevie_G has just made me aware of something that could make the type search much more efficient:
http://www.blitzmax.com/Community/posts.php?topic=72979
see his second post!


D4NM4N(Posted 2007) [#3]
Just made some mods to this, still havent tested it in BB yet :/
Let me know how it goes :)