Temporary Inventories
On this page we dive into what "Temporary Inventories" are and what they can be used for.
Temporary Inventories are Inventories that are loaded during runtime when needed, and unloaded when no player is accessing the Inventory. For example this can be used for stashes. In this example we are going to make a police storage.
In order to use this, you need to have some experience with coding.
First we need to register a temporary inventory handler using RegisterTemporaryInventoryHandler. Temporary inventory handler are the code that is called when the police locker is accessed, in this code you can add checks & load the specific inventory that is needed.
local api = exports["bb_inventory"]:GetAPI()
api.RegisterTemporaryInventoryHandler("police_storage",
function(identifier, temporaryData, source)
-- Add any checks you'd like,
-- such as checking if the player has the right to access this storage
end)
After registering the temporary inventory handler, we can load the inventory by the identifier(If it exists) using LoadInventoryByIdentifier.
local api = exports["bb_inventory"]:GetAPI()
api.RegisterTemporaryInventoryHandler("police_storage",
function(identifier, temporaryData, source)
-- Add any checks you'd like,
-- such as checking if the player has the right to access this storage
local inventory = api.LoadInventoryByIdentifier(identifier)
if (inventory) then
return inventory
end
end)
Now, if the inventory does not exist, we create a new one using CreateInventory.
The third parameter of CreateInventory is 'container', make sure the container is registered in your configs/container.shared.lua. For example:
["police_storage"] = {
sizeX = 6,
sizeY = 6,
maxWeight = 40000 -- Also can be null
}
local api = exports["bb_inventory"]:GetAPI()
api.RegisterTemporaryInventoryHandler("police_storage",
function(identifier, temporaryData, source)
-- Add any checks you'd like,
-- such as checking if the player has the right to access this storage
local inventory = api.LoadInventoryByIdentifier(identifier)
if (inventory) then
return inventory
end
-- If the inventory does not exist, we create a new one
return api.CreateInventory(identifier, "STASH", "police_storage", "police_storage", "police_storage",
true)
end)
Now, all the server-side part is done. We are now going to access the inventory on the client-side. Thats quite easy, we only need to make one API call, thats AccessInventory.
local api = exports["bb_inventory"]:GetAPI()
-- Everything in front of the colon is the 'identifierHandle',
-- which is the one that is needed at RegisterTemporaryInventoryHandler
-- The second parameter is the temporaryData, which can be nil if you don't need it
-- It can be used to pass any data you want to the inventory handler
-- The third and fourth parameters are the title and icon of the inventory,
-- which are temporary until the title & icon is fetched from the database
-- which can also be nil if you don't need them
api.AccessInventory("police_storage:5", nil, "police_storage", "police_storage")
Last updated