Thaeh
|
  |
| Joined: 05 Feb 2011 |
| Total Posts: 7685 |
|
|
| 21 Jan 2014 07:33 PM |
a = 1 and 2 print(a) --> 2
a = 2 and 1 print(a) --> 1
a = 2 and 1 if a == 2 then print(2) --> no output
Can variables have two values? As far as I can see the 'and' keyword overwrites and nothing else. |
|
|
| Report Abuse |
|
|
Vyxium
|
  |
| Joined: 31 Aug 2010 |
| Total Posts: 1020 |
|
|
| 21 Jan 2014 07:36 PM |
| the and keyword is for if statements most of the time or when a certain object equals 2 or more things, there are ways to make variables with 2 or more values and im pretty sure they are called tables. try the wiki |
|
|
| Report Abuse |
|
|
TheJKM98
|
  |
| Joined: 20 Dec 2010 |
| Total Posts: 511 |
|
|
| 21 Jan 2014 07:36 PM |
| No, not to my knowledge, they cannot. |
|
|
| Report Abuse |
|
|
shawnyg
|
  |
| Joined: 21 Apr 2011 |
| Total Posts: 1428 |
|
|
| 21 Jan 2014 07:36 PM |
No, they can't. Well, sort of. a = math.random(1,2) --Random numbers 1 through 2 if a == 2 then print(2) end
~Hope I helped, if you need a script, PM me. ~Glory to CAT!~ ~CAT General Shawnyg~ |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2014 07:37 PM |
That's not how the and operator works. And is used to compare two boolean values. It returns true if both booleans are true.
local Result = true and true --Result would be true local Result = true and false --Result would be false |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2014 07:37 PM |
You do not understand what the 'and' keyword does.
a = b and c
If b is nil or false it will return b, but if not it will return c. That is how they work in if statements, and you will understand what I said much better if you understand that if statements will run following code if the statement is truthy.
You cannot have a variable have two values, but you can store two value in a table to achieve the same effect.
a = {Hi = true, Bye = false}
print(a.Hi) |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2014 07:37 PM |
It might go like this: a = 1,2
Anyway you can also set variables based off conditions:
a = 5
b = a == 5 and 10 or a == 2 and 5 |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2014 07:38 PM |
| And there is no reason why one variable should hold 2 values. If you want to store multiple values use a table. |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2014 07:41 PM |
You are doing it wrong let me re right the code a = ("1,2") print (a)
for the == one i have no idea i will mess around ok
|
|
|
| Report Abuse |
|
|
Bebee2
|
  |
| Joined: 17 May 2009 |
| Total Posts: 3985 |
|
|
| 21 Jan 2014 07:44 PM |
| It depends how Lua handles their variable creation. |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2014 07:44 PM |
No, only tables can store multiple values.
I'll explain how and works in lua so you can understand whats going on:
In Lua, the only 2 'objects' considered as false or nonexistent are nil and false. Lua uses short-cut evaluation, this means that when you do 'a and b', it evaluates a, if a is false, it the evaluation ends and it will never evaluate b, if a is nil, the same happens (though the final value will be false if a is false, or nil if a is nil). If a is ANYTHING but false or nil, then it will proceed to evaluate b (know true, workspace, 1, {}, "" will pass the evaluation). But wait, it is short-cut! So it never evaluates b and just sort of like 'returns' b. So whatever value b has will take the place of a and b. EXAMPLES!
print(true and true) -->true print(true and 1) -->1 print(1 and true) -->true print(false and true) -->false print(true and false) -->false print(nil and false) -->nil print(1 and nil) -->nil
So when you do a = 1 and 2, since 1 is not nil nor false, a becomes 2. Same for the opposite.
This short-cut behavior is used for something called ternary. 'or' works the opposite. a or b is evaluated this way: if a is anything but false or nil, then it 'returns' a, if a is false or nil, then it 'returns b'; since it evaluates every 'and' and 'or' one at a time, this is what would happen if you combine them:
a = true print(a and 1 or 2) -->1 So first goes a and 1: true and 1: 1 so it becomes print(1 or 2) now it evaluates 1 or 2: 1 print(1) -->1
a = false print(a and 1 or 2) -->2 a and 1: false and 1: false print(false or 2) false or 2: 2 print(2)
:B This is known as ternary, basically you do a very short if:
if blah.Parent == nil then func1() else func2() end
could be shortened using ternary this way:
blah.Parent == nil and func1() or func2() |
|
|
| Report Abuse |
|
|
Thaeh
|
  |
| Joined: 05 Feb 2011 |
| Total Posts: 7685 |
|
|
| 21 Jan 2014 07:48 PM |
| Marvelous answer Alex, thank you. Thanks to everybody else aswell. |
|
|
| Report Abuse |
|
|
| |
|
Bebee2
|
  |
| Joined: 17 May 2009 |
| Total Posts: 3985 |
|
|
| 21 Jan 2014 07:50 PM |
I forgot what you call it, but there's a way to conditional stating. Ex.
print(true and false) (false) print(true and true) (true) print(true and true and false) (false) print(true or false) (true) print(true or true and false) (true) print(true and true or false) (true) print(false or false and true) (false)
You get the gist? |
|
|
| Report Abuse |
|
|
Thaeh
|
  |
| Joined: 05 Feb 2011 |
| Total Posts: 7685 |
|
| |
|
Thaeh
|
  |
| Joined: 05 Feb 2011 |
| Total Posts: 7685 |
|
|
| 21 Jan 2014 07:56 PM |
I was joking!
Axel, I tried to replicate what you showed at the end of your post but it wasn't valid. Do you mind telling me if it's possible to use that instead of the usual 'if'? |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2014 08:07 PM |
It is possible, I had a friend make a whole game without using if at all, sure he was a very good scripter (he became famous but I won't say his name).
Though that specific example needs some very specific type of functions. Also I just checked and you will need an assignment for that (sorry!):
function f1() print "1" return true end
function f2() print "2" return true end
a = false and f1() or f2()
OUTPUT: 2
You might have noticed my functions return true. If they don't return anything, they, by default, return nil, and so the f1() or f2() will become nil or f2(). It would then run both functions. So you can use this if your functions return anything but nil or false. You could use anonymous functions though:
a = false and (function() f1() return true end)() or (function() f2() return true end)() |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2014 08:11 PM |
Oh oh! This one is fun too:
function a() print "1" end
function b() print "2" end
(true and a or b)()
It prints 1. Since functions can be used in variables.
a = function() end b = a; c = b; c(); |
|
|
| Report Abuse |
|
|
Thaeh
|
  |
| Joined: 05 Feb 2011 |
| Total Posts: 7685 |
|
|
| 21 Jan 2014 10:18 PM |
| This will only work with two functions, right? Since it returns true or false there can't be more than two possible outcomes. Am I right? |
|
|
| Report Abuse |
|
|
|
| 21 Jan 2014 10:23 PM |
If you change the return then you can "stack" it:
This is if elseif elseif else end:
print(a and b and c or d)
Every and would be like an if, the or is the 'else' so to speak.
a = b<5 and kill() --if b is lower than 5 it will kill, else it won't do anything b = c < 5 and (function() kill() return true) or d > 5 and save() --if else if |
|
|
| Report Abuse |
|
|