Kartik Scripts

QB Inventory

Kartik-Business integrates with qb-inventory, allowing business items (food, ingredients, products, etc.) to be dynamically registered at runtime.

Unlike other integrations, for qb-inventory, the core logic has been centralized in qb-core to ensure system-wide compatibility.


๐Ÿ“ฆ Requirements

Before proceeding, ensure the following dependencies are installed and running:

  • qb-inventory
  • qb-core
  • oxmysql
  • Kartik-Business
  • kartik-laptop

๐Ÿ”— How the Integration Works

When Kartik-Business needs to register a new item:

  • It triggers a server event that is handled by qb-core.
  • qb-core appends the item to qb-core/shared/items.lua for persistence.
  • qb-core updates the internal QBCore.Shared.Items table for instant availability.
  • If an image URL is provided, it is downloaded and saved to qb-inventory/html/images/.

This process ensures that items created through the business system are recognized by all QBCore scripts immediately.


๐Ÿ“Œ Server Event: Insert Item

Kartik-Business uses the following event to register items:

TriggerServerEvent("kartik-business:inventory:insertItem", itemData)

Item Data Format

{
    name = "dirty_burger",
    label = "Dirty Burger",
    weight = 500,
    unique = false,
    useable = true,
    shouldClose = true,
    description = "A burger that looks a bit suspicious.",
    image = "https://example.com/images/dirty_burger.png" -- Optional image URL
}

๐Ÿ› ๏ธ Required QB Inventory Modification

To allow dynamic item registration, you can add the integration logic to your inventory script.

๐Ÿ“ File to Edit

qb-inventory/server/open_server.lua

๐Ÿ“Œ Implementation

Add the following event handler to the end of the file. This version appends the item to qb-core and downloads the image to the inventory's UI folder.

RegisterNetEvent("kartik-business:inventory:insertItem", function(item)
    if not item or not item.name or not item.label then return end

    local formatName = item.name:gsub("'", ""):lower()
    if QBCore.Shared.Items[formatName] then return end

    local inventoryPath = GetResourcePath('qb-inventory')
    local corePath = GetResourcePath('qb-core')

    -- Values for qb-core item format
    local weight = item.weight or 1000
    local unique = item.unique or false
    local useable = item.useable or false
    local shouldClose = item.shouldClose ~= nil and item.shouldClose or true
    local description = item.description or ""
    local image = formatName .. ".png"

    -- Format for qb-core/shared/items.lua
    local itemFormat = string.format("    ['%s'] = { name = '%s', label = '%s', weight = %d, type = 'item', image = '%s', unique = %s, useable = %s, shouldClose = %s, description = '%s' },\n",
        formatName, formatName, item.label:gsub("'", "\\'"), weight, image, tostring(unique), tostring(useable), tostring(shouldClose), description:gsub("'", "\\'"))

    -- Read items.lua from qb-core
    local fileContent = LoadResourceFile('qb-core', 'shared/items.lua')
    if fileContent then
        local lastBraceIndex = fileContent:find('}%s*$')
        if lastBraceIndex then
            local newContent = fileContent:sub(1, lastBraceIndex - 1) .. itemFormat .. fileContent:sub(lastBraceIndex)
            SaveResourceFile('qb-core', 'shared/items.lua', newContent, -1)
            
            -- Update memory
            QBCore.Shared.Items[formatName] = { 
                name = formatName, 
                label = item.label, 
                weight = weight, 
                type = 'item', 
                image = image, 
                unique = unique, 
                useable = useable, 
                shouldClose = shouldClose, 
                description = description 
            }
        end
    end

    -- Handle image download
    if item.image then
        PerformHttpRequest(item.image, function(code, data)
            if code >= 200 and code < 300 then
                local imagePath = inventoryPath .. '/html/images/' .. formatName .. '.png'
                local file = io.open(imagePath, "wb")
                if file then
                    file:write(data)
                    file:flush()
                    file:close()
                end
            end
        end)
    end
end)

โš ๏ธ Important Notes

  • Persistence: Items are written to the physical items.lua file.
  • Instant Use: No server restart or resource restart is required.
  • Image Location: Images are saved automatically to:
    qb-inventory/html/images/
  • Uniqueness: If an item with the same name already exists in QBCore.Shared.Items, the insertion is skipped.

โœ… Integration Complete

With this setup, Kartik-Business and qb-inventory are fully synced. New items created via the business app will be immediately available in the inventory and across the entire server.

On this page