aboutsummaryrefslogtreecommitdiff
path: root/tutorials/tut020_nrequire.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/tut020_nrequire.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/tut020_nrequire.md')
-rw-r--r--tutorials/tut020_nrequire.md111
1 files changed, 111 insertions, 0 deletions
diff --git a/tutorials/tut020_nrequire.md b/tutorials/tut020_nrequire.md
new file mode 100644
index 0000000..2d0cb6d
--- /dev/null
+++ b/tutorials/tut020_nrequire.md
@@ -0,0 +1,111 @@
+# Tut 0x020
+
+## nrequire()
+
+Most gamemodes place a table in the global namespace that exposes the various features of the gamemode. Artery takes a different approach.
+
+Artery places a function in the global namespace, `nrequire()`, which acts as the global table, the way to create dependencies between files, and the auto-includer all at once. Let's do a simple example. If you had a folder structure like this:
+```
+gamemode/
+ one/
+ onefile.lua
+ same.lua
+ two/
+ same.lua
+```
+
+and you wanted to access methods or data in the file `onefile.lua`, you would set your files up like this:
+
+garrysmod/gamemodes/artery/gamemode/one/onefile.lua
+
+ local ret = {}
+
+ ret.mydata = 24
+
+ return ret
+
+
+garrysmod/addons/artery_rougelite/data/artery/global/somefile.lua
+
+ local one = nrequire("onefile.lua")
+
+ print(one.mydata) --Prints 24
+
+
+## Resolving conflicts
+
+But what if you wanted to run some data from `one/same.lua`?
+
+garrysmod/gamemodes/artery/gamemode/one/same.lua
+
+ local ret = {}
+
+ ret.data = 3.14
+
+ return ret
+
+
+If you did
+
+
+garrysmod/addons/artery_rougelite/data/artery/global/somefile.lua
+
+ local same = nrequire("same.lua")
+
+ print(same.data)
+
+
+You would get an error:
+```
+Ambiguous scan, there are two or more paths that match "same.lua"
+ gamemode/one/same.lua
+ gamemode/two/same.lua
+Specify more of the file path.
+```
+
+The solution is simply to write more of the file path
+
+ local same = nrequire("one/same.lua")
+ print(same.data)
+
+
+## nrequire() as dependency resolution
+
+You don't actually need to save whatever nrequire() returns. If you know the file `sv_invfuncs` adds a method to the player metatable, `player:HasItem(itemname)` which you want to use, you can just call nrequire() without saving the return table.
+
+garrysmod/addons/artery_routelite/data/artery/global/somefile.lua
+
+ nrequire("sv_invfuncs.lua")
+
+ local ply = Entity(1)
+
+ ply:HasItem("Test Item")
+
+
+## nrequire() as auto includer
+
+nrequire() is automatically loads all the other modules in artery. It runs them in the client or server domain depending on their file name, much like DarkRP. If the file begins with `sv_` it is run on the server only, if it begins with `cl_` it is sent to the client and run on the client only. Anything else will be run on both the server and the client.
+
+Much likes the main files of artery, the files under `garrysmod/addons/artery_rougelite/data/artery/global` will follow the same scheme. If you want your data to only be run on only the server or client, just name the file beginning with `sv_` or `cl_`.
+
+## Using artery modules
+
+Let's do an example. On the left, you will see a lot of modules provided by artery. One of these, under "shared" domain is `log.lua`. Open it in a new tab.
+
+You will see several methods provided by `log.lua`, to use them, you will first use nrequire() to get access to the functions, then call whatever you like.
+
+garrysmod/addons/artery_rougelite/data/artery/global/logthing.lua
+
+ local log = nrequire("log.lua")
+
+ log.debug("Hello, world!")
+
+
+Note that we named the file `logthing.lua`, since the file does not begin with `sv_` or `cl_`, it will be in the shared domain, that is, run once on the client and once on the server. Since it is run twice, we can expect to see 2 messages in the console!
+
+## Next steps
+
+From here the tutorials open up a bit. You can continue on, or read more in depth about the subject. Tutorials named tut030, tut040, ect. are in logical order needed to build an addon for Artery, tutorials named tut031, tut041, ect. would be continuations of tut030 and tut040 respectively.
+
+Further reading: @{tut021_detouring.md}
+Next tutorial: @{tut030_inventories.md}