Simple(?) inventory and item menu

Hey guys,

Ok someone wrote me a short example for a simple inventory and item menu. I’m at the point now that I need it but the OG poster is now where around. ive also been on three different sites with no help. I’m really hoping some one can help me figure this out.

Here is the test sketch. please ignore the gb parts.

#ifndef _Menu_H_
#define _Menu_H_

#include "Variables.h"

int menu_1 = 1;
int key_1 = 1;
int key_2 = 1;
int sox_1 = 1;

 int menuy = -84;
 int keyy = -76;
 int scroll1;

/////add item_loc's here////
int key_loc;
int key2_loc;
int sox_loc;

////item slot locations////
int keyslot;
int keyslot2;
int soxslot;

int Active_slot;
int S1 = 0;
int S2 = 0;
int S3 = 0;
int S4 = 0;


     if(S1==0 && S2==0 && S3==0 && S4==0){Active_slot = 1;}
else if(S1==1 && S2==0 && S3==0 && S4==0){Active_slot = 2;}
else if(S1==1 && S2==1 && S3==0 && S4==0){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==1 && S4==0){Active_slot = 4;}
else if(S1==1 && S2==1 && S3==1 && S4==1){Active_slot = 0;}
else if(S1==1 && S2==0 && S3==1 && S4==0){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==0 && S4==1){Active_slot = 2;}
else if(S1==1 && S2==1 && S3==0 && S4==1){Active_slot = 3;}
else if(S1==0 && S2==1 && S3==0 && S4==0){Active_slot = 1;}
else if(S1==0 && S2==1 && S3==1 && S4==0){Active_slot = 1;}
else if(S1==0 && S2==1 && S3==1 && S4==1){Active_slot = 1;}
else if(S1==0 && S2==1 && S3==0 && S4==1){Active_slot = 1;}
else if(S1==0 && S2==0 && S3==1 && S4==0){Active_slot = 1;}
else if(S1==0 && S2==0 && S3==1 && S4==1){Active_slot = 1;}
else if(S1==0 && S2==0 && S3==0 && S4==1){Active_slot = 1;}

if (gb.buttons.pressed(BTN_C))){
   menu_1 ++;}

if(menu_1 == 3){
  scroll1 = 1;
  gb.display.setColor(BLACK);
  gb.display.drawBitmap(0,menuy,menu1);
  gb.display.setColor(WHITE);
  gb.display.drawBitmap(0,menuy,menu2);
  gb.display.setColor(BLACK);}


else if(menu_1 >= 5){
scroll1 =2;
gb.display.setColor(BLACK);
  gb.display.drawBitmap(0,menuy,menu1);
  gb.display.setColor(WHITE);
  gb.display.drawBitmap(0,menuy,menu2);
  gb.display.setColor(BLACK);
    }

if((menu_1 == 3 && key_1 == 3 && Active_slot==1) || (menu_1 == 5 && key_1 == 3 && Active_slot==1)){gb.display.drawBitmap(key_loc,keyy,key);}
if((menu_1 == 3 && key_1 == 3 && Active_slot==2) || (menu_1 == 5 && key_1 == 3 && Active_slot==2)){gb.display.drawBitmap(key_loc,keyy,key);}
if((menu_1 == 3 && key_1 == 3 && Active_slot==3) || (menu_1 == 5 && key_1 == 3 && Active_slot==3)){gb.display.drawBitmap(key_loc,keyy,key);}
if((menu_1 == 3 && key_1 == 3 && Active_slot==4) || (menu_1 == 5 && key_1 == 3 && Active_slot==4)){gb.display.drawBitmap(key_loc,keyy,key);}

if(keyslot==1 && gb.buttons.pressed(BTN_B)){key_1 = 5, S1 = 0, keyslot=0 ;} //////test remove item key
if(keyslot==2 && gb.buttons.pressed(BTN_B)){key_1 = 5, S2 = 0, keyslot=0 ;}
if(keyslot==3 && gb.buttons.pressed(BTN_B)){key_1 = 5, S3 = 0, keyslot=0 ;}
if(keyslot==4 && gb.buttons.pressed(BTN_B)){key_1 = 5, S4 = 0, keyslot=0 ;}

