LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

2D array inventory help.

Root / Programming Questions / [.]

Z_E_R_OCreated:
I've been terrible with arrays sense the beginning, If anyone would help I would greatly appreciate it. My problem is I want to have a bag system but I want all the things you have not collected yet to appear as questionmarks, I also want each item to have a description. There will be 3 seperate inventories. The items, Compendium, and Music. I know now how to set up the array Dim bag[8,16] I do not the fundamentals of how to make items appear inside the arrays. Thank you... sorry if I said anything wrong.

To clarify The bag array I want to fill with usable items that when used will disappear or if its equipment stay there. The compendium I want to have question Mark's until you discover it. And the music player I want to also be question Mark's until you unlock the music. (The tracks will be represented by CD sprites)

This can be a bit complex and lengthy. Here is what I’ve done in similar circumstances. For items (consumed or not consumed), I have a 1D array because it is easier to remove items after use. However, this means 1 item = 1 inventory slot and no item stacking, such as “potion x 4” etc. Each item is a user defined function that is called when the item is selected.
Example code
LOOP
 VAR ITEM$=pickitem()
 CALL ITEM$
ENDLOOP

DEF pickitem()
 'Code for navigation/confirm selection
  RETURN 'x item
END

DEF 'x item
 'Whatever the item does
 'remove item
END
For the music and compendium, you could create a 2D string array where first piece is the title and the second contains a description. This would be the lookup information. Then, when displaying the info, obscure the title and/or description.
Example code
DIM LOOKUPREF$[2,2]
COPY LOOKUPREF$,”@INFO”

@INFO
DATA “Pyrodance”,”A fiery, upbeat song”
DATA “Hodgepodge”, “a monster made from other monster parts”

DisplayTitle

LOOP
 pickoption
ENDLOOP

DEF DisplayTitle
 VAR I,TITLE$=“???”
 FOR I=0 TO 1'length of lookupref
  IF UNLOCKED[I]==1 THEN TITLE$=LOOKUPREF$[I,0]'where title is stored
  'you will need to track what is unlocked vs not, I used an array for that here
 LOCATE 0,0+I:?TITLE$
 NEXT
END

DEF pickoption
 'in navigation code, each time you move, call “Lookup x” where x is selected item
END

DEF Lookup n$
 VAR I
 FOR I=0 TO 1'len of lookup
  IF LOOKUPREF$[I,0]==n$ THEN BREAK
 NEXT
 LOCATE 0,27:?LOOKUPREF$[I,1]' where info is stored
END
The code will obviously need some tweaking but I hope it gives you an idea of how to go about your situation. Good luck

Thank you! It put me on the right path. I'm getting closer to understanding arrays... Anyways again thank you! The only thing I have to really figure out now is how to save a randomly generated map into a save file. I'm using to for statements and Rnd to generate changes in grass patches but I dont really know how to save that into a string for a file... if I even use strings for that. Ffs lol

And also how to destinguish what tile the player is standing on like grass, Wood etc. Sorry I'm not trying to bother you or pressure you.

No problem, I am happy to help. Saving a randomly generated map could be tricky. But you are on the right track I think. I would recommend storing the map height and width to global variables and do something like this:
Example
VAR H,W' stores map height and width

DEF SAVEMAP
 DIM SAVEARRAY[4,H,W]
 VAR I,J,L
 FOR L=0 TO 3' store layers 0-3
  FOR I=0 TO H-1' assuming 0 is minimum point of map
   FOR J=0 TO W-1
    SAVEARRAY[L,H,W]=CHKCHR(L,H,W)
   NEXT
  NEXT
 NEXT
'use save function to save the array 
END

DEF LOADMAP
 DIM LOADARRAY[4,H,W]'load map into this array 
 VAR I,J,L
 FOR L=0 TO 3
  FOR I=0 TO H-1
   FOR J=0 TO W-1
    TPUT L,J,H,LOADARRAY[L,I,J]
   NEXT
  NEXT
 NEXT
END
Again, I’m not 100% confident the above code will work for your situation, so you have to modify it some. To distinguish the tile your character is standing on, the CHKCHR function is handy. It takes layer, x, y and either sprite or map coordinates (set to 1 for sprite coordinates) as arguments. Include that function in either your main loop or movement function. It will return the tile number.
VAR TILE=CHKCHR(0,X,Y,1)