GMS 2.3+ Remove item from ds_map. I don't understand the script.

I'm making quick inventory slots and don't understand how I can remove an item. I am using a script, but I do not understand what to write in arg [slot]. Help me please.

GML:
global.inv_toolbar  = ds_map_create();

selected_slot = noone;

step:

var _key = string_digits(keyboard_lastchar);

if(_key != "") {

     var _slot_index = (real(_key) + 9) mod 10;

    selected_slot = slots[_slot_index];

    keyboard_lastchar = "";

}
oPlayer step:
GML:
var _key = obj_inv_panel_toolbar.selected_slot.key;
if(_key == "food_apple") && keyboard_check_pressed(vk_space)
global.playerHP +=1;
//removes from the inventory
scrItemRemove(global.inv_toolbar, "food_apple", 1, ????? ); // what to write here???
scrItemRemove
GML:
///@func    scrItemRemove(inv, key, amount, [slot])

///@desc    removes an item from the inventory in general, or from a specific slot

///@arg        {number} inv - inventory

///@arg        {string} key - the key of the item to remove (ignored if removing from a specific slot)

///@arg        {number} amount - number of items to remove. -1 removes all the items.

///@arg        {number} [slot] - (optional) the slot index where the items are removed from

///@return    {number} the amount of items actually removed (same as amount, given enough items)

function scrItemRemove() {



    var _inv = argument[0];

    var _slot = argument_count > 3 ? argument[3] : -1;



    var _inv_items = _inv[? "items"];



    //check if slot specified, and fetch key and amount

    var _key, _amount;

    if(_slot >= 0) {

        _key = _inv_items[# EX_COLS.key, _slot];

        _amount = argument[2] < 0 ? _inv_items[# EX_COLS.amount, _slot] : argument[2];

    }

    else {

        _key = argument[1];

        _amount = argument[2] < 0 ? scr_inv_count(_inv, _key) : argument[2];

    }



    //remove items

    var _count = _scr_fn_item_remove(_inv, _key, _amount, _slot);



    //fire notification

    scr_ev_notify(_inv, EX_EVENTS.inv_updated);



    //return items count

    return _count;





}
 

TailBit

Member
The description of the script says it is optional, so I guess if you don't specify a slot then it will find the first one that contains an apple
GML:
if keyboard_check_pressed(vk_space){
    var _key = obj_inv_panel_toolbar.selected_slot.key;
    
    // I would use a switch case here
    if(_key == "food_apple") global.playerHP +=1;
    
    scrItemRemove(global.inv_toolbar, _key, 1 );
}
 
I tried to do this. But I need to remove the item from the selected slot. Now deletes an apple even if it is not in the selected slot.
 

TailBit

Member
Jup, kinda expexted that .. then you need to remember the slot index as well:
GML:
// create:
global.inv_toolbar  = ds_map_create();
selected_slot = noone;
selected_index = -1;

// step:

var _key = string_digits(keyboard_lastchar);
if(_key != "") {
     var _slot_index = (real(_key) + 9) mod 10;
    selected_slot = slots[_slot_index];
    selected_index = _slot_index;
    keyboard_lastchar = "";
}
GML:
if keyboard_check_pressed(vk_space){
    var _key = obj_inv_panel_toolbar.selected_slot.key;
    
    // I would use a switch case here
    if(_key == "food_apple") global.playerHP +=1;
    
    scrItemRemove(global.inv_toolbar, _key, 1, obj_inv_panel_toolbar.selected_index);
}
 
Top