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

How to launch CraftOS in a custom OS

Started by Obnoxious_Ninja, 02 July 2014 - 05:32 PM
Obnoxious_Ninja #1
Posted 02 July 2014 - 07:32 PM
I'm currently working on an OS, and I wanted to add a button that allows a user to launch CraftOS. I tried experimenting with the multishell API so the OS could still be open, but I'm not entirely certain on how to use it. I currently have this:


local tabID = multishell.launch({}, "rom/programs/shell")
  multishell.setTitle(tabID, "Shell")

It does this just fine; it launches a second shell with the terminal, while maintaining the original. However, it also gives me an error that says "OS.MAIN: 66: attempt to compare nil with number". I understand what the error means, but I have no idea why it's even going to that section of the script. This is all the related code:

while true do
local event, button, x, y = os.pullEventRaw()
  if slc == 0 then
	if event == "mouse_click" then
	  if x >= 2 and x <= 8 and y == 1 and button == 1 then --Start menu
		GUI_API.drawMenuStart()
		slc = 1

   elseif x >= 17 and x <= 25 and y == 1 and button == 1 then --Console
	 local tabID = multishell.launch({}, "rom/programs/shell")
  multishell.setTitle(tabID, "Shell")
  slc = 1

	  elseif x >= 27 and x <= 33 and y == 1 and button == 1 then --Games
	 GUI_API.drawMenuGames()
  slc = 1

   elseif x >= 2 and x <= 5 and y >= 3 and y <= 9 and button == 1 then --Icon
		shell.run("file")

   elseif x >= 1 and y >=2 and button == 2 then slc = 2 --For right clicks
			if x >= 38 then
			  contextX = 38
			end
			if y >= 14 then
			  contextY = 14
			end
			if x <= 38 then
			  contextX = x
			end
			if y <= 14 then
			  contextY = y
			end
			GUI_API.drawMenuContext(contextX, contextY)
		else
		GUI_API.drawDesktop()
	  end
   end

   elseif slc == 1 then
	if x >= 1 and x <= 11 and y == 3 and button == 1 then slc = 0 --For Shutdown button
	  os.shutdown()  --THIS IS LINE 66!
  
	elseif x >= 1 and x <= 11 and y == 4 and button == 1 then slc = 0 --For Restart button
	  os.reboot()

	elseif x >= 1 and x <= 11 and y == 5 and button == 1 then slc = 0 --For the LuaIDE button
   shell.run("LuaIDE")

elseif x >= 1 and x <= 11 and y == 6 and button == 1 then slc = 0 --For the New account button
   shell.run("new_account")

	elseif x >= 1 and x <= 11 and y == 8 and button == 1 then slc = 0 --For the Uninstall button
   shell.run("uninstall")

	else
	  slc = 0
	  GUI_API.drawDesktop()
	end
   elseif slc == 2 then
		if x >= contextX and x <= contextX+13 and y == contextY+1 and button == 1 then slc = 0 --Edit GUI
		  shell.run("edit", "GUI_API")
		  GUI_API.drawDesktop()

		  elseif x >= contextX and x <= contextX+13 and y == contextY+2 and button == 1 then slc = 0 --Edit Background
			shell.run("paint", ".background")
   GUI_API.drawDesktop()
  
	elseif x >= contextX and x <= contextX+13 and y == contextY+3 and button == 1 then slc = 0 --Edit icon
			shell.run("paint", ".icons")
   GUI_API.drawDesktop()
  
	elseif x >= contextX and x <= contextX+13 and y == contextY+4 and button == 1 then slc = 0 --File Manager
	  shell.run("file") --Does not exist yet
			GUI_API.drawDesktop()
  
		  else slc = 0
			GUI_API.drawDesktop()
	  end
  end
end

Any help would be much appreciated. Also, if I'm unclear about something (which is quite common when I ask for help) please tell me so I may clarify!
Lyqyd #2
Posted 02 July 2014 - 07:55 PM
You aren't ensuring that the event is a mouse click event at that point, so any event with fewer parameters would cause that error.
Obnoxious_Ninja #3
Posted 02 July 2014 - 08:13 PM
When I first check if the event is mouse_click, I set "slc" to 1. Line 66 is in the block that firsts checks if slc == 1, so it essentially is checking for mouse_click event (since the only place where slc can become one is in the "if event == "mouse_click" block), right? Am I misunderstanding this?
Edited on 02 July 2014 - 06:24 PM
Lyqyd #4
Posted 02 July 2014 - 08:51 PM
Your if blocks aren't set up to do that, though. They're like this:


get event
if slc == 0 then
  if mouse click then
    if other condition then
      slc = 1
    end
  end
elseif slc == 1 then
  if condition that assumes a mouse click without verifying that it is then
  end
end

So you get a mouse click in with slc being 0, it gets set to one, then a non-mouse-click event comes in, and your code attempts to compare against x and y without verifying that you actually got a mouse click.
Obnoxious_Ninja #5
Posted 02 July 2014 - 09:01 PM
Your if blocks aren't set up to do that, though. They're like this:


get event
if slc == 0 then
  if mouse click then
	if other condition then
	  slc = 1
	end
  end
elseif slc == 1 then
  if condition that assumes a mouse click without verifying that it is then
  end
end

So you get a mouse click in with slc being 0, it gets set to one, then a non-mouse-click event comes in, and your code attempts to compare against x and y without verifying that you actually got a mouse click.
Oooohhh. I see now. Since I don't actually even need slc in console, I just removed it and it worked. I literally just copy-pasted from one of the above elseif's, and didn't realize that I wouldn't need it. Thanks for the help!