Every item must have a location. This location can be the loc it is in, or special locations if the item is hidden or if player carries the items.
This location can be outside the grid, for example, I use location 255 as my inventory.
When I pick up an object, I place it in location 255. When I want to show the inventory, I print all the objects stored in location 255.
Time to see the code.
;-----------------
Inventory_CMD
lda #str_you_carry
sta INPUT_BUFFER+1
lda #LOC_INVENTORY
sta P0_TMP_BYTE
jsr Inventory_Entry
rts
; Helper subrotuine for command Inventory
Inventory_Entry
; Prepare the callback for the subroutin
lda #C_Print_Item
sta P0_TMP_B
jsr F_Search_Object
rts
Inventory subroutine is very similar to the subroutines for searching verbs, because I used the same idea.
Subrotuine Inventory_Entry is a common part of two different subroutines, the one that search objects in the actual loc, and the one that prints the inventory.
There is a helper subroutine, called F_Search_Object that traverse all objects calling a callback and this second one decides what to do.
;--- Search for items
; LOC is in P0_TMP_BYTE
F_Search_Object
ldx #$0
F_SO_Loop
lda obj_status,x
beq F_SO_End
; 2 status bit = 4 status
and #%00111111
cmp P0_TMP_BYTE
bne F_SO_Next_Obj
; Object found in current loc
txa ; Save x before callback
pha
jmp $(fb) ; Callback
F_SO_Back
pla ; Restore X
tax
F_SO_Next_Obj
inx
jmp F_SO_Loop
F_SO_End
rts
The callback is C_Print_Item.
; Callback
C_Print_Item
; If the item is found,
; prints the message stored in INPUT_BUFFER
; Item found in loc
; Print item name
; This code is the same than the code for showing the loc
; x = (loc*2)
asl
pha
ldy INPUT_BUFFER+1
lda INPUT_BUFFER
; This subrotuine chages x register
; so I stored first and I recover it later
JSR PRINT
pla
tax
ldy obj_list,x+1
lda obj_list,x
JSR PRINT
lda #RETURN
jsr PRINT_CHAR
; Jump back to the subroutine
jmp F_SO_Back
I though this could be a good idea but I am not happy at the end. I am not sure if I reuse enough code to deal with the extra complexity. I will try different ideas in following games.