aboutsummaryrefslogtreecommitdiff
path: root/tutorials/tut020_nrequire.md
blob: 2d0cb6d200c6aca4b4522eeaef2b00dd57145b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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}