if((menu_1 == 3 && key_2 == 3 && Active_slot==1) || (menu_1 == 5 && key_2 == 3 && Active_slot==1)){gb.display.drawBitmap(key2_loc,keyy,key2);}
if((menu_1 == 3 && key_2 == 3 && Active_slot==2) || (menu_1 == 5 && key_2 == 3 && Active_slot==2)){gb.display.drawBitmap(key2_loc,keyy,key2);}
if((menu_1 == 3 && key_2 == 3 && Active_slot==3) || (menu_1 == 5 && key_2 == 3 && Active_slot==3)){gb.display.drawBitmap(key2_loc,keyy,key2);}
if((menu_1 == 3 && key_2 == 3 && Active_slot==4) || (menu_1 == 5 && key_2 == 3 && Active_slot==4)){gb.display.drawBitmap(key2_loc,keyy,key2);}

//if(keyslot2==1 && gb.buttons.pressed(BTN_B)){key_1 = 5, S1 = 0, keyslot2=0 ;} //////test remove item key
//if(keyslot2==2 && gb.buttons.pressed(BTN_B)){key_1 = 5, S2 = 0, keyslot2=0 ;}
//if(keyslot2==3 && gb.buttons.pressed(BTN_B)){key_1 = 5, S3 = 0, keyslot2=0 ;}
//if(keyslot2==4 && gb.buttons.pressed(BTN_B)){key_1 = 5, S4 = 0, keyslot2=0 ;}

if((menu_1 == 3 && sox_1 == 3 && Active_slot==1) || (menu_1 == 5 && sox_1 == 3 && Active_slot==1)){gb.display.drawBitmap(sox_loc,keyy,sox);}
if((menu_1 == 3 && sox_1 == 3 && Active_slot==2) || (menu_1 == 5 && sox_1 == 3 && Active_slot==2)){gb.display.drawBitmap(sox_loc,keyy,sox);}
if((menu_1 == 3 && sox_1 == 3 && Active_slot==3) || (menu_1 == 5 && sox_1 == 3 && Active_slot==3)){gb.display.drawBitmap(sox_loc,keyy,sox);}
if((menu_1 == 3 && sox_1 == 3 && Active_slot==4) || (menu_1 == 5 && sox_1 == 3 && Active_slot==4)){gb.display.drawBitmap(sox_loc,keyy,sox);}

//if(soxslot==1 && gb.buttons.pressed(BTN_B)){sox_1 = 5, S1 = 0;} //////test remove item sock
//if(soxslot==2 && gb.buttons.pressed(BTN_B)){sox_1 = 5, S2 = 0;}
//if(soxslot==3 && gb.buttons.pressed(BTN_B)){sox_1 = 5, S3 = 0;}
//if(soxslot==4 && gb.buttons.pressed(BTN_B)){sox_1 = 5, S4 = 0;}



if (scroll1 == 1 && menuy < 0){
  menuy = menuy + 2;    ////2 = menu speed down
  keyy = keyy + 2;}     ////2 = menu speed down
  if (scroll1 == 2 && menuy > -84){
    menuy = menuy - 2;  ////2 = menu speed up
    keyy = keyy - 2;}   ////2 = menu speed up
    if (menuy == -84 && scroll1 == 2){
      menu_1 ++;}
      if (menu_1 > 7){
       menu_1 = 1;
        scroll1 = 0;}




////PICKUP ITEMS/EMPTY LOCATIONS/////
else      if((gb.display.solid[i].spritecol == houseB3) && room == 1 && key_1 == 1) {gb.popup(F(" ""It's locked!!"" "),20); return true;}
else      if((gb.display.solid[i].spritecol == houseB3) && room == 6) {gb.popup(F(" ""It's locked!!"" "),20); return true;}


else      if((gb.display.solid[i].spritecol == chest) && room == 1 && Active_slot == 1 && gb.buttons.pressed(BTN_A) && key_1 == 1) {gb.popup(F(" ""You got key"" "),20); key_1 = 3, S1=1, key_loc=8, keyslot=1; return true;}
else      if((gb.display.solid[i].spritecol == chest) && room == 1 && Active_slot == 2 && gb.buttons.pressed(BTN_A) && key_1 == 1) {gb.popup(F(" ""You got key"" "),20); key_1 = 3, S2=1, key_loc=26, keyslot=2 ; return true;}
else      if((gb.display.solid[i].spritecol == chest) && room == 1 && Active_slot == 3 && gb.buttons.pressed(BTN_A) && key_1 == 1) {gb.popup(F(" ""You got key"" "),20); key_1 = 3, S3=1, key_loc=44, keyslot=3; return true;}
else      if((gb.display.solid[i].spritecol == chest) && room == 1 && Active_slot == 4 && gb.buttons.pressed(BTN_A) && key_1 == 1) {gb.popup(F(" ""You got key"" "),20); key_1 = 3, S4=1, key_loc=62, keyslot=4; return true;}


