Improving my inventory system...

N

nlolotte

Guest
Hello,

I am looking for some help improving and in some parts, re-doing my inventory system. I made the inventory in the early stages of developing my game. Since then, It doesn't really seem fit for purpose and also has a few flaws.
  • The way I am controlling the cursor is completely wrong.
Code:
/// @description Check Position
if global.cursor_pos = 1
{
    x = obj_inventory_cell_1.x;
    y = obj_inventory_cell_1.y;
}
if global.cursor_pos = 2
{
    x = obj_inventory_cell_2.x;
    y = obj_inventory_cell_2.y;
}
if global.cursor_pos = 3
{
    x = obj_inventory_cell_3.x;
    y = obj_inventory_cell_3.y;
}
if global.cursor_pos = 4
{
    x = obj_inventory_cell_4.x;
    y = obj_inventory_cell_4.y;
}
Etc...

//Get Input
scr_input();

//Left
if leftkeypressed
{
    if global.cursor_pos = 1
    {
        global.cursor_pos = 48;
        exit;
    }
    if global.cursor_pos = 2
    {
        global.cursor_pos = 1;
        exit;
    }
    if global.cursor_pos = 3
    {
        global.cursor_pos = 2;
        exit;
    }

 Etc.....

  • The way I have created the inventory is poorly optimized and uses alot of resources. 48 different boxes to contain the items. This also allows for the cursor to move accross a space that doesnt have an item in it.

This is a video fo the inventory thus far:


This is how my inventory is set up presently.

Code:
//Initialize Variables
global.weapon_in_inv = 0;
globalvar max_weapon_slots;
max_weapon_slots = 50;

//Create Inventory Slots
var i;
for(i = 0; i <= max_weapon_slots; i ++)
{
    global.weapon_inventory[i,0] = "";    //Name
    global.weapon_inventory[i,1] = "";    //Description
    global.weapon_inventory[i,2] = "";    //Stackable?
    global.weapon_inventory[i,3] = "";    //Quantity
    global.weapon_inventory[i,4] = "";    //Inventory Sprite
    global.weapon_inventory[i,5] = "";    //Weapon ID
    global.weapon_inventory[i,6] = "";    //Added STR
    global.weapon_inventory[i,7] = "";    //Added CON
    global.weapon_inventory[i,8] = "";    //Added INT
    global.weapon_inventory[i,9] = "";    //Added LCK
    global.weapon_inventory[i,10] = "";    //Attack Power
    global.weapon_inventory[i,11] = "";    //Type: 0 - Empty, 1 - Stab, 2 - Slash, 3 - Swing
    global.weapon_inventory[i,12] = ""; //Atrribute: 0 - Nothing, 1 - Poison, 2 - Burn
    global.weapon_inventory[i,13] = ""; //Atrribute Chance
    global.weapon_inventory[i,14] = ""; //Attack Speed
 
}

//Define Weapons Inventory
global.weapon[0,0] = "EMPTY HAND";
global.weapon[0,1] = "----";
global.weapon[0,2] = false;
global.weapon[0,3] = 1;
global.weapon[0,4] = spr_inventory_empty_hand;
global.weapon[0,5] = 1;
global.weapon[0,6] = 0;
global.weapon[0,7] = 0;
global.weapon[0,8] = 0;
global.weapon[0,9] = 0;
global.weapon[0,10] = 0;
global.weapon[0,11] = 0;
global.weapon[0,12] = 0;
global.weapon[0,13] = 0;
global.weapon[0,14] = 18;

global.weapon[1,0] = "BASIC WEAPON";
global.weapon[1,1] = "THIS WEAPON SWINGS.";
global.weapon[1,2] = false;
global.weapon[1,3] = 1;
global.weapon[1,4] = spr_inventory_basic_weapon;
global.weapon[1,5] = 2;
global.weapon[1,6] = 3;
global.weapon[1,7] = 0;
global.weapon[1,8] = 0;
global.weapon[1,9] = 0;
global.weapon[1,10] = 32;
global.weapon[1,11] = 3;
global.weapon[1,12] = 0;
global.weapon[1,13] = 0;
global.weapon[1,14] = 14;

global.weapon[2,0] = "TEST WEAPON 1";
global.weapon[2,1] = "THIS WEAPON SLASHES.";
global.weapon[2,2] = false;
global.weapon[2,3] = 2;
global.weapon[2,4] = spr_inventory_test_weapon_1;
global.weapon[2,5] = 3;
global.weapon[2,6] = 7;
global.weapon[2,7] = 4;
global.weapon[2,8] = 3;
global.weapon[2,9] = 0;
global.weapon[2,10] = 15;
global.weapon[2,11] = 2;
global.weapon[2,12] = 0;
global.weapon[2,13] = 0;
global.weapon[2,14] = 15;

global.weapon[3,0] = "TEST WEAPON 2";
global.weapon[3,1] = "THIS WEAPON STABS.";
global.weapon[3,2] = false;
global.weapon[3,3] = 1;
global.weapon[3,4] = spr_inventory_test_weapon_2;
global.weapon[3,5] = 4;
global.weapon[3,6] = 1;
global.weapon[3,7] = 2;
global.weapon[3,8] = 4;
global.weapon[3,9] = 6;
global.weapon[3,10] = 5;
global.weapon[3,11] = 1;
global.weapon[3,12] = 0;
global.weapon[3,13] = 0;
global.weapon[3,14] = 16;

Etc...

Code:
//Add Weapon To Inventory
var i;
for(i = 0; i < max_weapon_slots; i += 1)
{
    if(global.weapon_inventory[i,0] == "")
    {
        global.weapon_inventory[i,0] = argument0;
        global.weapon_inventory[i,1] = argument1;
        global.weapon_inventory[i,2] = argument2;
        global.weapon_inventory[i,3] = argument3;
        global.weapon_inventory[i,4] = argument4;
        global.weapon_inventory[i,5] = argument5;
        global.weapon_inventory[i,6] = argument6;
        global.weapon_inventory[i,7] = argument7;
        global.weapon_inventory[i,8] = argument8;
        global.weapon_inventory[i,9] = argument9;
        global.weapon_inventory[i,10] = argument10;
        global.weapon_inventory[i,11] = argument11;
        global.weapon_inventory[i,12] = argument12;
        global.weapon_inventory[i,13] = argument13;
        global.weapon_inventory[i,14] = argument14;
        global.weapon_in_inv += 1;
        i = max_weapon_slots;
    }
    if(global.weapon_inventory[i,0] == argument0 && global.weapon_inventory[i,2] == true)
    {
        global.weapon_inventory[i,3] += argument3;
        global.weapon_in_inv += 1;
        i = max_weapon_slots;
    }
}

I'd like to make a scrollable grid inventory, how can I approach this?

Thank you.
 
Last edited:

PlayerOne

Member
I might be able to help with some of what you're asking.

Few things:

1) Are you using a seperate object as a cursor or did you draw the mouse on screen? Can't tell from the code you posted.

