HELP WITH JSON SAVE FILE

Hi everyone and sorry if i write bad some words, but im not english.
So, my problem is that i have a code that i buy in yoyo market for GMS2 and i insert in my game, the problem is when i want to load the game or save it.
The inventory (is the code that i buy) doesnt save it.
I use .ini to save my game, like that:
GML:
ini_open(working_directory + "save.ini");
ini_write_real("Object4", "rm", room);
ini_write_real("Object4", "x", Object4.x);
ini_write_real("Object4", "y", Object4.y);
ini_close();
show_debug_message("JUEGO GUARDADO");
and for load it i used:
Code:
if file_exists(working_directory + "save.ini") {
    ini_open(working_directory + "save.ini");
    var rm = ini_read_real("Object4", "rm", 0);
    var px = ini_read_real("Object4", "x", 0);
    var py = ini_read_real("Object4", "y", 0);
    ini_close();
    
    if (room_exists(rm) )
    {
        game_load("save.ini");
        if (room != rm) room_goto(rm);
But, i buy a inventory sistem and have that code to write the object that are inside, but idk how to save it, i try to make a json but always that i try it give me error.

Code:
if not global.DisplayInventory exit
    
draw_set_font(InventoryFont)

//draw all boxes and what's in them
    
for (var _BoxRow = 0; _BoxRow < Height; _BoxRow++) {
        
    for (var _BoxNo = 0; _BoxNo < Width; _BoxNo++) {
            
        var _BoxX = XPos + (_BoxNo * BoxSize)
        var _BoxY = YPos + (_BoxRow * BoxSize)           
        draw_sprite(s_InventoryBox, Box[# _BoxNo, _BoxRow], _BoxX, _BoxY)
            
        if ItemCount[# _BoxNo, _BoxRow] > 1 {
            draw_set_colour(c_white)
            draw_set_halign(fa_right)
            draw_set_valign(fa_bottom)
            draw_text(_BoxX + 60, _BoxY + 60, ItemCount[# _BoxNo, _BoxRow])
        }   
    }   
}
        
        
//draw indication sprite

if GridX < Width and GridX >= 0 {
    if GridY < Height and GridY >= 0 {

        var _BoxX = XPos + (GridX * BoxSize)
        var _BoxY = YPos + (GridY * BoxSize)       
        draw_sprite(s_InventoryBoxSelected, 0, _BoxX, _BoxY)   
    }
}


    
//Description   
if Box[# GridX, GridY] != Item.None {
        
    draw_set_halign(fa_left)
    draw_set_valign(fa_top)
    draw_text(XPos, YPos + (Height*BoxSize) + 5, Description[Box[# GridX, GridY]])
            
}

//EquippedItem

draw_sprite_ext(s_InventoryBox, EquippedItem, EquippedItemX, EquippedItemY, 2, 2, 0, c_white, 1)

if GridX == EquippedItemX {
    
    draw_sprite_ext(s_InventoryBoxSelected, 0, EquippedItemX, EquippedItemY, 2, 2, 0, c_white, 1)   

}

if SelectedItem1 == EquippedItem and EquippedItem!= Item.None {
    
    draw_sprite_ext(s_InventoryBoxSelected, 1, EquippedItemX, EquippedItemY, 2, 2, 0, c_white, 1)   
    
}

//selecting items and crafting

#region
    
if SelectedItem1 != Item.None {
        
    
    _BoxX = XPos + (SelectX1 * BoxSize)
    _BoxY = YPos + (SelectY1 * BoxSize)
    draw_sprite(s_InventoryBoxSelected, 1, _BoxX, _BoxY) //frame around selected Item 1

    
    _BoxX = XPos
    _BoxY = YPos + (Height* BoxSize) + BoxSize
    draw_sprite(s_InventoryBox, Box[# SelectX1, SelectY1], _BoxX, _BoxY) //draw selected item 1


    if SelectedItem2 != Item.None {
        
        _BoxX = XPos + (SelectX2 * BoxSize)
        _BoxY = YPos + (SelectY2 * BoxSize)
        draw_sprite(s_InventoryBoxSelected, 2, _BoxX, _BoxY) //frame around selected item 2
    
        _BoxX = XPos + BoxSize
        _BoxY = YPos + (Height* BoxSize) + BoxSize
        draw_sprite(s_InventorySymbols, 0, _BoxX, _BoxY) //plus sign
    
    
        _BoxX = XPos + (BoxSize*2)
        _BoxY = YPos + (Height* BoxSize) + BoxSize

        draw_sprite(s_InventoryBox, Box[# SelectX2, SelectY2], _BoxX, _BoxY) //selecteditem 2
    
        _BoxX = XPos + (BoxSize*3)
        _BoxY = YPos + (Height* BoxSize) + BoxSize
        draw_sprite(s_InventorySymbols, 1, _BoxX, _BoxY) //equals sign
        
        _BoxX = XPos + (BoxSize*4)
        _BoxY = YPos + (Height* BoxSize) + BoxSize
        draw_sprite(s_InventoryBox, Combination[SelectedItem1, SelectedItem2], _BoxX, _BoxY) //result item
        
        if Combination[SelectedItem1, SelectedItem2] != Item.None {
            
            draw_sprite(s_CombineButton, 0, CombineBoxX, CombineBoxY) //combinebox
            draw_set_halign(fa_left)
            draw_set_valign(fa_top)
            draw_text(CombineBoxX + 4, CombineBoxY, "combinar")
        
        }
        
        //detects mouse hovering over "combine" button
        if device_mouse_x_to_gui(0) >= CombineBoxX and device_mouse_x_to_gui(0) <= (CombineBoxX+sprite_get_width(s_CombineButton)) {       
            if device_mouse_y_to_gui(0) >= CombineBoxY and device_mouse_y_to_gui(0) <= (CombineBoxY+sprite_get_height(s_CombineButton)) {               
                draw_sprite(s_CombineButton, 1, CombineBoxX, CombineBoxY)         
            }
        }
                    
        if GridX = CombineBoxX and GridY = CombineBoxY {
            
            draw_sprite(s_CombineButton, 1, CombineBoxX, CombineBoxY) //Combinationbox selected       
        }       
    }   
}
#endregion
and for step:
Code:
//toggle inventory and reset variables
#region

if keyboard_check_pressed(ShowInventoryButton) {
    
    if global.DisplayInventory == false {
        global.DisplayInventory = true
        //reset selected items
        SelectedItem1 = Item.None
        SelectedNumber1 = 0
        SelectedItem2 = Item.None
        SelectedNumber2 = 0
        GridX = 0
        GridY = 0
        OldGridX = -1
        OldGridY = -1
        ReadyToCombine = false
        
    } else global.DisplayInventory = false
}

if not global.DisplayInventory exit


if SelectedItem1 != Item.None and SelectedItem2 != Item.None {
    
    if Combination[SelectedItem1, SelectedItem2] != Item.None {
        
        ReadyToCombine = true       
    }
} else ReadyToCombine = false


#endregion
        
//__________________Mouse controls       

#region
if mouse_check_button_pressed(mb_left) {
    
    var _MouseX = device_mouse_x_to_gui(0)
    var _MouseY = device_mouse_y_to_gui(0)
    
    if _MouseX < XPos + (Width * BoxSize) and _MouseX >= XPos {
        
        if _MouseY < YPos + (Height * BoxSize) and _MouseY >= YPos {
    
            GridX = X_To_Grid_X(_MouseX)
            GridY = Y_To_Grid_Y(_MouseY)
    
            var _BoxClickedOn = Box[# GridX, GridY]
            var _NumberClickedOn = ItemCount[# GridX, GridY]
            
                
            if SelectedItem1 != Item.None {//if item 1 is selected
                
                    if SelectedItem2 != Item.None { //if item1 is selected and item 2 as well
                            
                            
                            switch (_BoxClickedOn) {
                                    
                                case SelectedItem1:
                                    
                                    SelectedItem2 = Item.None
                                    SelectedNumber2 = 0
                                    
                                break
                                    
                                case SelectedItem2:
                                        
                                    SelectedItem2 = Item.None
                                    SelectedNumber2 = 0
                                
                                break
                                
                                default:
                                    SelectedItem2 = _BoxClickedOn
                                    SelectedNumber2 = _NumberClickedOn
                                    SelectX2 = GridX
                                    SelectY2 = GridY
                                break
                            }   
                                
                        
                    } else { //if item 2 is not selected
                            
                        switch (_BoxClickedOn) {
                                
                            case Item.None:
                            
                                if EquippedItem != Item.None {
                                    if OldGridX == EquippedItemX {
                                        EquippedItem = Item.None
                                    }
                                }
                                
                                if GridX == OldGridX and GridY == OldGridY {
                                //if we click on the same box again
                                    
                                    //move item1 to an empty slot we clicked on
                                    Box[# GridX, GridY] = SelectedItem1
                                    ItemCount[# GridX, GridY] = SelectedNumber1
        
                                    //old box should become empty
                                    Box[# SelectX1, SelectY1] = Item.None
                                    ItemCount[# SelectX1, SelectY1] = 0
                                
                                    //we have nothing selected anymore
                                    SelectedItem1 = Item.None
                                    SelectedNumber1 = 0
                                    
                                }
                            
                            break
                            
                            case SelectedItem1: 
                                
                                //unselect item1 if we click on it again
                                SelectedItem1 = Item.None
                                SelectedNumber1 = 0
                            
                            break
                            
                            default:
                            
                                SelectedItem2 = _BoxClickedOn
                                SelectedNumber2 = _NumberClickedOn
                                SelectX2 = GridX
                                SelectY2 = GridY
                            
                            break
                    
                        }                   
                    }
                        
                        
                } else {  //when we don't have anything selected
                    
                    if _BoxClickedOn == Box[# OldGridX, OldGridY] {
                        //if we click on the same box again
                
                        SelectedItem1 = _BoxClickedOn
                        SelectedNumber1 = _NumberClickedOn

                        SelectX1 = GridX
                        SelectY1 = GridY
                    
                    }                                   
                }
                
            //save what we clicked on before for next use           
            OldGridX = GridX
            OldGridY = GridY
                    
        }
    }
    
    if _MouseX >= CombineBoxX and _MouseX <= (CombineBoxX+sprite_get_width(s_CombineButton)) {
        
        if _MouseY >= CombineBoxY and _MouseY <= (CombineBoxY+sprite_get_height(s_CombineButton)) {
            
            Combine_Items(SelectedItem1, SelectedItem2)
        
        }
    }
    
    if _MouseX >= EquippedItemX and _MouseX <= (EquippedItemX+(BoxSize*2)) {
        
        if _MouseY >= EquippedItemY and _MouseY <= (EquippedItemY+(BoxSize*2)) {
            
            GridX = EquippedItemX
            GridY = EquippedItemY
            
            if OldGridX == EquippedItemX {
                
                //check for the equipped item in inventory
                for (var _y = 0; _y < Height; _y++) {   
                    for (var _x = 0; _x < Width; _x++) {       
                        if Box[# _x, _y] == EquippedItem {
                            SelectedNumber1 = ItemCount[# _x, _y]
                            SelectedItem1 = Box[# _x, _y]
                            SelectX1 = _x
                            SelectY1 = _y
                        }
                    }
                }
                
                SelectedItem2 = Item.None
                SelectedNumber2 = 0
                
                
            } else {
            
                if SelectedItem1 != Item.None {
                
                    EquippedItem = SelectedItem1
                    SelectedItem1 = Item.None
                    SelectedNumber1 = 0
                    SelectedItem2 = Item.None
                    SelectedNumber2 = 0
                
                }
            }
            

            OldGridX = GridX
            OldGridY = GridY
                
        
        }
    }
    
}
#endregion

//_________________Keyboard controls

#region

//navigating
#region
if keyboard_check_pressed(RightKey) {
    
    GridX += 1
    
    if GridX > Width-1 {
        
        if GridX == EquippedItemX+1 {
            GridX = 0
            GridY = 0
        }
        if GridX == Width {
            GridX = EquippedItemX
            GridY = EquippedItemY
        }
        if GridX == CombineBoxX+1 {
            GridX = 0
            GridY = 0
        }
    }
}
            
if keyboard_check_pressed(LeftKey) {
    
    GridX -= 1
                
    if GridX == EquippedItemX-1 {
        GridX = Width-1
        GridY = 0
    }
    if GridX < 0 {
        GridX = EquippedItemX
        GridY = EquippedItemY
    }
    if GridX == CombineBoxX-1 {
        GridX = Width-1
        GridY = Height-1
    }   
}   

if keyboard_check_pressed(UpKey) {
        
    GridY -= 1
        
    if ReadyToCombine {
        if GridY == CombineBoxY-1 {
            GridY = Height-1
            GridX = Width-1
        }
        if GridY < 0 {
            GridY = CombineBoxY
            GridX = CombineBoxX
        }
    } else {
        if GridY < 0 {
            GridY = Height-1
        }
    }   
}

if keyboard_check_pressed(DownKey) {
        
    GridY += 1
    
    if GridY > Height-1 {
        if ReadyToCombine {
            if GridY == CombineBoxY+1 {
                GridY = 0
                GridX = Width-1
            } else {
                GridY = CombineBoxY
                GridX = CombineBoxX
            }
        } else {
            GridY = 0
        }
    }   
}
#endregion

//selecting
#region

if keyboard_check_pressed(SelectKey) {
    
    
    if GridX == CombineBoxX and GridY == CombineBoxY { //___position at combine box
        
        Combine_Items(SelectedItem1, SelectedItem2)
        
        if SelectedItem1 == Item.None or SelectedItem2 == Item.None {
            
            GridX = 0
            GridY = 0
            
        }   
        
    } else if GridX == EquippedItemX { //___position at equipped item       
            
        if SelectedItem1 == Item.None {
                
            if EquippedItem != Item.None { //if there is an equipped item
                
                SelectedItem1 = EquippedItem
                
                //check for the equipped item in inventory
                for (var _y = 0; _y < Height; _y++) {   
                    for (var _x = 0; _x < Width; _x++) {       
                        if Box[# _x, _y] == EquippedItem {
                            SelectedNumber1 = ItemCount[# _x, _y]
                            SelectX1 = _x
                            SelectY1 = _y
                        }
                    }
                }
                    
                SelectedItem2 = Item.None
                SelectedNumber2 = 0   
            }
                

        } else {
                
            if SelectedItem1 == EquippedItem {
                    
                SelectedItem1 = Item.None
                SelectedNumber1 = 0
                    
            } else {
                EquippedItem = SelectedItem1
                SelectedItem1 = Item.None
                SelectedNumber1 = 0
                
                SelectedItem2 = Item.None
                SelectedNumber2 = 0   
            }
        }
        
        
    } else { //________position in the grid
    
    
        var _BoxClickedOn = Box[# GridX, GridY]
        var _NumberClickedOn = ItemCount[# GridX, GridY]
            
        if SelectedItem1 != Item.None {//if item 1 is selected
                
            if SelectedItem2 != Item.None { //if item1 is selected and item 2 as well
                                                        
                switch (_BoxClickedOn) {
                                    
                    case SelectedItem1:
                                    
                        SelectedItem2 = Item.None
                        SelectedNumber2 = 0
                                    
                    break
                                    
                    case SelectedItem2:
                                        
                        SelectedItem2 = Item.None
                        SelectedNumber2 = 0
                                
                    break
                    
                    case Item.None:
                        
                        if OldGridX == CombineBoxX {                           
                            //we have nothing selected anymore
                            SelectedItem1 = Item.None
                            SelectedNumber1 = 0
                            SelectedItem2 = Item.None
                            SelectedNumber2 = 0
                        } else {
                            SelectedItem2 = Item.None
                            SelectedNumber2 = 0
                        }
                                                    
                    break
                                
                    default:
                        SelectedItem2 = _BoxClickedOn
                        SelectedNumber2 = _NumberClickedOn
                        SelectX2 = GridX
                        SelectY2 = GridY
                    
                        if Combination[SelectedItem1, SelectedItem2] != Item.None {
                            //go to combine button
                            GridX = CombineBoxX
                            GridY = CombineBoxY
                        }
                    
                    break
                }   
                                
                        
            } else { //if item 2 is not selected but item 1 is selected
                            
                switch (_BoxClickedOn) {
                                
                    case Item.None:
                                    
                        if OldGridX == CombineBoxX {
                            
                            //we have nothing selected anymore
                            SelectedItem1 = Item.None
                            SelectedNumber1 = 0
                        } else {
                            
                            if SelectedItem1 == EquippedItem  and OldGridX == EquippedItemX {
                                
                                EquippedItem = Item.None
                                SelectedItem1 = Item.None
                                SelectedNumber1 = 0
                                
                            } else {
                                //move item1 to an empty slot we clicked on
                                Box[# GridX, GridY] = SelectedItem1
                                ItemCount[# GridX, GridY] = SelectedNumber1
        
                                //old box should become empty
                                Box[# SelectX1, SelectY1] = Item.None
                                ItemCount[# SelectX1, SelectY1] = 0
                                
                                //we have nothing selected anymore
                                SelectedItem1 = Item.None
                                SelectedNumber1 = 0
                            }
                        }
                            
                    break
                            
                    case SelectedItem1: 
                                
                        //unselect item1 if we click on it again
                        SelectedItem1 = Item.None
                        SelectedNumber1 = 0
                            
                    break
                            
                    default:
                            
                        SelectedItem2 = _BoxClickedOn
                        SelectedNumber2 = _NumberClickedOn
                        SelectX2 = GridX
                        SelectY2 = GridY
                    
                        if Combination[SelectedItem1, SelectedItem2] != Item.None {
                        
                            //go to combine button
                            GridX = CombineBoxX
                            GridY = CombineBoxY
                        }
                            
                    break
                    
                }
                    
            }
                        
                        
        } else {  //when we don't have anything selected
                                    
            SelectedItem1 = _BoxClickedOn
            SelectedNumber1 = _NumberClickedOn

            SelectX1 = GridX
            SelectY1 = GridY
                                                        
        }
                
        
    }
    
    //save what we clicked on before for next use           
    OldGridX = GridX
    OldGridY = GridY
            
}   

#endregion

#endregion
and the create:
Code:
//defining items, descriptions and combinations
#region

enum Item {
    
    None,
    Herb,
    Mushroom,
    Stick,
    Potion,
    Ring,
    Light
    
}

Description[0] = ""
Description[1] = "herb"
Description[2] = "glowing mushroom"
Description[3] = "stick"
Description[4] = "potion"
Description[5] = "magic ring"
Description[6] = "a bright shining light"


ItemAmount = array_length_1d(Description) // note: for this to work you have to add a description for every single item even if you don't want a description to be displayed.
                                            // otherwise just update this number when adding items

//creating all the combinations and filling with nothing
for (var _i1 = 0; _i1 <= ItemAmount; _i1++) {
    for (var _i2 = 0; _i2 <= ItemAmount; _i2++) {
        Combination[_i1, _i2] = Item.None
    }
}

//now here we create the combinations that we want
Combination[Item.Herb, Item.Mushroom] = Item.Potion
Combination[Item.Mushroom, Item.Stick] = Item.Light


//turns them all around so it will return the same item no matter which way round you selected them
for (var _i1 = 0; _i1 <= ItemAmount; _i1++) {
    for (var _i2 = 0; _i2 <= ItemAmount; _i2++) {
        Combination[_i1, _i2] = max(Combination[_i1, _i2], Combination[_i2, _i1])
    }
}
#endregion


//settings and variables
#region
depth = -10000 //makes sure to draw in front of everything else

global.DisplayInventory = false

ShowInventoryButton    = ord("I")

LeftKey                = ord("A")
RightKey            = ord("D")
UpKey                = ord("W")
DownKey                = ord("S")
SelectKey            = ord("E")

Width                = 6 //how many columns wide is the inventory
Height                = 5
BoxSize                = sprite_get_width(s_InventoryBox) //this just works for square inventory boxes

XPos                = 20 //the top left corner of the inventory
YPos                = 50

CombineBoxX            = XPos + (BoxSize*5) + 3 //where the combine Button is placed
CombineBoxY            = YPos + (Height* BoxSize) + BoxSize + (BoxSize - 22)

EquippedItemX        = XPos + (Width*BoxSize) + 80
EquippedItemY        = YPos

EquippedItem        = Item.None

InventoryFont        = f_Inventory
#endregion


//create the actual inventory
#region
//Inventory Slots
Box = ds_grid_create(Width, Height)

ds_grid_clear(Box, Item.None)

//Item Count
ItemCount = ds_grid_create(Width, Height)

ds_grid_clear(ItemCount, 0)

#endregion

//Examples
#region
Pickup_Item(Item.Mushroom)
Pickup_Item(Item.Mushroom)
Pickup_Item(Item.Mushroom)
Pickup_Item(Item.Mushroom)

Pickup_Item(Item.Herb)
Pickup_Item(Item.Herb)
Pickup_Item(Item.Herb)
Pickup_Item(Item.Herb)
Pickup_Item(Item.Herb)
Pickup_Item(Item.Herb)

Pickup_Item(Item.Stick)
Pickup_Item(Item.Ring)
Pickup_Item(Item.Stick)


Use_Item(Item.Herb)
Use_Item(Item.Ring)

Pickup_Item(Item.Ring)
#endregion
WELL if someone could help me im really appreciated
 

curato

Member
You can save an variable to a file the same way you did at the top of the post. Before you go too deep in it, you may want to consider contacting the person that put this on the market place for support. Perhaps, there is a save feature already, or they could point you as the variable that you need to save a reload to pull the inventory back in.
 
You can save an variable to a file the same way you did at the top of the post. Before you go too deep in it, you may want to consider contacting the person that put this on the market place for support. Perhaps, there is a save feature already, or they could point you as the variable that you need to save a reload to pull the inventory back in.
i try it, but no reply for that seller, 1 week stopping programming becouse i cant contact with the seller.
And i want to make a variable to save the inventory, the problem was when i put all the code like a variable to rechanche in game, crash it
 
Top