else      if((gb.display.solid[i].spritecol == chest) && room == 1 && gb.buttons.pressed(BTN_A) && key_1 > 1) {gb.popup(F(" ""It's empty!!"" "),20); return true;}
//else      if(gb.display.solid[i].spritecol == chest) return true;

else      if((gb.display.solid[i].spritecol == chest) && room == 5 && Active_slot == 1 && gb.buttons.pressed(BTN_A) && key_2 == 1) {gb.popup(F(" ""You got key"" "),20); key_2 = 3, S1=1, key2_loc=8, keyslot2=1; return true;}
else      if((gb.display.solid[i].spritecol == chest) && room == 5 && Active_slot == 2 && gb.buttons.pressed(BTN_A) && key_2 == 1) {gb.popup(F(" ""You got key"" "),20); key_2 = 3, S2=1, key2_loc=26, keyslot2=2 ; return true;}
else      if((gb.display.solid[i].spritecol == chest) && room == 5 && Active_slot == 3 && gb.buttons.pressed(BTN_A) && key_2 == 1) {gb.popup(F(" ""You got key"" "),20); key_2 = 3, S3=1, key2_loc=44, keyslot2=3; return true;}
else      if((gb.display.solid[i].spritecol == chest) && room == 5 && Active_slot == 4 && gb.buttons.pressed(BTN_A) && key_2 == 1) {gb.popup(F(" ""You got key"" "),20); key_2 = 3, S4=1, key2_loc=62, keyslot2=4; return true;}


else      if((gb.display.solid[i].spritecol == chest) && room == 5 && gb.buttons.pressed(BTN_A) && key_2 > 1) {gb.popup(F(" ""It's empty!!"" "),20); return true;}
else      if(gb.display.solid[i].spritecol == chest) return true;


else      if((gb.display.solid[i].spritecol == bedsidebottom) && room == 5 && Active_slot == 1 && gb.buttons.pressed(BTN_A) && sox_1 == 1) {gb.popup(F(" ""You got a sock?"" "),20); sox_1 = 3, S1=1, sox_loc=7, soxslot=1; return true;}
else      if((gb.display.solid[i].spritecol == bedsidebottom) && room == 5 && Active_slot == 2 && gb.buttons.pressed(BTN_A) && sox_1 == 1) {gb.popup(F(" ""You got a sock?"" "),20); sox_1 = 3, S2=1, sox_loc=25, soxslot=1; return true;}
else      if((gb.display.solid[i].spritecol == bedsidebottom) && room == 5 && Active_slot == 3 && gb.buttons.pressed(BTN_A) && sox_1 == 1) {gb.popup(F(" ""You got a sock?"" "),20); sox_1 = 3, S3=1, sox_loc=43, soxslot=1; return true;}
else      if((gb.display.solid[i].spritecol == bedsidebottom) && room == 5 && Active_slot == 4 && gb.buttons.pressed(BTN_A) && sox_1 == 1) {gb.popup(F(" ""You got a sock?"" "),20); sox_1 = 3, S4=1, sox_loc=61, soxslot=1; return true;}


else      if((gb.display.solid[i].spritecol == bedsidebottom) && room == 5 && gb.buttons.pressed(BTN_A) && sox_1 > 1) {gb.popup(F(" ""It's empty!!"" "),20); return true;}
else      if(gb.display.solid[i].spritecol == bedsidebottom) return true;

This example uses a slot system that automatically puts the item in an inventory slot on a special bitmap with slots left empty. Once in inventory the item should appear in a slot on the inventory menu, The problem is that I’m not seeing where the coordinates are for placing the item in its proper place in the inventory bitmap.

here goes the instruction I was given…

First off we start with an integer for the menu/inventory
(I called mine Menu but you van call yours whatever you like)