2) How you set up the inventory is no different from my own system(s). I'm sure you manually added the item information for testing purposes instead of adding it to the item/object itself. All good on that front.

If you intend to add item stacking to your game I'll refer you to this topic I made:

https://forum.yoyogames.com/index.p...llover-into-new-inventory-slot-ds_grid.48108/

3) For scrolling you need to modify the y position.

Code:
CREATE:

yscroll=0


//STEP

if (yscroll!=0){y_scroll += mouse_wheel_up() * 15;}
yscroll -= mouse_wheel_down() * 15;


//DRAW
draw_text(x+10,y+10+yscroll,string(text))
Instead of using the mouse wheel in the example you can use a keyboard input. Either works.
 
N

nlolotte

Guest
I might be able to help with some of what you're asking.

Few things:

1) Are you using a seperate object as a cursor or did you draw the mouse on screen? Can't tell from the code you posted.

2) How you set up the inventory is no different from my own system(s). I'm sure you manually added the item information for testing purposes instead of adding it to the item/object itself. All good on that front.

If you intend to add item stacking to your game I'll refer you to this topic I made:

https://forum.yoyogames.com/index.p...llover-into-new-inventory-slot-ds_grid.48108/

3) For scrolling you need to modify the y position.

Code:
CREATE:

yscroll=0


