aboutsummaryrefslogtreecommitdiff
path: root/tutorials/tut042_too_many_items.md
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-11-26 21:07:54 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2017-11-26 21:07:54 -0500
commit83af51534bf16bf048aea1cd3b74a0308ed9dd71 (patch)
treeff82f3e6dd841633b1355b73181bcae607ee1138 /tutorials/tut042_too_many_items.md
parent25e4d04a331a6a0b9d897d4f721757730771ff97 (diff)
downloadartery-83af51534bf16bf048aea1cd3b74a0308ed9dd71.tar.gz
artery-83af51534bf16bf048aea1cd3b74a0308ed9dd71.tar.bz2
artery-83af51534bf16bf048aea1cd3b74a0308ed9dd71.zip
Started work on writing tutorials
Wrote tutorials for * Setup * Addon structure * Inventories * Items
Diffstat (limited to 'tutorials/tut042_too_many_items.md')
-rw-r--r--tutorials/tut042_too_many_items.md43
1 files changed, 43 insertions, 0 deletions
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