int menu_1 = 1;

next you build a bitmap for the item menu, omitted from above code.

then comes the code that actually displays/draws the menu…

if (gb.buttons.pressed(BTN_C)){
   menu_1 = menu_1 + 1;}

if(menu_1 == 3){
  gb.display.setColor(BLACK);
  gb.display.drawBitmap(0,0,menu1);
  gb.display.setColor(WHITE);
  gb.display.drawBitmap(0,0,menu2);
  gb.display.setColor(BLACK);}

else if(menu_1 >= 5){
    menu_1 = 1;}

If button C is pressed our menu_1 integer increases (for some reason this increases by 2 each time?? work in progress lol)
If its 1 it does nothing
if its 3 it displays both our BLACK and WHITE menu/inventory sprite
if it goes to 5 then it resets to 1

next we add the item integer

int key_1 = 1;

We then write the code to say if the menu is active (on screen) and the key is in possetion - display key in inventory (this is simply drawing the key sprite in top of our menu/inventory)

if(menu_1 == 3 && key_1 == 3){
  gb.display.drawBitmap(8,8,key);
}

But in the actual sketch its drawn like so,

if((menu_1 == 3 && key_1 == 3 && Active_slot==1) || (menu_1 == 5 && key_1 == 3 && Active_slot==1)){gb.display.drawBitmap(key_loc,keyy,key);}
if((menu_1 == 3 && key_1 == 3 && Active_slot==2) || (menu_1 == 5 && key_1 == 3 && Active_slot==2)){gb.display.drawBitmap(key_loc,keyy,key);}
if((menu_1 == 3 && key_1 == 3 && Active_slot==3) || (menu_1 == 5 && key_1 == 3 && Active_slot==3)){gb.display.drawBitmap(key_loc,keyy,key);}
if((menu_1 == 3 && key_1 == 3 && Active_slot==4) || (menu_1 == 5 && key_1 == 3 && Active_slot==4)){gb.display.drawBitmap(key_loc,keyy,key);}

but it never gives the actual x y coordinates for each slot. Can some one please help me understand how the item appears in the slot? You guys are my last resort

The code

if(menu_1 == 3 && key_1 == 3){
  gb.display.drawBitmap(8,8,key);
}

Imples that drawBitmap's arguments are (x, y, bitmap) in which case (unless drawBitmap has other overloads) it logically follows that the following lines:

if((menu_1 == 3 && key_1 == 3 && Active_slot==1) || (menu_1 == 5 && key_1 == 3 && Active_slot==1)){gb.display.drawBitmap(key_loc,keyy,key);}
if((menu_1 == 3 && key_1 == 3 && Active_slot==2) || (menu_1 == 5 && key_1 == 3 && Active_slot==2)){gb.display.drawBitmap(key_loc,keyy,key);}
if((menu_1 == 3 && key_1 == 3 && Active_slot==3) || (menu_1 == 5 && key_1 == 3 && Active_slot==3)){gb.display.drawBitmap(key_loc,keyy,key);}
if((menu_1 == 3 && key_1 == 3 && Active_slot==4) || (menu_1 == 5 && key_1 == 3 && Active_slot==4)){gb.display.drawBitmap(key_loc,keyy,key);}

imply that key_loc and keyy are the x and y coordinates.

(And the moral of this story is to use descriptive variable names.)

Teach me something. I pretty much figured it out but it in-efficient. Here’s why.

if((menu_1 == 3 && item_1 == 3 && Active_slot==1) || (menu_1 == 5 && item_1 == 3 && Active_slot==1)){tft.drawRect(slot1_x,slot1_y,81,16,BLUE); tft.setCursor(slot1_x, slot1_Y); tft.print(water);}                                                    ///{tft.writeRectNBPP(slot1_x,slot1_y,114,16,4,item_1);}

I have to repeat that line for as many times as I have slots for. So if I have 10 slots that item code to pick it up and display it on my item menu has to be repeated. So imagine if I made 100 slots? That’s a lot of code.

Heres my idea. This table from what I understand is always pushing the items up the list to take up any empty slot.

    if(S1==0 && S2==0 && S3==0 && S4==0 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 1;}
