I'm very new to the whole Lua scene.
So first of all... I've looked this up but the explanations I found simply make no sense to me...
Take the "customCheck" from DarkRP's Wiki for example:
[CODE]customCheck = function(ply) return ply:GetNWString("usergroup") == "donator" or ply:IsAdmin() end,[/CODE]
What the fuck does that "return" shit do?! I can decipher the rest of the code just fine. However, that damned return shit makes no sense to me. Can someone please give me an explanation for a beginner?
Scripts are able to call [B]functions[/B] that can process [B]input[/B], and, depending on that input, can [B]return[/B] an [B]output[/B].
[B]return[/B] is a way to tell whoever called the function what the [B]result[/B] of the function is. Think of a very simple function, such as one that adds two numbers.
[code]function add(x, y)
x + y
end[/code]
Very simple code, yes? But wait...where is the [B]result[/B] stored? Nowhere. If it isn't stored anywhere, how is the caller of the function supposed to know the output? This is why you need to [B]return[/B] the [B]output[/B] or [B]result[/B].
[code]function add(x, y)
return x + y
end[/code]
Then, you can do this.
[code]local result = add(6, 4)[/code]
And the result is equal to 10.
[QUOTE=man with hat;49762511]Scripts are able to call [B]functions[/B] that can process [B]input[/B], and, depending on that input, can [B]return[/B] an [B]output[/B].
[B]return[/B] is a way to tell whoever called the function what the [B]result[/B] of the function is. Think of a very simple function, such as one that adds two numbers.
[code]function add(x, y)
x + y
end[/code]
Very simple code, yes? But wait...where is the [B]result[/B] stored? Nowhere. If it isn't stored anywhere, how is the caller of the function supposed to know the output? This is why you need to [B]return[/B] the [B]output[/B] or [B]result[/B].
[code]function add(x, y)
return x + y
end[/code]
Then, you can do this.
[code]local result = add(6, 4)[/code]
And the result is equal to 10.[/QUOTE]
I see... However, how is it used in the custom check? I mean it doesn't use the stored result??
[QUOTE=Percipience;49762618]I see... However, how is it used in the custom check? I mean it doesn't use the stored result??[/QUOTE]
The result doesn't need to be stored anywhere if you return the result itself. Maybe the code being on one line makes it look weirder. Hopefully this clears it up.
[code]-- Bad.
local function customCheck(ply)
ply:GetNWString("usergroup") == "donator" or ply:IsAdmin()
end
-- Good.
local function customCheck(ply)
return ply:GetNWString("usergroup") == "donator" or ply:IsAdmin()
end
-- Not good, but valid.
local function customCheck(ply)
local result = ply:GetNWString("usergroup") == "donator" or ply:IsAdmin()
return result
end[/code]
[QUOTE=man with hat;49762658]The result doesn't need to be stored anywhere if you return the result itself. Maybe the code being on one line makes it look weirder. Hopefully this clears it up.
[code]-- Bad.
local function customCheck(ply)
ply:GetNWString("usergroup") == "donator" or ply:IsAdmin()
end
-- Good.
local function customCheck(ply)
return ply:GetNWString("usergroup") == "donator" or ply:IsAdmin()
end
-- Not good, but valid.
local function customCheck(ply)
local result = ply:GetNWString("usergroup") == "donator" or ply:IsAdmin()
return result
end[/code][/QUOTE]
So basically it tells the server to check ... but without the "return" it would check, just not return the results?
[QUOTE=Percipience;49762712]So basically it tells the server to check ... but without the "return" it would check, just not return the results?[/QUOTE]
The first piece of code would actually cause an error, but yes, the third piece of code (without the return) would perform the check and never tell what the result was.
[QUOTE=man with hat;49762728]The first piece of code would actually cause an error, but yes, the third piece of code (without the return) would perform the check and never tell what the result was.[/QUOTE]
Thank you so much bro! :D I understand finally lmaooo!
I have never looked into DarkRP, but the way I imagine they use this is by doing something like this:
[lua]-- When trying to switch job
if jobtable[targetJob].customCheck(ply) then
-- Allow ply to become job
end[/lua]
You have to imagine the return value replacing the function call. In which case it would be true if the player Is a donator or is an admin. It's only when you call this function that the return value gets calculated, meaning that if the player later becomes a donator, it will return true from then on.
Sorry, you need to Log In to post a reply to this thread.