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

Reading from write.

Started by Enderslime, 20 October 2015 - 07:30 PM
Enderslime #1
Posted 20 October 2015 - 09:30 PM
Is there a way to take an input from write?
Whenever i use read(), it only takes user input,
I want to be able to receive input from my program use write.

term.clear()
term.setCursorPos(1,1)
print("1 2 3")
print("4 5 6")
print("7 8 9")
print("+ -")
print("* /")

while true do
local event, button, x, y = os.pullEvent("mouse_click")
if x == 1 and y == 1 then
write("1")
elseif x == 3 and y == 1 then
write("2")
input = read()
end
end
Bomb Bloke #2
Posted 20 October 2015 - 10:46 PM
No, you can't get any information from write(), and I'm not sure I understand why you'd want to. Why not just do "input = 2", if you already know the user clicked on the number 2?
Enderslime #3
Posted 20 October 2015 - 10:51 PM
I wanted it to be so that if I clicked 1 it would display 1, then if I clicked 2 it would display 12, then I wanted to take that information and make it a variable.
KingofGamesYami #4
Posted 20 October 2015 - 11:06 PM
You could use concating to add the variables together.
An example:

local var = ""

while true do
  local input = read()
  var = var .. input
  print( var )
end
Edited on 20 October 2015 - 09:06 PM
TYKUHN2 #5
Posted 20 October 2015 - 11:10 PM

term.clear()
term.setCursorPos(1,1)
print("1 2 3")
print("4 5 6")
print("7 8 9")
print("+ -")
print("* /")

while true do
    numstore = ""
	local event, button, x, y = os.pullEvent("mouse_click")
	if x == 1 and y == 1 then
		numstore = numstore.."1"
	elseif x == 3 and y == 1 then
		numstore = numstore.."2"
	end
end

I would also recommend speeding up the translation of mouseclick to number, though not sure how would be the best way. And remember to tonumber(numstore) Generally math autoconverts numbers I believe.
Edited on 20 October 2015 - 09:13 PM
KingofGamesYami #6
Posted 20 October 2015 - 11:11 PM
Please note that TYKUHN2's code will not work because he has declared numstore as local to the while loop.
TYKUHN2 #7
Posted 20 October 2015 - 11:13 PM
I intenteded the while true do loop to overwrite it when he is done with the number, I forgot it would overwrite it every iteration. Fixing.
Enderslime #8
Posted 21 October 2015 - 12:08 AM
alright, got that working, but I have a new problem.

function draw()
term.clear()
term.setCursorPos(1,1)
print("1 2 3")
print("4 5 6")
print("7 8 9")
print("+ -")
print("* /")
end
draw()
ns = 0
while true do
local event, button, x, y = os.pullEvent("mouse_click")
if x == 1 and y == 1 then
ns = ns.."1"
draw()
print(ns)

end
end

If I use this, it adds a zero to the beginning, if I leave ns empty or use nil i get an error about adding to nil. (I know why it adds a zero i just want to know how to remove it, from the variable entirely)
Enderslime #9
Posted 21 October 2015 - 01:32 AM
figured it out.
KingofGamesYami #10
Posted 21 October 2015 - 01:37 AM
Just in case anyone is reading this at a later date, the solution would be to set ns to an empty string, like this:

local ns = ""
Enderslime #11
Posted 21 October 2015 - 02:31 AM
So! it is done! First, type this into your pocket computer, or any computer

pastebin get 0wkqTE1A calc

then type

edit startup

then type,

shell.run("calc")

save and exit, then reboot.