aboutsummaryrefslogtreecommitdiff
path: root/tutorials/tut021_detouring.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/tut021_detouring.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/tut021_detouring.md')
-rw-r--r--tutorials/tut021_detouring.md29
1 files changed, 29 insertions, 0 deletions
diff --git a/tutorials/tut021_detouring.md b/tutorials/tut021_detouring.md
new file mode 100644
index 0000000..7e4c980
--- /dev/null
+++ b/tutorials/tut021_detouring.md
@@ -0,0 +1,29 @@
+# Tut 0x021
+## Detouring
+
+Detouring is a method commonly used in lua, even outside of gmod development. The idea is to redirect a function through your function first, then call the function you're redirecting at the end (or maybe the beginning, or anywhere really). Let's see an example:
+
+garrysmod/lua/autorun/detourprint.lua
+
+ local oldprint = print
+ function print(...)
+ local args = {...}
+ args[#args + 1] = "if you know what I mean..."
+ oldprint(unpack(args))
+ end
+
+The above script __replaces__ the print function, with a new function, which eventually calls the original print function, with modified arguments. The above can be done with modules in Artery as well. For example, `log.lua`'s .error() function currently only prints a message in red, it dosn't actually show what failed or why. Let's modify it to show a stack trace.
+
+garrysmod/addons/artery_rougelite/trace_errors.lua
+
+ local log = nrequire("log.lua")
+ local colors = nrequire("config/colortheme.lua") --Holds colors
+
+ local oldlog = log.error
+ function log.error(...)
+ oldlog(unpack({...}))
+ MsgC(colors.console.red,debug.traceback())
+ end
+
+
+For more information on colortheme.lua see @{colortheme.lua}, for more on debug.traceback() see [the gmod wiki](http://wiki.garrysmod.com/page/debug/traceback).