SOLVED Get/Do not get items

uni00

Member
Hello. I created an inventory that contains up to 4 items, referring to the blog here. Pressing the Enter key on an item removes it from the field and puts it in your inventory. Now, let's assume that four items are filled. This script finds a place where you can put an item and returns true if you put one. Returns false if there is no place to enter. Until now, I started instance_destroy(); just by pressing the enter key, so the item disappeared regardless of whether it was stored. However, if you can pick up false and true in the script, the item will not disappear if you do not press the Enter key to enter. What should I do to pick up this judgment? I intend to invoke instance_destroy(); if true.
thanks!


GML:
-o_player_create--
global.inv = ds_grid_create(4,2);
ds_grid_clear(global.inv,0);
GML:
--script--

/// @description Adds an item and a quantity into the inventory in a valid slot.
/// @function scr_gain_item(item_ID, amount);
/// @param item_ID
/// @param amount


var iid = argument0;
var amount = argument1;


var slot = 0; //A temporary variable to loop through the slots
var inventory_width = ds_grid_width(global.inv);

while (slot < inventory_width)
{
if (global.inv[# slot, 0] == iid || global.inv[# slot, 0] == item.none)
{
  global.inv[# slot, 0] = iid;
  global.inv[# slot, 1] += amount;
  return true; //Did set the slot (return/exit)
  }
slot ++;
}
return false; //Did not set slot
GML:
--item_collision(item and o_player)--

if(keyboard_check_pressed(vk_enter)){
instance_destroy();
}
GML:
-item_destroy_event-
scr_gain_item(item.draw_set,1);
 

Roldy

Member
Hello. I created an inventory that contains up to 4 items, referring to the blog here. Pressing the Enter key on an item removes it from the field and puts it in your inventory. Now, let's assume that four items are filled. This script finds a place where you can put an item and returns true if you put one. Returns false if there is no place to enter. Until now, I started instance_destroy(); just by pressing the enter key, so the item disappeared regardless of whether it was stored. However, if you can pick up false and true in the script, the item will not disappear if you do not press the Enter key to enter. What should I do to pick up this judgment? I intend to invoke instance_destroy(); if true.
thanks!


GML:
-o_player_create--
global.inv = ds_grid_create(4,2);
ds_grid_clear(global.inv,0);
GML:
--script--

/// @description Adds an item and a quantity into the inventory in a valid slot.
/// @function scr_gain_item(item_ID, amount);
/// @param item_ID
/// @param amount


var iid = argument0;
var amount = argument1;


var slot = 0; //A temporary variable to loop through the slots
var inventory_width = ds_grid_width(global.inv);

while (slot < inventory_width)
{
if (global.inv[# slot, 0] == iid || global.inv[# slot, 0] == item.none)
{
  global.inv[# slot, 0] = iid;
  global.inv[# slot, 1] += amount;
  return true; //Did set the slot (return/exit)
  }
slot ++;
}
return false; //Did not set slot
GML:
--item_collision(item and o_player)--

if(keyboard_check_pressed(vk_enter)){
instance_destroy();
}
GML:
-item_destroy_event-
scr_gain_item(item.draw_set,1);

Can you just put scr_gain_item in the collision event?

GML:
--item_collision(item and o_player)--

if(keyboard_check_pressed(vk_enter)){

    // Only destroy the item instance if it is picked up
    if (scr_gain_item(item.draw_set, 1) == true) {
        instance_destroy();
    }
    
}
 

uni00

Member
Can you just put scr_gain_item in the collision event?

GML:
--item_collision(item and o_player)--

if(keyboard_check_pressed(vk_enter)){

    // Only destroy the item instance if it is picked up
    if (scr_gain_item(item.draw_set, 1) == true) {
        instance_destroy();
    }
   
}
Wow! Thank you! Thanks to you, it was possible! !!
 

Roldy

Member
Wow! Thank you! Thanks to you, it was possible! !!
By the way I don't know what you intend but your script might have a bug in it.

Imagine you inventory is currently:

  • item.mirror
  • item.draw_set
  • item.none
  • item.none

then you drop the mirror and pick up a draw_set so the inventory is:

  • item.draw_set // if when drop the slot is set to item.none
  • item.draw_set
  • item.none
  • item.none

instead of:

  • item.none
  • item.draw_set x 2
  • item.none
  • item.none

like I think you might intend.
 

uni00

Member
By the way I don't know what you intend but your script might have a bug in it.

Imagine you inventory is currently:

  • item.mirror
  • item.draw_set
  • item.none
  • item.none

then you drop the mirror and pick up a draw_set so the inventory is:

  • item.draw_set // if when drop the slot is set to item.none
  • item.draw_set
  • item.none
  • item.none

instead of:

  • item.none
  • item.draw_set x 2
  • item.none
  • item.none

like I think you might intend.
Thank you. I haven't created a script yet, so I'll create one
 

uni00

Member
By the way I don't know what you intend but your script might have a bug in it.

Imagine you inventory is currently:

  • item.mirror
  • item.draw_set
  • item.none
  • item.none

then you drop the mirror and pick up a draw_set so the inventory is:

  • item.draw_set // if when drop the slot is set to item.none
  • item.draw_set
  • item.none
  • item.none

instead of:

  • item.none
  • item.draw_set x 2
  • item.none
  • item.none

like I think you might intend.
Wow... I just created and ran the drop script and surely draw used two boxes. What should I do. .. .. ..
 

TailBit

Member
One loop to find slots with the same type of item that isn't filled up to its max stack

If there are still items left to place after filling the stacks, then start putting the rest in empty slots.
 
Top