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

How to Group Cordinates

Started by Jman, 28 January 2013 - 11:32 AM
Jman #1
Posted 28 January 2013 - 12:32 PM
Okay so Im trying to group a pair of coordinates for my homemade button but I cant seem to figure out what Im doing wrong because the script doesn't run right and does not return anything, it just terminates the program. Plus I'm trying to avoid drawing anything.
while true do
local event, button,X, Y = os.pullEvent("mouse_click")
XY = X..","..Y
c = "40,11", "41,11", "42,11", "43,11", "44,11", "45,11"
If XY == c and button == 1 then
Loading()
break
end
end
GravityScore #2
Posted 28 January 2013 - 02:38 PM
Storing 2 x and y variables as a string to pair them is not a great idea. It would be much easier if you just left them as separate integers.

What's wrong with your code though is that you're only setting c to the first variable in that comma-separated list you've made:

c = "40,11", "41,11", ...
-- c is only getting set to the first variable in the list, which is "40,11"
-- You cannot set c to multiple different values at the same time.

What you would want to use here is an array of the coordinates, set c to that, then loop over every coordinate (using a for loop) and check if XY equals any of the coordinates in the array. If it does, perform the loading and break from the loop. Eg:

while true do
  local event, button, x, y = os.pullEvent("mouse_click")
  c = {"40,11", "41,11", "42,11", "43,11", "44,11", "45,11"}
  XY = x .. "," .. y
  for k, v in pairs(c) do
    if XY == v then
      -- Perform loading
      break
    end
  end
end

A better way of doing it though is to check if the y value equals the y value of the printed text, and the x value is between the start and the end of the text on that line. Here:

while true do
  local e, but, x, y = os.pullEvent("mouse_click")
  -- Remember, you list stated that your click points were on line 11, and between 40 and 45.
  if y == 11 and x >= 40 and x <= 45 then
    -- Perform loading
  end
end
Jman #3
Posted 29 January 2013 - 02:27 AM
Thanks that makes more sense, and also points out that I didn't pay enough attention to pairing in my lua tutorials, back to reading.
Jman #4
Posted 29 January 2013 - 02:51 AM
Mind telling me on how I can shrink this please?
function Loading()
term.clear()
term.setCursorPos(20,10)
print"Loading."
sleep(1)
term.clear()
term.setCursorPos(20,10)
print"Loading.."
sleep(1)
term.clear()
term.setCursorPos(20,10)
print"Loading..."
sleep(1)
term.clear()
term.setCursorPos(20,10)
print"Loading...."
sleep(1)
Front()
end
If I'm thinking correctly then you cant do
function Loading()
while true do
  term.clear()
  term.setCursorPos(20,10)
  a=2
  i='Loading'
  i= i+'.'
  print(..i)
  when i == 'Loading....' do
   i='Loading'
   a= a-1
  end
  when a == 0 do
   a=2
   front()
   break
  end
end
end
theoriginalbit #5
Posted 29 January 2013 - 02:58 AM
ok well I am deeply against fake loading screens, to the extent that i wrote an API that allows for real loading screens not fake ones… but heres what u can do



function Loading()
  local a = 2
  while true do
    term.clear()
    term.setCursorPos(20,10)
    write( "Loading" )
    for i = 0, 4 do
      write( "." )
      sleep(1)
    end
    a = a - 1
    if a == 0 then break end
  end
end
Jman #6
Posted 29 January 2013 - 03:10 AM
ok well I am deeply against fake loading screens, to the extent that i wrote an API that allows for real loading screens not fake ones… but heres what u can do



function Loading()
  local a = 2
  while true do
	term.clear()
	term.setCursorPos(20,10)
	write( "Loading" )
	for i = 0, 4 do
	  write( "." )
	  sleep(1)
	end
	a = a - 1
	if a == 0 then break end
  end
end
your pictures were not loading and I dislike searching the whole internet just to find some more recent ones, so I didn't know if it was worth learning what all those functions do. But thanks. EDIT:Understood
theoriginalbit #7
Posted 29 January 2013 - 03:37 AM
your pictures were not loading and I dislike searching the whole internet just to find some more recent ones, so I didn't know if it was worth learning what all those functions do. But thanks.
My pictures? I only just added screenshots.
And you wouldn't have found another realtime one on these forums. Still the first and only one (last I checked 2 days ago).

EDIT: Also there is an entire section devoted to pastebin examples of how to use the code. they are all 100% functional.
Edited on 29 January 2013 - 02:40 AM
Jman #8
Posted 29 January 2013 - 04:19 AM
Storing 2 x and y variables as a string to pair them is not a great idea. It would be much easier if you just left them as separate integers.

What's wrong with your code though is that you're only setting c to the first variable in that comma-separated list you've made:

c = "40,11", "41,11", ...
-- c is only getting set to the first variable in the list, which is "40,11"
-- You cannot set c to multiple different values at the same time.

What you would want to use here is an array of the coordinates, set c to that, then loop over every coordinate (using a for loop) and check if XY equals any of the coordinates in the array. If it does, perform the loading and break from the loop. Eg:

while true do
  local event, button, x, y = os.pullEvent("mouse_click")
  c = {"40,11", "41,11", "42,11", "43,11", "44,11", "45,11"}
  XY = x .. "," .. y
  for k, v in pairs(c) do
	if XY == v then
	  -- Perform loading
	  break
	end
  end
end

A better way of doing it though is to check if the y value equals the y value of the printed text, and the x value is between the start and the end of the text on that line. Here:

while true do
  local e, but, x, y = os.pullEvent("mouse_click")
  -- Remember, you list stated that your click points were on line 11, and between 40 and 45.
  if y == 11 and x >= 40 and x <= 45 then
	-- Perform loading
  end
end
How would I do multiple lines?
--Front Ballast Tank Menu Input
while true do
local event, button,x, y = os.pullEvent("mouse_click")
a = {"20,8", "21,8", "22,8", "23,8", "24,8", "25,8", "26,8", "27,8", "28,8", "29,8", "30,8"}
b = {"20,9", "21,9", "22,9", "23,9", "24,9", "25,9", "26,9", "27,9", "28,9", "29,9", "30,9"}
c = {"20,10", "21,10", "22,,10", "23,10", "24.10", "25,10", "26,10", "27,10", "28,10", "29,10", "30,10"}
if (missing data) and button == 1 then
term.clear()
term.setCursorPos(20,10)
print("Filling..")
sleep(0.3)
FFill()
break
elseif (missing data) and button == 1 then
term.clear()
term.setCursorPos(10,10)
print("Draining Front Ballast Tank..")
sleep(0.3)
FEmpty()
break
elseif (missing data) and button == 1 then
term.clear()
term.setCursorPos(10,10)
print("Setting All Values To False..")
sleep(0.3)
FReset()
break
end
end
"Missing data" being where (in order) a, b, and c go.
Bogdacutu #9
Posted 29 January 2013 - 04:43 AM
if x >= 20 and x <= 30 and y >= 8 and y <= 10 then
Jman #10
Posted 29 January 2013 - 04:58 AM
if x >= 20 and x <= 30 and y >= 8 and y <= 10 then
they are seprate inputs , all three of them. Edit: Notice the print commands they identify separate buttons, and when I did the first and second examples the program just returned the text "multiple lines"