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

variable Variable Name

Started by Vreezer, 21 July 2014 - 10:09 AM
Vreezer #1
Posted 21 July 2014 - 12:09 PM
Hey I made this Touchscreen Programm and I am wondering if I could make a variable Variable Name for Example I got this function and everytime I use this function I want to save the first x and y coordinates in x1 and y1 or x2 and y2 and so on.


function writeButton(Name,x,y,Bg,Txt)
	 Buttons = Buttons+1
	 mon.setBackgroundColor(colors[Bg])
	 mon.setTextColor(colors[Txt])
	 mon.setCursorPos(x,y)
	 i = #Name
	 x[Buttons] = x -- x1, x2, ...
	 y[Buttons] = y -- y1, y2, ...
	 while i>0 do
		mon.write(" ")
		i = i-1
	 end
	 y = y+1
	 mon.setCursorPos(x,y)
	 mon.write(Name)
	 y = y+1
	 mon.setCursorPos(x,y)
	 i = #Name
	 while i>0 do
		mon.write(" ")
		i = i-1
	 end
end

I know it doesn't work like that but is it possible anyhow?
hilburn #2
Posted 21 July 2014 - 01:24 PM
Tables can do this in a couple of ways, either firstx[1]=x1, firstx[2]=x2 etc or you could do coordinates["x"..1]=x1, coordinates["y"..1]=y1 etc
Vreezer #3
Posted 21 July 2014 - 01:33 PM
Thanks I changed my code to the following:


function writeButton(Name,x,y,Bg,Txt)
	 Buttons = Buttons+1
	 mon.setBackgroundColor(colors[Bg])
	 mon.setTextColor(colors[Txt])
	 mon.setCursorPos(x,y)
	 i = #Name
	 xt = x-1
	 yt = y-1
	 firstX[""..Buttons..""] = xt
	 firstY[""..Buttons..""] = yt
	 while i>0 do
		mon.write(" ")
		i = i-1
	 end
	 y = y+1
	 mon.setCursorPos(x,y)
	 mon.write(Name)
	 y = y+1
	 mon.setCursorPos(x,y)
	 i = #Name
	 while i>0 do
		mon.write(" ")
		i = i-1
	 end
	 xt = x+1
	 yt = y+1
	 secondX[""..Buttons..""] = xt
	 secondY[""..Buttons..""] = yt
end

But now I want to make use of those coordinates in the following code


while true do
event,top,x,y=os.pullEvent("monitor_touch")
if x>firstX[1] and x<secondX[1] and y>firstY[1] and y<secondY[1] then

writeButton("   Nein   ",2,2,"yellow","red")

end
end

The Error I get is "attempt to compare __l t on nil and number"
Could you help me out?
Edited on 21 July 2014 - 12:12 PM
Lyqyd #4
Posted 21 July 2014 - 03:25 PM
If you don't mind using another API for the buttons, you might want to try out my Touchpoint API, over in the APIs and Utilities section. If you're dead set on making everything yourself, you're going to want to change how your buttons are set up. Separate tables for x and y coordinates are going to make things more difficult than necessary. I'm also really not sure what's going on with concatenating empty strings on to either side of a variable.
Vreezer #5
Posted 21 July 2014 - 03:30 PM
Here is the full Code I have written yet:


mon = peripheral.wrap("top")
mon.setTextScale(0.5)
mon.setTextColor(colors.pink)
mon.setBackgroundColor(colors.pink)
mon.clear()
mon.setBackgroundColor(colors.black)
Buttons = 0
firstX = {}
firstY= {}
secondX = {}
secondY = {}
function writeButton(Name,x,y,Bg,Txt)
	 Buttons = Buttons+1
	 mon.setBackgroundColor(colors[Bg])
	 mon.setTextColor(colors[Txt])
	 mon.setCursorPos(x,y)
	 i = #Name
	 xt = x-1
	 yt = y-1
	 firstX[""..Buttons..""] = xt
	 firstY[""..Buttons..""] = yt
	 while i>0 do
		mon.write(" ")
		i = i-1
	 end
	 y = y+1
	 mon.setCursorPos(x,y)
	 mon.write(Name)
	 y = y+1
	 mon.setCursorPos(x,y)
	 i = #Name
	 while i>0 do
		mon.write(" ")
		i = i-1
	 end
	 xt = x+1
	 yt = y+1
	 secondX[""..Buttons..""] = xt
	 secondY[""..Buttons..""] = yt
