From faa8312074e6ea8a96efd70d6188ef592f81e579 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 4 May 2019 15:52:00 -0400 Subject: Updated tutorials Various fixes and updates for the tutorials --- tutorials/tut030_inventories.md | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'tutorials/tut030_inventories.md') diff --git a/tutorials/tut030_inventories.md b/tutorials/tut030_inventories.md index 41fb293..1ac45a0 100644 --- a/tutorials/tut030_inventories.md +++ b/tutorials/tut030_inventories.md @@ -6,23 +6,34 @@ Many gamemode bases try to build an inventory system into the gamemode, leaving In this tutorial, we'll build a simple rougelike inventory, where we expect items to have a "weight", and we can carry as many items as we want, as long as we don't exceed our "max weight". -Inventories and items in Artery are just tables, as per usual in lua. They have a few required fields each. On the left, under "Classes" (all the way at the bottom!), open invtbl and itemtbl in new tabs. +Inventories and items in Artery are just tables, as per usual in Lua. They have a few required fields each. On the left, under "Classes" (all the way at the bottom!), open invtbl and itemtbl in new tabs. invtbl contains a list of methods every inventory has, and itemtbl contains a list of methods every item has. ## A simple inventory You can see all the fields needed for an inventory, so let's get started. Recall that function tbl:func_name(one) + print(self.Name) ... end is the same as function tbl.func_name(self,one) + print(self.Name) ... end -lua will automatically create the variable "self" in the first example +Note the period `tbl.func_name` versus the colon `tbl:func_name`. +Lua will automatically create the variable "self" in the first example + +and that the function calls: + + sometable:dothing("blah") + +is the same as + + sometable.dothing(sometable,"blah") garrysmod/addons/artery_rougelite/data/artery/global/rougeinv.lua @@ -73,7 +84,7 @@ garrysmod/addons/artery_rougelite/data/artery/global/rougeinv.lua self.weight = self.weight + item.weight end - --This one is a bit complicated + --This one is a bit complicated, check if we have an item. --ptr can be either a string, or a function that takes 1 argument, -- and returns true when given an object we should return. --this function returns a POSITION that can be given back to get, remove, ect. @@ -124,8 +135,9 @@ garrysmod/addons/artery_rougelite/data/artery/global/rougeinv.lua --Re-creates this inventory from the data created by Serialize() function inv:DeSerialize(data) - local self_copy = table.Copy(self) local json = util.JSONToTable(data) + self.items = {} + self.weight = 0 for k,v in pairs(json) do local item_name = v[1] local item_data = v[2] @@ -135,7 +147,7 @@ garrysmod/addons/artery_rougelite/data/artery/global/rougeinv.lua for k,v in pair(self_copy.items) do self_copy.weight = self_copy.weight + v.weight end - return self_copy + return self end --Don't forget to register it with the gamemode! -- cgit v1.2.3-70-g09d2