Inventory API Intro - rebel1324/NutScript GitHub Wiki

Inventory Guide

In NutScript, an inventory is an object that contains items. This guide will go over concepts for creating an inventory. The result of this guide should be an inventory with a maximum capacity.

The reader is assumed to have moderate knowledge of Lua and to understand basic object-oriented programming principles.

Inventory Types

NutScript provides methods to define different types of inventories using a common interface. This makes it easy to create custom inventory types with ease. For example, the grid inventory plugin included with NutScript uses the Inventory API to create a grid inventory. Grid inventories allow items to have a width and height, which form a rectangles that take up space in a 2D grid.

Introduction

An inventory type or inventory class is a blueprint for an inventory. It defines how an inventory of the particular type behaves and what data it holds. So, an inventory instance is an actual inventory that is of a certain type. For example, the grid inventory class contains the logic for how an instance of a grid inventory behaves. For more information, see this post on Object Oriented Programming.

Setup

If you are creating a new inventory type, it should be created in a plugin. This makes it easy to add new features without modifying too many files. To start, go to your schema's plugins folder and create a new folder called myinventory. Inside the myinventory folder, create a file named sh_plugin.lua. The file should contain the following:

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

Creating an Inventory Class

The Inventory API comes with a base class which can be extended to add additional features. TIt is available in a global variable named nut.Inventory. The nut.Inventory class contains some basic boilerplate code for an inventory and should be used as a starting point for a new inventory type. To create a new inventory type, we extend the nut.Inventory. This can be done by writing

local MyInventory = nut.Inventory:extend("MyInventory")

The extend method takes one string parameter, which is the name of the class. It returns a new inventory class which one can overwrite methods in to add new functionality. Note that MyInventory can also be extended later to add even more functionality on top of whatever additional functionality it has. This can be done by using

local MyOtherInventory = MyInventory:extend("MyOtherInventory")

Note: The members of the inventory you extend can be accessed within the newly created inventory using the BaseClass member. For example, with the MyInventory object, you can use MyInventory.BaseClass.getType to access the original getType method for an inventory. This is useful if you really do just want to add additional functionality, rather than completely overwriting something of the nut.Inventory class.

Registering

After creating the class, you can overwrite any methods of the inventory class or add more members to store some additional data. But, after all of that, the inventory must be registered before use so NutScript understands your new inventory type. The inventory class contains a register method which takes one string as a parameter. This string is a unique identifier for the inventory type. It should be short and be alphanumeric. To register the MyInventory class, write

MyInventory:register("myinv")

So, the sh_plugin.lua file should contain

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

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

When the plugin runs, it will create a new inventory type (which does not do much besides hold items) identified by "myinv".

In the next part, we will go over how to use our inventory class to create inventories.