edenojack
|
  |
| Joined: 18 Jul 2008 |
| Total Posts: 989 |
|
|
| 03 Jan 2016 02:37 PM |
I've encountered this in a lot of scripts, but still do not understand its use. Lines such as
function SubtleDifference(PB) local Jelly-Fish = "Jellyfish" or PB ... ... end
SubtleDifference("Jelly Fish")
What are its uses? And what happens when you add more "or" vairables ? |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 02:42 PM |
I know it's pseudo code; but that wouldn't work as intended.
local function Test(Var) Var = Var or "No variable supplied" print(Var) end
Test("Hello world!") Test()
> "Hello world!" > "No variable supplied"
It allows for optional arguments in this case.
We can also do something like this:
local function Test(Var) print(Var or "No variable supplied") end
Keep in mind though, if Var could be false; this method should not be used.
|
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 02:42 PM |
local a = false local b = "Test"
print(a or b)
In this case, it will print "Test" because in the case that a condition before "or" is false or nil, the condition after "or" will be used instead. |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 03 Jan 2016 02:42 PM |
Well that code will error because you have - between Jelll and Fish. But in that case it doesn't do anything, the variable will always equal "Jellyfish".
|
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 02:43 PM |
| http://wiki.roblox.com/index.php?title=Boolean#Conditional_expression |
|
|
| Report Abuse |
|
|
FlyNormal
|
  |
| Joined: 30 Sep 2015 |
| Total Posts: 344 |
|
|
| 03 Jan 2016 04:24 PM |
| http://wiki.roblox.com/index.php?title=Operator#Logical |
|
|
| Report Abuse |
|
|
|
| 03 Jan 2016 04:30 PM |
'or' and 'and' can be used as ternary operators in Lua
for example:
local bool = true local name = (bool and "Fred" or "Jake")
print(name) --> Fred
bool = false name = (bool and "Fred" or "Jake")
print(name) --> Jake
https://en.wikipedia.org/wiki/%3F: |
|
|
| Report Abuse |
|
|
edenojack
|
  |
| Joined: 18 Jul 2008 |
| Total Posts: 989 |
|
| |
|
|
| 03 Jan 2016 05:22 PM |
if you used the ? operator in c it would do the same. its a compacted ifstatement for setting a value basically
|
|
|
| Report Abuse |
|
|