• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Creating an Inventory using ds_maps

Hi everyone, I know that this is a vary wide topic that many people talk about, but I don't know how to do it. I've tried many tutorials and I couldn't get them to work... I want to make an inventory that has the ability to swap out items and able to stack items using ds_maps. I'm sure that this will require multiple ds_maps, and I want to make it where I can just use a script to add new items example: AddItem(ItemID, Sprite)
I've already tried this but it ended up not working... This is what I had before I came to you guys for help, maybe you can just find a way to fix it, because I don't know whats wrong with it.

IntInventory:
Code:
globalvar TotalInvSlots;
TotalInvSlots = argument0
tempgpselected = 1
tempmouseselected = 1
tempitemselected = -1
tempstackselected = -1
tempspriteselected = -1

Inventory = ds_map_create()
InventoryStack = ds_map_create()
InventoryItem = ds_map_create()

for(i=1; i<=TotalInvSlots; i+=1) {
ds_map_add(Inventory,i,0)
ds_map_add(InventoryStack,i,0)
}
AddInventoryItem:
Code:
///AddInventoryItem(ItemID,Sprite)

tempID = argument0
tempSprite = argument1
ds_map_add(InventoryItem, tempID, tempSprite)
Load:
Code:
///LoadInventory()
if(file_exists("PIN01.SAV") && file_exists("PIN02.SAV")) {
Inventory = ds_map_secure_load("PIN01.SAV")
InventoryStack = ds_map_secure_load("PIN02.SAV")
return(1)
} else {
return(-1)
}
Save:
Code:
///SaveInventory()

ds_map_secure_save(Inventory, "PIN01.SAV")
ds_map_secure_save(InventoryStack, "PIN02.SAV")

return(1)
DrawInventory:
Code:
///DrawInventorySlots(StartingX,StartingY,EndingX,EndingY)

tempx = argument0
tempy = argument1
tempxx = argument2
tempyy = argument3

xwidth = ((tempxx-tempx)-15)/8.5
ywidth = ((tempyy-tempy)-15)/8.5

drawx = tempx+5
drawy = tempy

//gamepad
if(gamepad_is_connected(0)) {
if(gamepad_button_check_pressed(0,gp_padl)) { if(tempgpselected > 1) {tempgpselected -= 1 } else { tempgpselected = TotalInvSlots } }
if(gamepad_button_check_pressed(0,gp_padr)) { if(tempgpselected < TotalInvSlots) { tempgpselected += 1 } else { tempgpselected = 1 } }
if(gamepad_button_check_pressed(0,gp_padu)) { if(tempgpselected > 8) {tempgpselected -= 8 } else { tempgpselected = round(tempgpselected/8) } }
if(gamepad_button_check_pressed(0,gp_padd)) { if(tempgpselected < TotalInvSlots-8) { tempgpselected += 8 } else { tempgpselected = round(tempgpselected/8) } }
tempmouseselected = 1
}

for(i = 1; i <= TotalInvSlots; i += 1) {
draw_set_alpha(0.2)
draw_rectangle(drawx,drawy,drawx+xwidth,drawy+ywidth,0)
if(sprite_exists(InventoryItemSprite(i))) {
tempsprite = InventoryItemSprite(i)
} else {
tempsprite = sprEmpty
}
draw_set_alpha(1)
draw_sprite_stretched(tempsprite,0,drawx,drawy,xwidth,ywidth)
draw_set_blend_mode(bm_subtract)
draw_text(drawx+(xwidth/2),drawy+(ywidth/2),string(ds_map_find_value(InventoryStack,i)))
draw_set_blend_mode(bm_normal)

if(gamepad_is_connected(0)) {
if(tempgpselected = i) {
draw_sprite_stretched(sprEmpty,1,drawx,drawy,xwidth,ywidth)
if(tempitemselected != -1) {
if(sprite_exists(InventoryItemSprite(tempitemselected))) {
draw_sprite_stretched(InventoryItemSprite(tempitemselected),1,drawx,drawy,xwidth,ywidth)
}
}
}
} else {
if(device_mouse_x_to_gui(0) > drawx && device_mouse_x_to_gui(0) < drawx+xwidth && device_mouse_y_to_gui(0) > drawy && device_mouse_y_to_gui(0) < drawy+ywidth) {
draw_sprite_stretched(sprEmpty,1,drawx,drawy,xwidth,ywidth)
tempmouseselected = i
}
if(tempitemselected != -1) {
if(sprite_exists(InventoryItemSprite(tempitemselected))) {
draw_sprite_stretched(InventoryItemSprite(tempitemselected),1,device_mouse_x_to_gui(0),device_mouse_y_to_gui(0),xwidth,ywidth)
}
}
}

drawx += xwidth+5
if(drawx+xwidth > tempxx) {
drawy += ywidth+5
drawx = tempx+5
}
}
InventoryItemSprite:
Code:
///InventoryItemSprite(Inventory Slot)
if(argument0 <= TotalInvSlots && argument0 > 0) {
tInvItem = ds_map_find_value(Inventory,argument0)
return(ds_map_find_value(InventoryItem,tInvItem))
} else {
return(-1)
}
SwapInventory:
Code:
///SwapInventory(Item1, Item2)

tItem1 = argument0
tItem2 = argument1

tItemNumber1 = ds_map_find_value(Inventory,argument0)
tItemStack1 = ds_map_find_value(InventoryStack,argument0)
tItemNumber2 = ds_map_find_value(Inventory,argument1)
tItemStack2 = ds_map_find_value(InventoryStack,argument0)

ds_map_replace(Inventory,argument0,tItemNumber2)
ds_map_replace(InventoryStack,argument0,tItemStack2)

ds_map_replace(Inventory,argument1,tItemNumber1)
ds_map_replace(InventoryStack,argument1,tItemNumber1)
InventorySelect:
Code:
///InventorySelect(slot)

tSlot = argument0

if(tempitemselected = -1) {
tempitemselected = ds_map_find_value(Inventory, tSlot)
tempstackselected = ds_map_find_value(InventoryStack, tSlot)
} else {
SwapInventory(tempitemselected, ds_map_find_value(Inventory, tSlot))
tempitemselected = -1
tempstackselected = -1
}
GiveItem:
Code:
///GivePlayerItem(ItemID,TotalItems)
tempItemID = argument0
tempTotalGiven = argument1

for(i=1;i<=TotalInvSlots;i+=1) {
if(ds_map_find_value(Inventory,i) = 0) {
ds_map_replace(Inventory,i,tempItemID)
ds_map_replace(InventoryStack,i,tempTotalGiven)
}
}
DestroyInventory:
Code:
///DestroyInventory();

ds_map_destroy(Inventory)
ds_map_destroy(InventoryStack)
ds_map_destroy(InventoryItem)
Create Event (in the test object):
Code:
//Inventory
IntInventory(64);
AddInventoryItem(0,sprEmpty)
AddInventoryItem(1,sprWeapon)
AddInventoryItem(2,sprBackPack)
AddInventoryItem(3,sprBasicBlock)
LoadInventory()
 
Top