end
function Feedback() -- Kommt noch
end
writeButton("	 Haupttor	",3,2,"black","pink")
writeButton("	 Hintertor   ",21,2,"black","pink")
writeButton("  Maschinenraum  ",39,2,"black","pink")
writeButton("	  Yoshi	  ",3,6,"black","pink")
writeButton("	Reaktortor   ",21,6,"black","pink")
mon.setBackgroundColor(colors.black)
mon.setCursorPos(1,10)
mon.write("														 ")

while true do
event,top,x,y=os.pullEvent("monitor_touch")
if x>firstX[1] and x<secondX[1] and y>firstY[1] and y<secondY[1] then
writeButton("   Nein   ",2,2,"yellow","red")

end
end

I am coding LUA since yesterday so don't be mad at me if it looks confusing.

Is your Touchpoint API usable in Multiplayer too?

//Edit: I didnt want to use your APIs because I am not that far into ComputerCraft so I want to learn more myself.
To finish that little Project I made That pretty long but basic Code.
Spoiler

mon = peripheral.wrap("top")
mon.setTextScale(0.5)
mon.setTextColor(colors.pink)
mon.setBackgroundColor(colors.pink)
mon.clear()
mon.setBackgroundColor(colors.black)
Buttons = 0
firstX = {}
firstY= {}
secondX = {}
secondY = {}
B1 = 1
B2 = 1
B3 = 1
B4 = 1
B5 = 1

function writeButton(Name,x,y,Bg,Txt)
	 Buttons = Buttons+1
	 mon.setBackgroundColor(colors[Bg])
	 mon.setTextColor(colors[Txt])
	 mon.setCursorPos(x,y)
	 i = #Name
	 while i>0 do
		mon.write(" ")
		i = i-1
	 end
	 y = y+1
	 mon.setCursorPos(x,y)
	 mon.write(Name)
	 y = y+1
	 mon.setCursorPos(x,y)
	 i = #Name
	 while i>0 do
		mon.write(" ")
		i = i-1
	 end
end

writeButton("	 Haupttor	",3,2,"black","pink")
writeButton("	 Hintertor   ",21,2,"black","pink")
writeButton("  Maschinenraum  ",39,2,"black","pink")
writeButton("	  Yoshi	  ",3,6,"black","pink")
writeButton("	Reaktortor   ",21,6,"black","pink")

mon.setBackgroundColor(colors.black)
mon.setCursorPos(1,10)
mon.write("														 ")

while true do
event,top,x,y=os.pullEvent("monitor_touch")

if x>2 and x<20 and y>1 and y<5 then
	if B1==1 then
	   writeButton("	 Haupttor	",3,2,"gray","pink")
	   B1 = 0
	else
	   writeButton("	 Haupttor	",3,2,"black","pink")
	   B1 = 1
	end

elseif x>20 and x<38 and y>1 and y<5 then
	if B2==1 then
	   writeButton("	 Hintertor   ",21,2,"gray","pink")
	   B2 = 0
	else
	   writeButton("	 Hintertor   ",21,2,"black","pink")
	   B2 = 1
	end

elseif x>38 and x<46 and y>1 and y<5 then
	if B3==1 then
	   writeButton("  Maschinenraum  ",39,2,"gray","pink")
	   B3 = 0
	else
	   writeButton("  Maschinenraum  ",39,2,"black","pink")
	   B3 = 1
	end

elseif x>2 and x<20 and y>5 and y<9 then
	if B4==1 then
	   writeButton("	  Yoshi	  ",3,6,"gray","pink")
	   B4 = 0
	else
	   writeButton("	  Yoshi	  ",3,6,"black","pink")
	   B4 = 1
	end

elseif x>20 and x<38 and y>5 and y<9 then
	if B5==1 then
	   writeButton("	Reaktortor   ",21,6,"gray","pink")
	   B5 = 0
	else
	   writeButton("	Reaktortor   ",21,6,"black","pink")
	   B5 = 1
	end
	end
end
Edited on 21 July 2014 - 02:22 PM