|
| 07 Oct 2013 02:51 PM |
if right ~= nil and left ~= nil and front ~= nil and back ~= nil and above ~= nil then
how do i change that line so only 3 of them instead of 4 needs to be true |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 02:54 PM |
Like this:
areTrue = 0 sides = {right,left,front,back} for i,v in ipairs(sides) do if v then areTrue = areTrue + 1 end end
if areTrue >= 3 then |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 02:55 PM |
| Yeah I was gonna do that I hoped there was a clean way of doing it on one line :P |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 02:55 PM |
| Not that I can think of, no. I'm sorry. |
|
|
| Report Abuse |
|
|
breuning
|
  |
| Joined: 30 Oct 2008 |
| Total Posts: 4268 |
|
|
| 07 Oct 2013 03:00 PM |
function GetTrueCount() -- im tired, so its very very inefficient but well ok local count = 0 local a local b local c local d
if right == true then a = right count = count+1 end
if left == true then if a== nil then a = left else b= left end count = count +1 end
if front == true then if a== nil then a = front elseif b == nil then b= front else c = front end count = count +1 end
if above== true then if a== nil then a = above elseif b == nil then b= above elseif c==nil then c = above else d = above end count = count +1 end
return count,a,b,c,d
end
if GetTrueCount() >= 3 then |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:01 PM |
Hmmm...I like his idea! Here, this would be my guess for the easiest way possible:
function atLeastThreeSides() areTrue = 0 sides = {right,left,front,back} for i,v in ipairs(sides) do if v then areTrue = areTrue + 1 end end
return areTrue >= 3 end
if atLeastThreeSides() then |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:01 PM |
No loop. I don't like loops:
areTrue = 0 bump = function() areTrue = areTrue + 1 end if right then bump() end if left then bump() end if front then bump() end if back then bump() end
if areTrue >= 3 then |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:02 PM |
| Loops are a million times more efficient than what you just posted... :/ |
|
|
| Report Abuse |
|
|
breuning
|
  |
| Joined: 30 Oct 2008 |
| Total Posts: 4268 |
|
|
| 07 Oct 2013 03:03 PM |
| stop bragging, mine is most ineffiecient. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:04 PM |
| I will admit...Yours is by far the least efficient. :P |
|
|
| Report Abuse |
|
|
breuning
|
  |
| Joined: 30 Oct 2008 |
| Total Posts: 4268 |
|
|
| 07 Oct 2013 03:04 PM |
yes, i am the king of ineffieciency
go to hell generic for loops! |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:08 PM |
More code
function wat(flu,xa,xb,xc,xd,xe,xf) return flu<=(xa and 1 or 0)+(xb and 1 or 0)+(xc and 1 or 0)+(xd and 1 or 0)+(xe and 1 or 0)+(xf and 1 or 0) and true or false end
ThisIsWhatOPWants = wat(3,right,left,front,back,top) |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:19 PM |
@BBaller I really don't think you know the difference between efficiency and inefficiency. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:20 PM |
| My version is the most efficient as it reuses variables rather than creating new ones for every single check... :/ |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:23 PM |
| I wasn't talking about you're script, it just seems with how you use the word efficient that you don't really know the real meaning. |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 07 Oct 2013 03:24 PM |
if a and b and c and d then
One line? acomplished. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:24 PM |
| Well what do you define the word as? |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:25 PM |
| @digpoe: He wanted to find a way to do it so that only three of the four had to be true. |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 07 Oct 2013 03:26 PM |
Also, here's what you were looking for:
if (function(...) local ret = 0 for _, v in pairs{...} do if v then ret = ret + 1 end end return ret end)(a, b, c, d) >= 3 then |
|
|
| Report Abuse |
|
|