I can't create a inventory!

W

WimpyLlama

Guest
I followed Shuan Spalding's inventory tutorial, and it was really good. But I am trying to make an inventory whereby scrolling with your mouse wheel, you can selected slots in the inventory for things like breaking and placing blocks. I can't seem to figure this out. I tried scripts, while loops, do loops, for loops, and a bunch of other stuff. I know how to do this if I type a long thing of code, but the long way does not work if I change the inventory size. Please help me!
 
I followed Shuan Spalding's inventory tutorial, and it was really good. But I am trying to make an inventory whereby scrolling with your mouse wheel, you can selected slots in the inventory for things like breaking and placing blocks. I can't seem to figure this out. I tried scripts, while loops, do loops, for loops, and a bunch of other stuff. I know how to do this if I type a long thing of code, but the long way does not work if I change the inventory size. Please help me!
What you want to do is set the index of the inventory everytime the mouse wheel is rotating.
Your code would like something like this in a step event:
Code:
if(mouse_wheel_up())
{
     if(index > max_index) //max index will be the size of your inventory
     {
           index = 0;
     }
     else
     {
          index++;
     }
}
if(mouse_wheel_down())
{
     if(index == 0) 
     {
           index = max_index;
     }
     else
     {
          index--;
     }
}
then use inventory[index] (depends on the data structure) to set the current index.
 
W

WimpyLlama

Guest
Can you explain what your code does? I am trying to figure it out. The system that I have currently is that I have two arrays. One for the inventory like the tutorial I talked about. And one for if something is selected. Here is my code. I am happy to change the system I am currently using if I need to.
Code:
Create:

/*

    inventroy items and blocks
    -1 empty
    0 dirt
    1 grass
    2 sword
   
    inventory selected
    0 not selected
    1 selected

*/

//create global variables
globalvar invSize;
invSize = 5;

globalvar invShow;
invShow = true;

globalvar index;
index = 0;

//set arrays
for (var i = 0; i < invSize; i++) {

    global.inventory[i] = -1;
    global.inventorySelected[i] = 0;

}

global.inventorySelected[0] = 1;

Draw GUI:
//draw inventory
if (invShow) {

    for (var i = 0; i < invSize; i++) {
   
        draw_sprite(sInvBorder,global.inventorySelected[i],x+20+(i*40),y+20);
   
        if (global.inventory[i] != -1) {
       
            draw_sprite(sInvBI,global.inventory[i],x+20+(i*40),y+20);
       
        }
   
    }

}
 
Can you explain what your code does? I am trying to figure it out. The system that I have currently is that I have two arrays. One for the inventory like the tutorial I talked about. And one for if something is selected. Here is my code. I am happy to change the system I am currently using if I need to.
Code:
Create:

/*

    inventroy items and blocks
    -1 empty
    0 dirt
    1 grass
    2 sword
  
    inventory selected
    0 not selected
    1 selected

*/

//create global variables
globalvar invSize;
invSize = 5;

globalvar invShow;
invShow = true;

globalvar index;
index = 0;

//set arrays
for (var i = 0; i < invSize; i++) {

    global.inventory[i] = -1;
    global.inventorySelected[i] = 0;

}

global.inventorySelected[0] = 1;

Draw GUI:
//draw inventory
if (invShow) {

    for (var i = 0; i < invSize; i++) {
  
        draw_sprite(sInvBorder,global.inventorySelected[i],x+20+(i*40),y+20);
  
        if (global.inventory[i] != -1) {
      
            draw_sprite(sInvBI,global.inventory[i],x+20+(i*40),y+20);
      
        }
  
    }

}
The code I posted detects if the mouse wheel is being scrolled, if it is, then it changes the index of the inventory.

Add this code into the step event:

Code:
var prev_index = index;
if(mouse_wheel_up())
{
    if(global.index > (array_length_1d(global.inventory)-1))
    {
          index = 0;
    }
    else
    {
         index++;
    }
    global.inventorySelected[prev_index] = 0;
    global.inventorySelected[index] = 1;
}
if(mouse_wheel_down())
{
    if(index == 0)
    {
          index = (array_length_1d(global.inventory)-1);
    }
    else
    {
         index--;
    }
    
   global.inventorySelected[prev_index] = 0;
   global.inventorySelected[index] = 1;
}
 
W

WimpyLlama

Guest
Thank you so much. This works really well. The only problem is if you go all the way to the right then you have to scroll up two more to get to the left then you would normally.
 
Thank you so much. This works really well. The only problem is if you go all the way to the right then you have to scroll up two more to get to the left then you would normally.
I'm glad that it's working for you! :) If it isn't scrolling correctly to the right, then change the mouse wheel up condition to the below
Code:
if(mouse_wheel_up())
{
   if(global.index = (array_length_1d(global.inventory)-1))
   {
         index = 0;
   }
   else
   {
        index++;
   }
   global.inventorySelected[prev_index] = 0;
   global.inventorySelected[index] = 1;
}
If it still doesn't work, then try -2, the whole point is to teach you how to use arrays and draw them to the screen, if you want a specific one selected, you need to work between the min and max index of the array (0 to n).
 
Top