else if(S1==1 && S2==0 && S3==0 && S4==0 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 2;}
else if(S1==1 && S2==1 && S3==0 && S4==0 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==1 && S4==0 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 4;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 5;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 6;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 7;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==0 && S9==0 && S10==0){Active_slot = 8;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==0 && S10==0){Active_slot = 9;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==0){Active_slot = 10;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 0;}

else if(S1==1 && S2==0 && S3==0 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==0 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==0 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==0 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==1 && S7==0 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==0 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==0 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==0){Active_slot = 2;}

else if(S1==1 && S2==1 && S3==0 && S4==0 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==0 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==0 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==1 && S7==0 && S8==1 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==1 && S7==1 && S8==0 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==0 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==0){Active_slot = 3;}

else if(S1==1 && S2==1 && S3==1 && S4==0 && S5==0 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 4;}
else if(S1==1 && S2==1 && S3==1 && S4==0 && S5==1 && S6==0 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 4;}
else if(S1==1 && S2==1 && S3==1 && S4==0 && S5==1 && S6==1 && S7==0 && S8==1 && S9==1 && S10==1){Active_slot = 4;}
else if(S1==1 && S2==1 && S3==1 && S4==0 && S5==1 && S6==1 && S7==1 && S8==0 && S9==1 && S10==1){Active_slot = 4;}
else if(S1==1 && S2==1 && S3==1 && S4==0 && S5==1 && S6==1 && S7==1 && S8==1 && S9==0 && S10==1){Active_slot = 4;}
else if(S1==1 && S2==1 && S3==1 && S4==0 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==0){Active_slot = 4;}

else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==0 && S6==0 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 5;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==0 && S6==1 && S7==0 && S8==1 && S9==1 && S10==1){Active_slot = 5;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==0 && S6==1 && S7==1 && S8==0 && S9==1 && S10==1){Active_slot = 5;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==0 && S6==1 && S7==1 && S8==1 && S9==0 && S10==1){Active_slot = 5;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==0 && S6==1 && S7==1 && S8==1 && S9==1 && S10==0){Active_slot = 5;}


else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==0 && S7==0 && S8==1 && S9==1 && S10==1){Active_slot = 6;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==0 && S7==1 && S8==0 && S9==1 && S10==1){Active_slot = 6;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==0 && S7==1 && S8==1 && S9==0 && S10==1){Active_slot = 6;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==0 && S7==1 && S8==1 && S9==1 && S10==0){Active_slot = 6;}

else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==0 && S8==0 && S9==1 && S10==1){Active_slot = 7;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==0 && S8==1 && S9==0 && S10==1){Active_slot = 7;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==0 && S8==1 && S9==1 && S10==0){Active_slot = 7;}

else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==0 && S9==0 && S10==1){Active_slot = 8;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==0 && S9==1 && S10==0){Active_slot = 8;}

else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==0 && S10==0){Active_slot = 9;}

if so I think I should be able to make a structure for each slot with the coordinates of each slot. Then on the menu parts and collision parts just mark as to go to slot 1. then it goes to the table to see which slot is actually empty which should always be the last open slot.

I’m just not sure how to go about doing so. I’m up to some suggestions or ideas

Why can’t the slots be an array of integers or even an array of structures?

Then you can iterate through them using a ‘for’ loop.

1 Like

Well that leads to more questions. Maybe since I’m learning a new coding skill I should start with a fresh set of functions.

first… if I give a structure to S1 like so

struct S1{
int x = 16;
int y = 48;
int w = 81;
int h = 16;};

can I still give it an inyerger like so…

int S1 = 0;
int S2 = 0;
int S3 = 0;
int S4 = 0;
int S5 = 0;
int S6 = 0;
int S7 = 0;
int S8 = 0;
int S9 = 0;
int S10 = 0;

I was told I could do this but I don’t understand how get the hex value otherwise it works the same by always shifting everthing to nearest open slot

Blockquote
You basically need an association table. S1, S2, S3 and S4 are booleans, so you can perform bitwise operations to create a key:

For example S1 : bit 4, S2 bit 3, S3 bit 2 and S4 bit 1.
It gives:

table[0x00] = 1;
table[0x08] = 2;
table[0x0c] = 3;
table[0x0e] = 4;
table[0x0f] = 0;
table[0x0a] = 2;
table[0x0b] = 2;

and so forth.
Then you just need: Active_slot = table[S1 << 4 | S2<<3 | S3<<2 | S4];
You save A LOT of CPU instructions this way.

