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

Variable modification

Started by Kibrak, 03 April 2014 - 04:54 AM
Kibrak #1
Posted 03 April 2014 - 06:54 AM
Starting to get back into computercraft again and remembered its all about lua, For some reason I cannot get a variable to change to a different number/text format on a click of a button on the monitor. The 2 swiching buttons actually do change if I manually set it as such, but it refuses to change when you click the button. So in short I am stumped as to why….

Spoiler

side = "top"
backbutton = 0
m = peripheral.wrap(side)
bactive = colors.cyan
binactive=colors.red
tactive=colors.white
tinactive=colors.black
bgcolor = colors.black
buttons = {}
	  function newButton(id,xmin,xmax,ymin,ymax,text,func)
		buttons[id] = {}
		buttons[id]["xmin"] = xmin
		buttons[id]["xmax"] = xmax
		buttons[id]["ymin"] = ymin
		buttons[id]["ymax"] = ymax
		buttons[id]["active"] = false
		buttons[id]["text"] = text
		buttons[id]["func"] = func
	  end
	  function printButton(id)
		ymin = buttons[id]["ymin"]
		ymax = buttons[id]["ymax"]
		xmin = buttons[id]["xmin"]
		xmax = buttons[id]["xmax"]
		text = buttons[id]["text"]
		ia = buttons[id]["active"]
			width = xmax - xmin
			height = ymax - ymin
			if ia then m.setBackgroundColor(bactive) m.setTextColor(tactive)
			else m.setBackgroundColor(binactive) m.setTextColor(tinactive) end
			for j = ymin,ymax do
			 m.setCursorPos(xmin,j)
			  for i = xmin,xmax do
				m.write(" ")
			 end
		   end
		m.setCursorPos(xmin + width / 2 - #text / 2 + 1,ymin + height / 2)
	   m.write(text)
	   m.setBackgroundColor(bgcolor)
	  end
	  function refreshButtons()
		for i = 1,#buttons do
		  printButton(i)
		end
	  end
	function checkxy( x,y )
		for i = 1, #buttons do
		  if y >= buttons[i]["ymin"] and y <= buttons[i]["ymax"] then
			if x >= buttons[i]["xmin"] and x <= buttons[i]["xmax"] then
			  buttons[i]["active"] = not buttons[i]["active"]
			  clicked = i
			  if buttons[i]["func"] ~= nil then
				buttons[i]["func"]()
			  end
			end
		  end
		end
	  end
bool1 = false
bool2 = false
bool3 = false
rs.setBundledOutput("bottom",0)
function maprules()
backbutton = 1
sleep(0.1)
local m = peripheral.wrap(side)
m.setCursorPos(3,5)
m.setTextScale(1)
m.setTextColor(0x400)
m.write("1. Stay in GameMode 2 at all times")
m.setCursorPos(3,6)
m.write("2. Read all lore books they give information")
m.setCursorPos(3,7)
m.write("3. All puzzles are solveable...some are very difficult")
m.setCursorPos(3,8)
m.write("4. Turn off all types of minimap mods")
m.setCursorPos(5,9)
m.write("(this includes: Zans, Opis, Voxel, X-Ray)")
m.setCursorPos(3,10)
m.write("5. Do not remove Computercraft, it kills the map D:")
m.setCursorPos(11,13)
m.write("For full experience and most fun please use")
m.setCursorPos(16,14)
m.write("DireWolf20 FTB modpack 1.6.4")
sleep(1)
term.setCursorPos(1,1)
end
function reboot()
os.reboot()
end
function testingz()
backbutton = 1
end
newButton(1,2,16,2,2,"Map Rules",maprules)
if (backbutton == 1) then
newButton(2,42,56,25,25,"Back",reboot)
elseif (backbutton == 0) then
newButton(2,42,56,25,25,"Start Map",reboot)
end
newButton(3,12,26,4,4,"Testing",testingz)
m.clear()
refreshButtons()
while true do
e,side,x,y = os.pullEvent("monitor_touch")
checkxy(x,y)

refreshButtons()
end
CometWolf #2
Posted 03 April 2014 - 06:37 PM
The part where you react to the change in your variable never happens, because it's outside the loop where you change it

if (backbutton == 1) then
newButton(2,42,56,25,25,"Back",reboot)
elseif (backbutton == 0) then
newButton(2,42,56,25,25,"Start Map",reboot)
end
Should be inside the while true loop, or inside your refreshButtons function.
Kibrak #3
Posted 03 April 2014 - 07:22 PM
The part where you react to the change in your variable never happens, because it's outside the loop where you change it

if (backbutton == 1) then
newButton(2,42,56,25,25,"Back",reboot)
elseif (backbutton == 0) then
newButton(2,42,56,25,25,"Start Map",reboot)
end
Should be inside the while true loop, or inside your refreshButtons function.

When I put that bit inside of a while true loop either the computer breaks to a point I have to create a new one and resetup the script or the buttons do not work at all. :/
CometWolf #4
Posted 03 April 2014 - 07:39 PM
Where exactly are you putting it? As it should not do that.
Kibrak #5
Posted 03 April 2014 - 07:46 PM
I have put it in the maprules function (use to have a while true function but that broke the buttons from ever displaying when the text shows), Gave it its own while true function (that broke the computer), I put it in the already existing while true and that broke the computer to a point I had to reload the map. And putting it in the refreshButtons function itself just caused it to do nothing at all (meaning no change).
CometWolf #6
Posted 03 April 2014 - 08:02 PM
Obviously you don't put it in it's own while true loop. You realize what those do, right? Provided your computer does not yield whilst in one after a short time it will crash. os.pullEvent yields, so putting it along with that should work just fine.
Kibrak #7
Posted 03 April 2014 - 11:11 PM
Obviously you don't put it in it's own while true loop. You realize what those do, right? Provided your computer does not yield whilst in one after a short time it will crash. os.pullEvent yields, so putting it along with that should work just fine.
ugg I get what you mean, kinda feel like a derp over something very simple….this happens to me alot while coding…(even in my own minecraft mod) thanks for pointing it out x.X