//STEP

if (yscroll!=0){y_scroll += mouse_wheel_up() * 15;}
yscroll -= mouse_wheel_down() * 15;


//DRAW
draw_text(x+10,y+10+yscroll,string(text))
Instead of using the mouse wheel in the example you can use a keyboard input. Either works.
Thanks for your reply.

The cursor is it's own object and the scode posted above is how I move it from square the square. I manually hardcode each individual space. Which, as you can imagine, takes a long time when you have 48 spaces and have to code each event for up down left right.

How do I approach scrolling just the grid size and how do I only create as many grid spaces as I have items?
 

PlayerOne

Member
Thanks for your reply.

The cursor is it's own object and the scode posted above is how I move it from square the square. I manually hardcode each individual space. Which, as you can imagine, takes a long time when you have 48 spaces and have to code each event for up down left right.

How do I approach scrolling just the grid size and how do I only create as many grid spaces as I have items?
The cursor can be drawn instead of using an object. The trick is changing the position of the cursor with each press.

Code:
EXAMPLE:
   /// Inventory boxes are 64x64 for this instance. I'm using my setup to show you what I did.

//CREATE:

slot_num=0;
row_y=0;
mpos=0;



//STEP:

///This will move the cursor and move it down a row once it hits the max on the x position, then moving down to the next row reseting move_y.


           var move_y=0;
             
           move_x-=max(keyboard_check_pressed(vk_a),keyboard_check_pressed(vk_left),0)
           move_x+=max(keyboard_check_pressed(vk_d),keyboard_check_pressed(vk_right),0)
 
           var x_amount = 4; // how many inventory boxes on the xaxis can the cursor go


           //NOTE: I wrote this code and wrote row_y instead of row_x. lol
           if (slot_num > (x_amount-1)) // <<< This is based on how the inventory is set.      
           {
           row_y++;
           slot_num=0
           }
          
           if (slot_num < 0) // <<< This is based on how the inventory is set.      
           {
           row_y--;
           slot_num=(x_amount-1)

           }


    

               if (move_x!=0) // <<< Movement of cursor
               {
               mpos+=move_x;
               slot_num+=move_x;
               }



/DRAW GUI:

draw_sprite(spr_cursor,0,pos_x+(slot_num*inv_size),pos_y+(row_y*inv_size))

What I posted in my previous post will scroll down the inventory, but as far as trying to factor in grid spaces with items inside I don't know how to help you there.
 
N

nlolotte

Guest
The cursor can be drawn instead of using an object. The trick is changing the position of the cursor with each press.

Code:
EXAMPLE:
   /// Inventory boxes are 64x64 for this instance. I'm using my setup to show you what I did.

//CREATE:

slot_num=0;
row_y=0;
mpos=0;



//STEP:

///This will move the cursor and move it down a row once it hits the max on the x position, then moving down to the next row reseting move_y.


           var move_y=0;
            
           move_x-=max(keyboard_check_pressed(vk_a),keyboard_check_pressed(vk_left),0)
           move_x+=max(keyboard_check_pressed(vk_d),keyboard_check_pressed(vk_right),0)
 
           var x_amount = 4; // how many inventory boxes on the xaxis can the cursor go


           //NOTE: I wrote this code and wrote row_y instead of row_x. lol
           if (slot_num > (x_amount-1)) // <<< This is based on how the inventory is set.     
           {
           row_y++;
           slot_num=0
           }
         
           if (slot_num < 0) // <<< This is based on how the inventory is set.     
           {
           row_y--;
           slot_num=(x_amount-1)

           }


   

               if (move_x!=0) // <<< Movement of cursor
               {
               mpos+=move_x;
               slot_num+=move_x;
               }



/DRAW GUI:

draw_sprite(spr_cursor,0,pos_x+(slot_num*inv_size),pos_y+(row_y*inv_size))

What I posted in my previous post will scroll down the inventory, but as far as trying to factor in grid spaces with items inside I don't know how to help you there.
Thanks for your help, that has resolved the cursor issue :)
 
Top