This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Trikk's profile picture

[Lua]Question about my code

Started by Trikk, 08 May 2013 - 09:25 PM
Trikk #1
Posted 08 May 2013 - 11:25 PM
"[Lua]Question about my code.

So I've coded a touch control program using an advanced monitor. I've got it functioning, but im just wondering why I can't have two different commands on the same y line. For example if I have this line of code:

if (xPos = 1 or 2) and (yPos = 10) then
–do something here–

Then this one:
if (xPos = 5 or 6) and (yPos = 10) then
–do something here–

the second line of code doesn't work. the function I call for on the first line of code is always what happens even if im clicking where the second should kick in.
to work around it I've just separated them to different y lines but is there another way? it doesn't look like I want it at the moment. I was thinking I may need to use buttons instead, but I need some guidance on where to go for that. Anyways here is the code so far so maybe you can fix it.

--Control for Wireless Turtle: Trikk 2013

rednet.open("top") --You can use top, left, right, or back for the wireless modem.
local m = peripheral.wrap("right") --Again, top right left or back. This time for the monitor.

function draw() --This is the code to put text on the attached monitor.
m.clear()
m.setTextScale(0.5)
m.setCursorPos(7,1)
m.setTextColor(16)
m.write("UP")
m.setCursorPos(6,10)
m.setTextColor(16)
m.write("DOWN")
m.setCursorPos(1,5)
m.setTextColor(2048)
m.write("LEFT")
m.setCursorPos(11,6)
m.setTextColor(2048)
m.write("RIGHT")
m.setCursorPos(5,3)
m.setTextColor(16384)
m.write("FORWARD")
m.setCursorPos(6,8)
m.setTextColor(16384)
m.write("BACK")
end
draw()

while true do --This is the code to tell if there has been a specific position on the monitor clicked.
event, side, xPos, yPos = os.pullEvent("monitor_touch") --Additionally different locations call for different functions.
if (xPos == 7 or 8) and (yPos == 1) then
rednet.broadcast("u")

elseif (xPos == 6 or 7 or 8 or 9) and (yPos == 10) then
rednet.broadcast("d")

elseif (xPos == 1 or 2 or 3 or 4) and (yPos == 5) then
rednet.broadcast("l")

elseif (xPos == 11 or 12 or 13 or 14 or 15) and (yPos == 6) then
rednet.broadcast("r")

elseif (xPos == 5 or 6 or 7 or 8 or 9 or 10 or 11) and (yPos == 3) then
rednet.broadcast("f")

elseif (xPos == 6 or 7 or 8 or 8) and (yPos == 8) then
rednet.broadcast ("b")
end
end

and

--Wireless Turtle Code : Trikk 2013

rednet.open("right") --Open the modem on the turtle.

function forward() --Forward function
  detect = turtle.detect()
  if detect == true then
   dig()
  end
  turtle.forward()
end

function back() --Back function
  turtle.back()
end

function right() --Right function
  turtle.turnRight()
end

function left() --Left function
  turtle.turnLeft()
end

function up() --Up function
  detect = turtle.detectUp()
  if detect == true then
   digU()
  end
  turtle.up()
end

function down() --Down function
  detect = turtle.detectDown()
  if detect == true then
   digD()
  end
  turtle.down()
end

function dig() --Dig function
  turtle.dig()
  sleep(0.50)
end

function digU() --Dig up (above the turtle) function
  turtle.digUp()
  sleep(0.50)
end

function digD() --Dig down (below the turtle) function
  turtle.digDown()
  sleep(0.50)
end

while true do --Code to wait for a command, if a certain message is recieved, a certain function is given.
  id, msg = rednet.receive()

  if msg == "f" then
  forward()
  end

  if msg == "b" then
  back()
  end

  if msg == "r" then
  right()
  end

  if msg == "l" then
  left()
  end

  if msg == "u" then
  up()
  end

  if msg == "d" then
  down()
  end
end

Thanks!
LBPHacker #2
Posted 09 May 2013 - 12:42 PM
That's how Lua works, sorry. But you can actually test for intervals:
if x == 1 or x == 2 or x == 3 then
is the same as
if x > 0 and x < 4 then

And don't forget to use double = as an equality operator…
DiamondQ #3
Posted 09 May 2013 - 03:56 PM
So, first, I'm assuming that you really meant

if (xPos == 1 or 2) and (yPos == 10) then
since with only one equals, this doesn't compile (due to the fact you can't do an assignment within an expression).

If you expand this to what the Lua interpreter actually sees:

if ((xPos == 1) or ((2 ~= nil) and (2 ~= false))) and (yPos == 10) then

The problem here is that 2 is just being checked if it's true, and Lua defines an expression as true, if it's not nil and not false.

Thus, logically, if you boil this down (each line is the logical reduction of the previous)


if ((xPos == 1) or ((2 ~= nil) and (2 ~= false))) and (yPos == 10) then
if ((xPos == 1) or ((  true  ) and (   true   ))) and (yPos == 10) then
if ((xPos == 1) or (           true            )) and (yPos == 10) then
if (                    true                    ) and (yPos == 10) then
if                                                    (yPos == 10) then

Thus, the line always runs (assuming yPos is equal to 10).

As indicated by LBPHacker, just repeat the variable.

if (xPos == 1 or xPos == 2) and (yPos == 10) then

And for bonus marks :)/> , you can increase performance slightly by leveraging the short-circuit evaluation behavior of Lua with:

if (yPos == 10) and (xPos == 1 or xPos == 2) then
Trikk #4
Posted 09 May 2013 - 04:37 PM
I'll do some tweaking. Thanks for your input guys. Very appreciated.
Trikk #5
Posted 09 May 2013 - 04:43 PM
Oh, could someone point me in a direction that would help with using buttons? Thanks..
Engineer #6
Posted 09 May 2013 - 05:01 PM
Oh, could someone point me in a direction that would help with using buttons? Thanks..
I have made a tutorial on that:

http://www.computercraft.info/forums2/index.php?/topic/11046-clickable-buttons-in-cc/