-
Notifications
You must be signed in to change notification settings - Fork 31
Inventory Creating Instances
In this part of the tutorial, we will go over how to create and use inventory instances. Since all of this happens at runtime, you will need to actually start a Garry's Mod server. Furthermore, you will need to have access to the server console (either a listen server or using srcds) so you can use the lua_run
command. This will allow you to follow the examples in this part.
From the previous part, we have our own inventory class MyInventory
. With the MyInventory
class, we can actually create an inventory instance. To do so, we can use the instance
method. The instance
method returns a Promise
that resolves to the inventory instance. To try it out, we can add the following code to our plugin:
-- Only add the console command in the server state.
if (CLIENT) then return end
concommand.Add("makeinventory", function()
MyInventory:instance()
:next(function(inventory)
-- Save the instance to a global variable called myInstance.
myInstance = inventory
print(myInstance)
end)
end)
In the console, we can run the makeinventory
command. Something like MyInventory[1]
should print in the console.
Running the console command creates a global variable myInstance
, which we can interact with using the lua_run
command. You can view documentation for inventory methods here. For example, we can set some persistent data for the inventory.
lua_run myInstance:setData("foo", "bar")
> myInstance:setData("foo", "bar")...
lua_run myInstance:setData("baz", 123)
> myInstance:setData("baz", 123)...
lua_run print(myInstance:getData("foo"), myInstance:getData("baz"))
> print(myInstance:getData("foo"), myInstance:getData("baz"))...
bar 123
One major feature of inventories is storing items. The nut.Inventory
class comes with an add
method, which give an item instance, adds the item to the inventory. Keeping items in inventories is important as items are loaded with inventories and any interaction with items will probably involve inventories.
Recall that the nut.item.instance
function takes a string containing the item type and returns a promise that resolves to the item instance. So, if you have a "water" item, then you can create a new water item and add it to the inventory by running the following command:
lua_run nut.item.instance("water"):next(function(item) myInstance:add(item) end)
So, we create a water item instance. Once it has been created, we add the item instance to the inventory. We can check that the item has been added by running lua_run PrintTable(myInstance:getItems())
. Here is the output I got:
> PrintTable(myInstance:getItems())...
8:
data:
id = 8
invID = 6
Indeed, the myInstance
inventory has one item in it with ID 8.
lua_run print(nut.item.instances[8])
> print(nut.item.instances[8])...
item[water][8]
Moreover, it is a water item!
Inventories usually do not hold items forever. At some point in time, we might want to remove an item. To remove an item with an ID of x
, we can use the inventory's remove
method. From the previous example, I have a water item with ID 8. The remove
method takes in an ID of the item that we wish to remove. So, to remove the water item I added, I just run the following command:
lua_run myInstance:remove(8)
Afterwards, if I try to print all of the items again, I get the following output:
lua_run myInstance:remove(8)
> myInstance:remove(8)...
lua_run PrintTable(myInstance:getItems())
> PrintTable(myInstance:getItems())...
The output is blank as the myInstance
inventory has no items.
In the next part, we will go over overwriting inventory methods to make the MyInventory
class more useful.