You can create an array of structs:

struct Slot {
  int x;
  int y;
  int w;
  int h;
};

const Slot slots[] = { 
  { 16, 48, 81, 16 },
  { 16, 48, 81, 17 },
  ...  // and so on.
};

If you know how many slots you have you can :slight_smile:

const uint8_t no_of_slots = 2;

const S1 slots[no_of_slots] = { 
  { 16, 48, 81, 16 },
  { 16, 48, 81, 17 }
};

Untested!

1 Like

Not sure what the other poster had in mind, sorry.

instead of doing this…

     if(S1==0 && S2==0 && S3==0 && S4==0 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 1;}
else if(S1==1 && S2==0 && S3==0 && S4==0 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 2;}
else if(S1==1 && S2==1 && S3==0 && S4==0 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==1 && S4==0 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 4;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==0 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 5;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==0 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 6;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==0 && S8==0 && S9==0 && S10==0){Active_slot = 7;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==0 && S9==0 && S10==0){Active_slot = 8;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==0 && S10==0){Active_slot = 9;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==0){Active_slot = 10;}
else if(S1==1 && S2==1 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 0;}

else if(S1==1 && S2==0 && S3==0 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==0 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==0 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==0 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==1 && S7==0 && S8==1 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==0 && S9==1 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==0 && S10==1){Active_slot = 2;}
else if(S1==1 && S2==0 && S3==1 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==0){Active_slot = 2;}

else if(S1==1 && S2==1 && S3==0 && S4==0 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==0 && S6==1 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==0 && S7==1 && S8==1 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==1 && S7==0 && S8==1 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==1 && S7==1 && S8==0 && S9==1 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==0 && S10==1){Active_slot = 3;}
else if(S1==1 && S2==1 && S3==0 && S4==1 && S5==1 && S6==1 && S7==1 && S8==1 && S9==1 && S10==0){Active_slot = 3;}

and so on

which will get longer the more slots I make

Right … back to my array of structs answer.

ok I was thinking something more like this but I need active slot to point to which ever slot is empty rather than slot 1.

the menu works by increasing the integer, if 1 no menu if 3 menu if 5 go back to 1 which kills it. it uses the table I made above

struct S1{
int x = 16;
int y = 48;
int w = 81;
int h = 16;};

struct s2{
int x = 16;
int y = 64;
int w = 81;
int h = 16;};

struct s3{
int x = 16;
int y = 80;
int w = 81;
int h = 16;};

struct s4{
int x = 16;
int y = 96;
int w = 81;
int h = 16;};

struct s5{
int x = 16;
int y = 112;
int w = 81;
int h = 16;};

struct s6{
int x = 16;
int y = 128;
int w = 81;
int h = 16;};

struct s7{
int x = 16;
int y = 144;
int w = 81;
int h = 16;};

struct s8{
int x = 16;
int y = 160;
int w = 81;
int h = 16;};

struct s9{
int x = 16;
int y = 176;
int w = 81;
int h = 16;};

struct s10{
int x = 16;
int y = 192;
int w = 81;
int h = 16;};

struct s11{
int x = 16;
int y = 208;
int w = 81;
int h = 16;};

int Active_slot;
int S1 = 0;
int S2 = 0;
int S3 = 0;
int S4 = 0;
int S5 = 0;
int S6 = 0;
int S7 = 0;
int S8 = 0;
int S9 = 0;
int S10 = 0;


int menu_1 = 1;
int item_1 = 1;
int item_2 = 1;
int item_3 = 1;
int item_4 = 1;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    if (ButtonA.fallingEdge()){menu_1;}
            menu_1 = menu_1 + 1;}


if(menu_1 == 3){

   
    tft.writeRectNBPP(0,0,16,16,4,itemmenutop,palette);
     tft.writeRectNBPP(0,32,16,16,4,itemmenu2,palette);
     tft.writeRectNBPP(0,48,16,16,4,itemmenu3,palette);
    tft.writeRectNBPP(0,224,16,16,4,itemmenu4,palette);
};

else if(menu_1 >= 5){
    menu_1 = 1;}

if((menu_1 == 3 && item_1 == 3 && Active_slot==1) || (menu_1 == 5 && item_1 == 3 && Active_slot==1)){tft.drawRect(slot1_x,slot1_y,81,16,BLUE); tft.setCursor(slot1_x, slot1_Y); tft.print(water);}    

