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

Program Exits Prematurely

Started by ElmuKelmuZ, 20 May 2013 - 01:22 PM
ElmuKelmuZ #1
Posted 20 May 2013 - 03:22 PM
Problems with the code:
in the "mainframe" program, it should wait for a key press event, and respond to that, however, as soon as it has printed the lines it should in "frame()", it exits all programs into the craftOS shell.

The program structure is as follows:
-Advanced computer
–"startup"
-Disk drive(connected to the advanced computer)
–Floppy disk
—"mainframe"

I recommend reading the code here, since its formatted better and easier to read: http://lua.paste.to/MjYxODQ=



--advanced computer/startup:
term.setTextColor(colors.lime)
modem = peripheral.wrap("top")
pumps = false
this = {}
this.id = os.getComputerID()
this.version = os.version()
this.day = os.day()
this.time = os.time()
this.running = false
this.hasDisk = function()
if disk.isPresent("bottom") then
if disk.hasData("bottom") then
return true
else
return false
end
else
return false
end
end
this.asd = 0

function kernelpanic(id, thread)
term.setBackgroundColor(colors.blue)
term.setTextColor(colors.white)
coroutine.yield(thread)
clear()
mbinfo()
write(id.."\n\n")
write("STOP ERROR 0x000002\n\n")
write("Press any key to shut down")
os.pullEvent("char")
os.shutdown()
end


function clear()
term.clear()
term.setCursorPos(1,1)
end

function mbinfo()
write("Absolute Zero Corporation (c) 1953-2013\n\n\n")
end

function info()
clear()
write("Rocket OS - Absolute Zero Corporation (c) 2013\n\n\n")
end

function main()
if this.hasDisk() then
if not this.running then
clear()
mbinfo()
print("Boot device connected.")
sleep(1)
clear()
mbinfo()
print("Running Floppy in 'Disk Drive 0'")
sleep(3)
clear()
mbinfo()
print("Running disk/mainframe")
sleep(1.6)
clear()
mainframeThread = coroutine.resume(coroutine.create(function()
shell.run("disk/mainframe")
end))
this.running = true
sleep(1)
main()
end
else
if not this.running then
clear()
mbinfo()
print("Insert a boot device")
sleep(3)
main()
else
kernelpanic("disk/ is nil!", mainframeThread)
end
end
end

main()


--disk/mainframe:
function frame()
info()
print("[1] - Launch pad control")
print("[2] - Oxygen control")
print("[3] - Bioreactor control")
print("[4] - Reboot the mainframe")
local _,k = os.pullEvent("char")
if k == "1" then
info()
if not pumps then
print("[1] - Enable fuel pumps")
--modem.transmit("launchpad_fuelcontrol_pumps_enable")
else
print("[1] - Disable fuel pumps")
end
print("[2] - Back")
local _,k2 = os.pullEvent("char")
if k2 == "1" then
if not pumps then
modem.transmit("launchpad_fuelcontrol_pumps_enable")
else
modem.transmit("launchpad_fuelcontrol_pumps_disable")
end
elseif k2 == "2" then
frame()
end
elseif k == "2" then
frame()
elseif k == "3" then
frame()
elseif k == "4" then
clear()
write("Rebooting...")
sleep(2)
os.reboot()
end
end

frame()

Lyqyd #2
Posted 20 May 2013 - 03:26 PM
Split into new topic. A title was provided for you.
Grim Reaper #3
Posted 20 May 2013 - 09:58 PM
You're missing an 'end' statement or two which is causing your program to not behave properly.
Pharap #4
Posted 20 May 2013 - 10:28 PM

if disk.isPresent("bottom") then
   if disk.hasData("bottom") then
    return true
  else
   return false
  end
else
  return false
end
end

Returning when not in a function will exit the program
ElmuKelmuZ #5
Posted 21 May 2013 - 11:30 AM

if disk.isPresent("bottom") then
   if disk.hasData("bottom") then
	return true
  else
   return false
  end
else
  return false
end
end

Returning when not in a function will exit the program



[color=#000088]this[/color][color=#666600].[/color][color=#000000]hasDisk [/color][color=#666600]=[/color][color=#000000] [/color][color=#000088]function[/color][color=#666600]()[/color]


As you can see it is a function, look closer ;)/>

You're missing an 'end' statement or two which is causing your program to not behave properly.


I am surely not, that would result in an error
Pharap #6
Posted 21 May 2013 - 02:20 PM


[color=#000088]this[/color][color=#666600].[/color][color=#000000]hasDisk [/color][color=#666600]=[/color][color=#000000] [/color][color=#000088]function[/color][color=#666600]()[/color]


As you can see it is a function, look closer ;)/>

My bad, it's just horribly formatted.
ElmuKelmuZ #7
Posted 24 May 2013 - 05:29 PM
bump ;(
Bomb Bloke #8
Posted 26 May 2013 - 12:32 AM
You want to loop the "frame()" function forever by having it call itself afresh whenever its current work is done, yes? That's a bad thing: a function that calls itself recursively without cease will eventually lead to a crash by overflow (each instance of the function you're using has to be recorded somewhere, and if those instances never end because they're opening up MORE instances, well…).

Instead, allow the frame() function to end, then recall it. That is to say, remove the calls to frame() from within frame() and change line 150 to "while true do frame() end".

As it stands, if the user opts for launchpad control and decides to toggle the fuel pumps, frame() will already not get recalled and so the program simply ends. You probably intended to put another call to the function between lines 134 and 135.