Inventory Overwriting Methods - rebel1324/NutScript GitHub Wiki

Overwriting Methods

In the previous part, we went over how to create instances of our MyInventory inventory and to use some methods of the Inventory class. In particular, we used the add method. However, using this method can be annoying as we need to instantiate an item, then call the method with the new item instance every time we want to add an item to our inventory.

In this part, we will go over how to overwrite methods in our MyInventory class so we can add an item to our inventory given its string item type.

As before, we have the following code for our MyInventory class:

PLUGIN.name = "My Inventory"
PLUGIN.author = "You"
PLUGIN.desc = "Creates a new inventory type."

local MyInventory = nut.Inventory:extend("MyOtherInventory")
MyInventory:register("myinv")

Note that MyInventory is just a table. So, we just need to define an add method that takes a string as the first argument. So, we can add the following code before registering our inventory class:

function MyInventory:add(itemType)
  -- TODO: implement
end

Like before, we can instantiate an item given the itemType string. So, we have:

function MyInventory:add(itemType)
  return nut.item.instance(itemType)
    :next(function(item)
      -- TODO: add `item` to the inventory
    end)
end

But, how can we add the item to the inventory?

Inherited Members

Whenever you use the extend method, the new class you get will have a BaseClass member that is a reference to the parent class. So, MyInventory.BaseClass.add will refer to the add method of the parent class. So, we can call that base class's add method to add the item we just created.

function MyInventory:add(itemType)
  return nut.item.instance(itemType)
    :next(function(item)
      -- Note we do not use the colon syntax since we do not want to
      -- pass in self.BaseClass as the first argument!
      return self.BaseClass.add(self, item)
    end)
end

This is useful if you really do want to extend functionality, rather than completely changing functionality.

Note: Part of the framework relies on the add method that has an item instance as its first argument. So, we should make the following patch to the above method:

function MyInventory:add(itemInstanceOrType)
  -- If the first argument is an item, just delegate to the original `add` method.
  if (nut.item.isItem(itemInstanceOrType)) then
    return self.BaseClass.add(self, itemInstanceOrType)
  end
  local itemType = itemInstanceOrType

  return nut.item.instance(itemType)
    :next(function(item)
      -- Note we do not use the colon syntax since we do not want to
      -- pass in self.BaseClass as the first argument!
      return self.BaseClass.add(self, item)
    end)
end

Testing

Now, you can try creating an instance of the MyInventory class and call the add method with a string as the first argument. For example, if you have a "water" item, you would use inventory:add("water") where inventory is an instance of MyInventory.

Next

In the next part, we will limit how many items can be added to our inventory using access rules.