Inventory System using 1 column

Discussion in 'Programming' started by RedP, Jun 12, 2019.

  1. RedP

    RedP Member

    Joined:
    Apr 8, 2017
    Posts:
    39
    Every time i start a new project i struggle with inventory, i understand the basic of grid system, but i lose my mind when i need to use too many variables, i just want to learn how to create and check for empty slots.
    . upload_2019-6-12_18-5-19.png
    This is what i am trying to create, every time i click on any of these buttons at the bottom create a sprite of itself in the inventory (Blue bar). I don't want any stack, just something really simple, similar to dota autochess bench. At the moment i am replacing the sprite when i click it, so this is what i have:

    Code:
    if position_meeting(mouse_x, mouse_y, self)
    {
        if mouse_check_button_pressed(mb_left)
        {
            //UNIDADES
            sprite[1] = spr_cat;
            sprite[2] = spr_ovelha;
            sprite[3] = spr_lion;
            sprite[4] = spr_chameleon;
            sprite[5] = spr_snake;
            sprite[6] = spr_alligator;
            sprite[7] = spr_fly;
            sprite[8] = spr_bee;
            sprite[9] = spr_butterfly;
            sprite[10] = spr_penguin;
            sprite[11] = spr_owl;   
            sprite[12] = spr_vulture;
            
            //CHANCE DE VIR CADA UNIDADE
            chance = irandom_range(0, 100)
            if chance <= 80
            {
                sprite_index = choose(1, 4, 7, 10);
            }
            else if chance > 80
            {
                sprite_index = choose(2, 5, 8, 11);
            }
    obj_inventory:

    Code:
    maxslots = 8;
    slots_width = 1;
    slots_height = 8;
    
    x_buffer = 16;
    y_buffer = 16;
    
    slot_size = 64;
     
  2. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    680
    The way I set up my inventories is to make an array and set all the entries to -1 (-1 for empty).

    I also make a single sprite with lots of different images so that if an apple is image_index 2 and inventory slot #3 holds an apple, the actual entry in the inventory would be "2"

    It's easier to explain with code:

    Code:
    //Create Event of a controller object
    max_inv = 8;
    
    for (var i = 0; i < max_inv; i ++){
       //set all 8 slots to -1 (empty)
       global.inv[i] = -1;
    }
    To add an item:

    Code:
    //Run this code whenever you want to add an item to the inventory
    
    //item will be an integer like "2"
    
    for (var i = 0; i < max_inv; i ++){
      //If the inventory slot is empty
      if global.inv[i] == -1{
        //Add the item to the free slot
         global.inv[i] = item;
       
        //Break the for loop
        break;
      }
    }
    To draw the inventory:

    Code:
    //A draw / draw_gui event
    
    for (var i = 0; i < max_inv; i ++){
       //Draw normal empty box sprite
       draw_sprite(spr_empty_box, 0, xx, yy);
    
       //If the inventory slot holds something, display it
       if global.inv[i] > -1{
          item = global.inv[i];
          draw_sprite(spr_all_item, item, xx, yy);
       }
    }
     
    woods likes this.
  3. Noizee

    Noizee Member

    Joined:
    Sep 17, 2016
    Posts:
    16
    That looks interesting. What are you trying to make?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice