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

Menu system not working

Started by nutcase84, 30 January 2013 - 05:16 AM
nutcase84 #1
Posted 30 January 2013 - 06:16 AM
This code isn't working right:

local event, button, x, y = os.pullEvent("mouse_click")
    if event == "mouse_click" then
	 if x == 1 or x == 2 or x == 3 or x == 4 or x == 5 or x == 6 or x == 7 or x == 8 then
	  if y == 2 then --option 1

	  else
	 
	  if y == 3 then --option 2

	  else
	 
	  if y == 4 then -- option 3

I have tried elseif and if but they both do the same thing, if you click the first option it works, if you click 2 or 3 it doesn't.
Doyle3694 #2
Posted 30 January 2013 - 06:19 AM
you lack…. 4 ends. Also, this was a obvious bug. But you got to give us the error it returns.
OmegaVest #3
Posted 30 January 2013 - 06:20 AM
Query: What are you doing to test this? Or rather, what are the contents of the if blocks?

It may not be the conditional statements that are failing, but something within them that is causing them to break instead of going to the next conditional.
nutcase84 #4
Posted 30 January 2013 - 06:21 AM
There is no error, it just doesn't work. Like i said, and i have all the ends in my program.
Doyle3694 #5
Posted 30 January 2013 - 06:23 AM
Can you quote yourself where you say all the ends are in the program? And maybe you should give us some info? I recon "This code isn't working right" is not enough. Also, post full code. Can't stress that enough. We need full code to help you.
nutcase84 #6
Posted 30 January 2013 - 06:24 AM
Query: What are you doing to test this? Or rather, what are the contents of the if blocks?

It may not be the conditional statements that are failing, but something within them that is causing them to break instead of going to the next conditional.
I don't understand. English please. My coding skills aren't that great, and I don't talk THAT geeky.
TheOddByte #7
Posted 30 January 2013 - 06:25 AM
And why don't you use "elseif's" instead?
nutcase84 #8
Posted 30 January 2013 - 06:26 AM
Can you quote yourself where you say all the ends are in the program? And maybe you should give us some info? I recon "This code isn't working right" is not enough. Also, post full code. Can't stress that enough. We need full code to help you.

--Vars
ver = "0.0.1"
function drawDesktopBackground(x, y)
desktopBackground = paintutils.loadImage("//.sys/background")
paintutils.drawImage(desktopBackground, x, y)
end
totalTicks = 0 --Resets tick counter
--Main loop
while true do
totalTicks = totalTicks + 1 --Tick counter
term.setBackgroundColor(colors.white)
term.clear()
drawDesktopBackground(13, 2) --Draws desktop background
paintutils.drawLine(1, 1, 99, 1, colors.gray) --Draws menu bar
term.setTextColor(colors.black)
term.setCursorPos(1, 1)
print("[Menu]							Total Ticks:", totalTicks) --Draws menu button and tick counter
local event, button, x, y = os.pullEvent("mouse_click")
if event == "mouse_click" then
  if x == 1 or x == 2 or x == 3 or x == 4 or x == 5 or x == 6 then
   if y == 1 then
	paintutils.drawLine(1, 1, 6, 1, colors.lightGray)
	paintutils.drawLine(1, 2, 8, 2, colors.lightGray)
	paintutils.drawLine(1, 3, 8, 3, colors.lightGray)
	paintutils.drawLine(1, 4, 8, 4, colors.lightGray)
	term.setCursorPos(1, 1)
	print("[Menu]")
	print("Programs")
	print("Settings")
	print("Run")
	local event, button, x, y = os.pullEvent("mouse_click")
	if event == "mouse_click" then
	 if x == 1 or x == 2 or x == 3 or x == 4 or x == 5 or x == 6 or x == 7 or x == 8 then
	  if y == 2 then --Programs
	   term.setBackgroundColor(colors.white)
	   term.clear()
	   paintutils.drawLine(1, 1, 99, 1, colors.gray)
	   term.setTextColor(colors.black)
	   term.setCursorPos(1, 1)
	   print("[Close]							Total Ticks:", totalTicks)
	   paintutils.drawLine(17, 10, 24, 10, colors.white)
	   term.setCursorPos(17, 10)
	   print("Nothing to display")
	   while true do
		local event, button, x, y = os.pullEvent("mouse_click")
		if event == "mouse_click" then
		 if x == 1 or x == 2 or x == 3 or x == 4 or x == 5 or x == 6 or x == 7 then
		  if y == 1 then
		   break
	  else
	
	  if y == 3 then --Settings
	   print("TEST")
	   sleep(3)
	  else
	
	  if y == 4 then --Run
	   term.setBackgroundColor(colors.white)
	   term.clear()
	   paintutils.drawLine(1, 1, 99, 1, colors.gray)
	   term.setTextColor(colors.black)
	   term.setCursorPos(1, 1)
	   print("								  Total Ticks:", totalTicks)
	   paintutils.drawLine(17, 10, 24, 10, colors.white)
	   term.setCursorPos(17, 10)
	   write("Run: ")
	   runcmd = read()
	   shell.run(runcmd)
	
		  end
		 end
		end
	   end
	  end
	 end end
	end	 end
   end
  end
end
end

And why don't you use "elseif's" instead?
I said I tried that.
nutcase84 #9
Posted 30 January 2013 - 06:39 AM
Just messed with the code some more: found out that it handles options 2 and 3 as if you were clicking off the menu to close the menu. Something is badly wrong.
Doyle3694 #10
Posted 30 January 2013 - 06:46 AM
all your ends are at the bottom. Place them properly and say if it works :)/>
TheOddByte #11
Posted 30 January 2013 - 06:49 AM
Oh sorry did'nt read that..
OmegaVest #12
Posted 30 January 2013 - 06:49 AM
all your ends are at the bottom. Place them properly and say if it works :)/>

Thing is, they are placed properly, since he di elses instead of elseifs. You would cut down on the amount of ends with elseifs, and if it does the same thing, smaller is better. Easier to handle.
nutcase84 #13
Posted 30 January 2013 - 06:52 AM
Tested proper ends, still does the same thing.
Doyle3694 #14
Posted 30 January 2013 - 06:58 AM
can you post your code with proper ends?
remiX #15
Posted 30 January 2013 - 07:21 AM
You had the elseif's in the wrong place.


--Vars
ver = "0.0.1"
screenX, screenY = term.getSize()

function drawDesktopBackground(x, y)
desktopBackground = paintutils.loadImage("//.sys/background")
paintutils.drawImage(desktopBackground, x, y)
end
totalTicks = 0 --Resets tick counter

--Main loop
while true do
	totalTicks = totalTicks + 1 --Tick counter
	term.setBackgroundColor(colors.white)
	term.clear()
	--drawDesktopBackground(13, 2) --Draws desktop background
	paintutils.drawLine(1, 1, 99, 1, colors.gray) --Draws menu bar
	term.setTextColor(colors.black)
	term.setCursorPos(1, 1)
	print("[Menu]")
	local event, button, x, y = os.pullEvent("mouse_click")
	if event == "mouse_click" then
		if x >= 1 and x <= 6 then
			if y == 1 then
				paintutils.drawLine(1, 1, 6, 1, colors.lightGray)
				paintutils.drawLine(1, 2, 8, 2, colors.lightGray)
				paintutils.drawLine(1, 3, 8, 3, colors.lightGray)
				paintutils.drawLine(1, 4, 8, 4, colors.lightGray)
				term.setCursorPos(1, 1)
				print("[Menu]")
				print("Programs")
				print("Settings")
				print("Run")
				b = false
				local event, button, x, y = os.pullEvent("mouse_click")
				if event == "mouse_click" then
					if x >= 1 and x <= 8 then
						term.setBackgroundColor(colors.white)
						term.clear()
						paintutils.drawLine(1, 1, screenX, 1, colors.gray)
						term.setTextColor(colors.black)
						term.setBackgroundColour(colours.white)
						term.setCursorPos(17, 10)
						if y == 2 then --Programs
							b = true
							print("Nothing to display")
						elseif y == 3 then -- settings
							b = true
							print("TEST")
						elseif y == 4 then -- run
							b = true
							write("Run: ")
							runcmd = read()
							shell.run(runcmd)
						end
						if b then
							term.setCursorPos(1, 1)
							term.setBackgroundColour(colours.grey)
							print("[Close]")
							while true do
								_, but, x, y = os.pullEvent("mouse_click")
								if x >= 1 and x <= 7 then -- To close!
									break
								end
							end
						end
					end
				end
			end
		end
	end
end
nutcase84 #16
Posted 31 January 2013 - 06:51 AM
Thank you!!!!!! It works!!!!!!
Persona #17
Posted 03 June 2013 - 02:20 AM
The OS looks and sounds pretty good. Good luck. :)/>