• Don't understand what is wrong with this class definition
    3 replies, posted
I am following the gmod wiki and the default lua class definition style like so, -- Inventory class Inventory = { } Inventory.__index = Inventory --[[ Constructor Args: Number fillCount, Number rows, Number cols Desc: Creates a new inventory class Return: Inventory ]] function Inventory:New( fillCount, rows, cols )     local this = { }      AccessorFunc( this, 'fillcount', 'FillCount', FORCE_NUMBER )     AccessorFunc( this, 'rows', 'RowCount', FORCE_NUMBER )     AccessorFunc( this, 'cols', 'ColumnCount', FORCE_NUMBER )     this.fillcount = fillCount or 0     this.rows = rows or 2     this.cols = cols or 6     for r = 1, this.rows do         this[r] = { }         for c = 1, this.cols do             this[r][c] = InvenSlot:New( '', '', 0 ) end     end     setmetatable( this, Inventory )     return this end --[[ Desc: Finds the next empty slot in the inventory Return: Number row, Number column ( false if full ) ]] function Inventory:FindNextEmptySlot()     for r = 1, self.rows do         for c = 1, self.cols do             if ( self[r][c]:GetItemCount() <= 0 ) then                 return r, c end         end     end     return false end --[[ Args: String classname, Number count Desc: Creates and inserts a new slot into the inventory ]] function Inventory:Insert( classname, count )     local r, c = self:Contains( classname )     if ( r ) then         self[r][c]:IncrementCount()     else         r = Inventory:FindNextEmptySlot()         if ( not r ) then LocalPlayer():ChatPrint( 'Inventory full' ) return end         local imagePath = 'materials/entities/' .. classname .. '.png'         self[r][c]:SetClass( classname )         self[r][c]:SetImage( imagePath )         self[r][c]:SetItemCount( count )     end end setmetatable( Inventory, { __call = Inventory.New } ) Creating an instance and calling insert: local inven = Inventory( 0 ) inven:Insert( 'fart', 1 ) Calling insert creates an error in the FindNextEmptySlot method: (self.rows is nil for some reason?) [ERROR] gamemodes/greyrp/gamemode/modules/inventory/client/c_inven.lua:36: 'for' limit must be a number   1. FindNextEmptySlot - gamemodes/greyrp/gamemode/modules/inventory/client/c_inven.lua:36    2. Insert - gamemodes/greyrp/gamemode/modules/inventory/client/c_inven.lua:72     3. unknown - gamemodes/greyrp/gamemode/modules/inventory/client/inventory.lua:10      4. include - [C]:-1       5. unknown - gamemodes/greyrp/gamemode/modules/inventory/init.lua:23        6. include - [C]:-1         7. unknown - gamemodes/greyrp/gamemode/cl_init.lua:14 However, when I PrintTable( inven ) it shows that .rows = 2 and .cols = 6 as intended...
You're calling `Inventory:FindNextEmptySlot()` instead of `self:FindNextEmptySlot()`
Damn, can't believe I missed that, thanks. What do you mean by doing the accessorfuncs on the meta table?
You made AccesorFunc on this table. Do this on Inventory
Sorry, you need to Log In to post a reply to this thread.