From 83af51534bf16bf048aea1cd3b74a0308ed9dd71 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sun, 26 Nov 2017 21:07:54 -0500 Subject: Started work on writing tutorials Wrote tutorials for * Setup * Addon structure * Inventories * Items --- tutorials/tut042_too_many_items.md | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tutorials/tut042_too_many_items.md (limited to 'tutorials/tut042_too_many_items.md') diff --git a/tutorials/tut042_too_many_items.md b/tutorials/tut042_too_many_items.md new file mode 100644 index 0000000..bcea957 --- /dev/null +++ b/tutorials/tut042_too_many_items.md @@ -0,0 +1,43 @@ +# Tut 0x042 + +## Too many items + +In the last tutorial we saw how to create lots and lots of items. Sometimes, you think to yourself "that's way too many", and when you do, you simplify. Rougelike games have a tendency to have attributes associated with items. Commonly, "blessed", normal, and "cursed". In this tutorial, we'll be using the technique discussed in @{tut021_detouring.md} to give every item one of these stats by overrideing @{item.lua.RegisterItem} the Serialize() and DeSerialize() methods to give every item a little extra data. + + +garrysmod/addons/artery_rougelite/data/artery/global/item_attributes.txt + + local itm = nrequire("item.lua") + local log = nrequire("log.lua") + + local oldregister = item.RegisterItem + function itm.RegisterItem(itemtbl) + local oldserialize = itemtbl.Serialize + local olddeserialize = itemtbl.DeSerialize + + --Add a single character "b" "n" or "c" to an item's serialize to tell us if it's blessed, normal, or cursed + function itemtbl.Serialize(self) + local firstchar = "n" + if self.attribute == 1 then + firstchar = "b" + elseif self.attribute == -1 + firstchar = "c" + end + return firstchar .. oldserialize(self) + end + --Note that we've added a "attribute" field to each item, which is 1 if blessed, -1 if cursed, and anything else is normal. + function itemtbl.DeSerialize(self,data) + local firstchar = string.sub(data,0,1) + local rest = string.sub(data,1) + olddeserialize(self,rest) + if firstchar == "b" then + self.attribute = 1 + elseif firstchar == "c" then + self.attribute = -1 + elseif firstchar == "n" then + self.attribute = 0 + else + log.error("Deserialized with unknown first character: " .. firstchar) + end + end + end -- cgit v1.2.3-70-g09d2