So, if you go into the default HUD of DarkRP you'll find, multiple times, the following:
[code]
localplayer = localplayer and IsValid(localplayer) and localplayer or LocalPlayer()
if not IsValid(localplayer) then return end
[/code]
What is the variable doing? It makes no sense to me.
1. Why check if "localplayer" is valid, if you do the same in an if statement?
2. Why is there so many and operators?
3. Why are the and operators stating the same thing?
4. Why have the or operator if "localplayer" is [u]supposed to be[/u] LocalPlayer()?
5. And how would you validate "localplayer" if it only equals a variable that is set to itself?!
So many questions...
If anyone can help me, I'd really appreciate it! It's really confusing to me.
in lua; the [and/or] operators can double as an if statement for [B]variable assignment[/B] if used correctly.
[CODE]localplayer = localplayer and IsValid(localplayer) and localplayer or LocalPlayer()
if not IsValid(localplayer) then return end[/CODE]
expands out to:
[CODE]
local localplayer -- assuming it's local
if localplayer and IsValid( localplayer ) then
localplayer = localplayer
else
localplayer = LocalPlayer()
end
if not IsValid(localplayer) then return end
[/CODE]
the reason this is done is because LocalPlayer() is a userdata function defined in C++; and the object it returns it invalid until after [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/InitPostEntity]GM:InitPostEntity[/url]. If cached or called too early, it'll return a NULL entity and break your script.
[editline]9th April 2017[/editline]
See: [url]https://en.wikipedia.org/wiki/Short-circuit_evaluation#Support_in_common_programming_languages[/url]
particularly [I]last value[/I] returns
[QUOTE=Bull29;52081396]in lua; the [and/or] operators can double as an if statement for [B]variable assignment[/B] if used correctly.
[CODE]localplayer = localplayer and IsValid(localplayer) and localplayer or LocalPlayer()
if not IsValid(localplayer) then return end[/CODE]
expands out to:
[CODE]
local localplayer -- assuming it's local
if localplayer and IsValid( localplayer ) then
localplayer = localplayer
else
localplayer = LocalPlayer()
end
if not IsValid(localplayer) then return end
[/CODE]
the reason this is done is because LocalPlayer() is a userdata function defined in C++; and the object it returns it invalid until after [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/InitPostEntity]GM:InitPostEntity[/url]. If cached or called too early, it'll return a NULL entity and break your script.
[editline]9th April 2017[/editline]
See: [url]https://en.wikipedia.org/wiki/Short-circuit_evaluation#Support_in_common_programming_languages[/url]
particularly [I]last value[/I] returns[/QUOTE]
Yup, I'm completely lost
I get how it could expand into an if statement (sort of)... But the whole "short-circuit evaluation" is super confusing.
[QUOTE=Wavie;52081572]Yup, I'm completely lost
I get how it could expand into an if statement (sort of)... But the whole "short-circuit evaluation" is super confusing.[/QUOTE]
It's quite easy to understand
Pretty much what it's saying is that and == then and or == else.
Look at the original code and replace and with then and or with else, then it should make since.
[QUOTE=Nick78111;52082013]It's quite easy to understand
Pretty much what it's saying is that and == then and or == else.
Look at the original code and replace and with then and or with else, then it should make since.[/QUOTE]
As I said, I sort of understand what he's saying there. But I don't understand short-circuit evaluation (or the explanation on Wikipedia). Is the wiki just a longer explanation of what he told me?
Try reading the "Logical Operators" sub chapter from the Lua manual [URL="https://www.lua.org/manual/5.1/manual.html#2.5.3"]here[/URL], it explains how the `and` and `or` operators work in probably a better way than we can.
After reading that you can apply the same to this expression, just this has more than a single `and`
[QUOTE=bigdogmat;52083376]Try reading the "Logical Operators" sub chapter from the Lua manual [URL="https://www.lua.org/manual/5.1/manual.html#2.5.3"]here[/URL], it explains how the `and` and `or` operators work in probably a better way than we can.
After reading that you can apply the same to this expression, just this has more than a single `and`[/QUOTE]
Thank, man! I understand it now :)
Sorry, you need to Log In to post a reply to this thread.