can you elaborate on array structs

wait you gave me an example

ok so making this was easy enough

const uint8_t no_of_slots = 10;

const slots[no_of_slots] = { 
  { 16, 48, 81, 16 },
  { 16, 64, 81, 16 },
  { 16, 80, 81, 16 },
  { 16, 96, 81, 16 },
  { 16, 112, 81, 16 },
  { 16, 128, 81, 16 },
  { 16, 144, 81, 16 },
  { 16, 160, 81, 16 },
  { 16, 176, 81, 16 },
  { 16, 192, 81, 16 },
 // { 16, 208, 81, 16 },

};

But I’m thinking that’s mot the array you were talking about

Maybe you can add to the struct:

struct Slot {
  int x;
  int y;
  int w;
  int h;
  bool avail;
};

Now you can look for the first available slot by doing something like:

uint8_t getFirstAvailSlot() {

  for (uint8_t i = 0; i < no_of_slots; i++) {
    if ((slots[i].avail == true) return i;
  }

  return 255; // Or something that signifies no slots available ..

}
1 Like

Ok, let’s simplify this: describe your actual problem.

What is your code trying to achieve?
What mechanism in the game does your code represent?
Is it an inventory system?
Is it an options menu?
What is it supposed to look like?
How is it supposed to behave?

1 Like

Inventory menu and placing stuff in the inventory.

Which of the six questions did you answer there @Duhjoker?

1 Like

I belive it was this one @filmote

Depending on how complex your code is for storing items you could have an array of struct that can be displayed

struct Item{
   byte Id;
   byte Stack;
}

struct Item Inventory[MAXITEMCOUNT];

Sorry long day. My code is trying to achieve an inventory system that can be added to a simple graphic menu.

My code should represent the start of an inventory system

Yea it is an inventory system

No options except to be able to choose an item in inventory and use it but for now i just need to be able to pick items up and place them in inventory

Visually it ahould look something like a classic Final fantasy menu.

I have the graphic part down i just need help learning how to create a slot system that automatically puts the item to the nearest available slot in inventory.

Would also be nice if each inventory slot could hold multiple items of the same kind. Like herbs x20

1 Like

Look at Structs and how to use them in arrays Can be found here

I hope this can help you. The previous code i used should sort out your stacking issue.

An empty element would return 0 for nothing and going by this you can use a loop to find an empty Inventory slot to which you can put the item or if the item has already been collected add it to that stack.

you will have to initialize your array at the start so that it is the case that all elements in the array begin with 0 as the ID as well as the Stack variable.

In this thread you have all the available tools to build this piece of code.

This is resolved here from filmote

As for this section of your code you will have to find the items that are available to pick up in a specific area relative to the player this can be done using Manhattan distance or Euclidean distance

If any of this is Unclear drop me a PM and i will respond as soon as i can

1 Like

Now we know your goal, it’s at lot easier to suggest things.

Firstly though, you don’t have one problem, you have several problems:

  • Defining an item
  • Defining a group of items (“Like herbs x20”)
  • Defining an inventory (a collection of items/groups of items)
  • Adding to the inventory
  • Picking items up
  • Displaying the contents of the inventory in a menu
  • Being able to select items from the menu
  • Having a selected item do something
  • Removing the selected item after use

So this isn’t a straightforward thing to solve.


@filmote and @Dreamer2345 have given a partial solution to a few of these problems and they’re right that you won’t solve this if you don’t know the ins and outs of arrays and structs.

We also don’t know the details of the system you’re writing for (how much RAM there is, whether it uses progmem, does it use C++03, C++11, C++14 or C++17, do you have access to the C++ stdlibs, do you have access to uint8_t, what is the graphics library like) so there’s a limit to what we can suggest.
(After all, this is the Arduboy forum, not the forum intended for the platform you’re writing for.)

There’s many different ways of having a menu system.
You can have one that shuffles all items down when one is removed, you can have one where empty gaps are allowed, you can have a hybrid of the two. It all depends on what you’re aiming for.

3 Likes

Cant wait to get back home so i can try what ive learned. In the meantime this should answer the questions on the ram and such.

https://forum.pjrc.com/threads/36613-Why-Teensy-3-5-or-3-6