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

Too long without yielding

Started by stene.m, 02 November 2014 - 10:34 AM
stene.m #1
Posted 02 November 2014 - 11:34 AM
Hi

My first time posting here, so bear with me and try to keep things simple.

I have worked with a code for a while now, which is composed of 3 different that I have found online.
Consisting of a boot sequence, a secure login and a menu system … but the start and log in is working properly, but the menu I just get a "bios: 32: Too long without yielding"

Any ideas on what could be wrong ..

PS the code contains some notes that i have made for myself… and the print part in odd security login looks strange when i previewed my post, but its ok in mindcraft….




os.pullEvent = os.pullEventRaw
term.clear()
term.setCursorPos (1, 2)
-------Startup-------
  print("")
  print("--------------------------------------------------")
  term.setCursorPos (16, 8)
  print(" ---------------")
  term.setCursorPos (16, 9)
  print("| DoomOS Engine |")
  term.setCursorPos (16, 10)
  print(" ---------------")
  term.setCursorPos (1,20)
  print("--------------------------------------------------")
  sleep(1)
  term.setCursorPos (30,16)
  print("Copyright Mort96 inc")
  sleep(2)
  term.setCursorPos (20, 14)
  print("Loading.")
  sleep(1)
  term.setCursorPos (20, 14)
  print("Loading..")
  sleep(1)
  term.setCursorPos (20, 14)
  print("Loading...")
  sleep (3)
  term.clear()
-------odd security login-------
  os.pullEvent = os.pullEventRaw
  term.clear()
  term.setCursorPos(1,1)
   p = "stene"
   r = "morten"
   print("")
   print("")
   print("")
   print("")
   print("")
   print("			   odd Security v1.4.5			")
   print("			+-----------------------+			 ")
   print("				Username:						 ")
   print("				Password:						  ")
   print("			+-----------------------+			 ")
   term.setCursorPos(26,8)
   print("Admin")
   term.setCursorPos (29,16)
   print("Copyright odd_kid inc")
   term.setCursorPos(26,9)
   pt = read("*")
if pt == (p) then
   term.setCursorPos(1,11)
   print("Logging in"); term.setCursorPos(11,11); textutils.slowPrint("...",1)
   term.setCursorPos(1,12)
   sleep(2)
   print("Login succesful!")
   sleep(1)
   print("Welcome Morten!")
   sleep(2)
   term.clear()
   term.setCursorPos(1,1)
   print("Preparing DoomOS")
   sleep(1)
   term.setCursorPos(1,2)
   print("Reading Files"); term.setCursorPos(14,2); textutils.slowPrint("...",1)
   sleep(1)
   term.setCursorPos(1,3)
   print("Loading menu"); term.setCursorPos(13,3); textutils.slowPrint("...",1)
   sleep(1)
   term.setCursorPos(1,4)
   print("Startup Succesful!")
   sleep(2)
   term.clear()  
else
   term.setCursorPos(1,11)
   print("Logging in"); term.setCursorPos(11,11); textutils.slowPrint("...",1)
   sleep(1)
   term.setCursorPos(1,12)
   print("Incorrect Password! This sesion will be terminated"); term.setCursorPos(1,14)
   sleep(2)
   print("3"); term.setCursorPos(1,14)
   sleep(1)
   print("2"); term.setCursorPos(1,14)
   sleep(1)
   print("1"); term.setCursorPos(1,14)
   sleep(1)
   os.shutdown()
end
-------Menyer-------
  os.pullEvent = os.pullEventRaw
  term.clear()
  pos = 1
  menu = true
  while menu do
   term.setCursorPos (1, 1)
   print("Welcome to the Main Control Center")
   print("")
   print("Please select a task")
   term.setCursorPos (1, 5)
   print(" ----------------- ")
   print("| Lights ON	   |")
   print("| Lights OFF	  |")
   print("| --------------- |")
   print("| Maintenance door|")
   print("| --------------- |")
   print("| Shutdown		|")
   print(" ----------------- ")
	if num == 200 then
	 pos = pos - 1
	end
  
	if num == 208 then
	 pos = pos + 1
	end
  
	if num == 28 then
	 menu = false
	end
  
	if pos > 4 then --------------------Endre
	 pos = 4 --------------------------Endre
	 elseif pos < 1 then
	 pos = 1
	end
  if pos == 1 then ------------------------Lys på
	 term.setCursorPos (19, 6)
	 print("<--")
  end
  if pos == 2 then ------------------------Lys av
	 term.setCursorPos (19, 7)
	 print("<--")
  end
  if pos == 3 then ------------------------Maintenance door
	 term.setCursorPos (19, 9)
	 print("<--")
  end
  if pos == 4 then ------------------------Shutdown
	 term.setCursorPos (19, 11)
	 print("<--")
	if num == 28 then
	   os.shutdown()
	  end
	end
  end
type, num = os.pullEvent()
term.clear()
Bomb Bloke #2
Posted 02 November 2014 - 12:06 PM
When a computer/turtle starts running code, ComputerCraft starts a ten second timer. If that code doesn't yield before that timer ends then ComputerCraft will either crash the script or the whole computer (depending on the nature of the functions your script is calling). After each yield, any other systems waiting to run code do so, then after they've all yielded, processing continues with a new time limit.

The reason why is that running your code chews up valuable server processing power, and so it shouldn't be able to monopolise it. In fact, only ONE CC device can run code at a time: While one is doing something, none of the others can do anything until it yields.

Whether or not it takes more than ten seconds for your code to execute has a lot to do with the power of the Minecraft server it's running on, and how often you allow your code to yield. Pulling events (eg getting typed characters or checking timers) triggers a yield, and many commands (eg turtle movements, sleeping, or getting text input from the user) have to pull events to work anyway.

In your script, you have a "while" loop near the bottom which doesn't yield at all. Underneath this loop you call os.pullEvent() (in order to set "type" and "num"), but as you're doing that outside the loop, that line never gets executed.

It's worth noting that the variable "type" usually holds a function pointer (the function in concern returns a string representing the type of variable you pass to it). Best not to overwrite it.
stene.m #3
Posted 02 November 2014 - 06:06 PM
Thanks, it worked =)

Do you have knowledge about loops?

I wish that instead of "os.shutdown", the code return to the menu selector (See the original post) … is there an easy way to do this ??



   if pos == 3 simply ------------------------ Maintenance door
	 term.setCursorPos (19, 9)
	 print ("<-")
	 if num == 28 simply
	   redstone.setBundledOutput ("back", colours.red)
	   term.clear ()
	   print ("Maintenance door are now open, you have 30 seconds to Enter")
	   sleep (3)
	   os.shutdown ()
	 end
   end
KingofGamesYami #4
Posted 02 November 2014 - 06:46 PM

while true do
 --#this will repeat infinately
end

local ok = true
while ok do
  --#this will not repeat
  ok = false
end


while true do
  --#this will not repeat
  break
end
stene.m #5
Posted 02 November 2014 - 07:14 PM

while true do
--#this will repeat infinately
end

local ok = true
while ok do
  --#this will not repeat
  ok = false
end


while true do
  --#this will not repeat
  break
end


I must be honest to say that I'm a rookie in lua and some advanced computer craft coding … Can you be a little more specific about how this will look in the code that was posted originally.
KingofGamesYami #6
Posted 02 November 2014 - 07:59 PM

while true do --#do this forever
   term.setCursorPos (1, 1)
   print("Welcome to the Main Control Center")
   print("")
   print("Please select a task")
   term.setCursorPos (1, 5)
   print(" ----------------- ")
   print("| Lights ON      |")
   print("| Lights OFF    |")
   print("| --------------- |")
   print("| Maintenance door|")
   print("| --------------- |")
   print("| Shutdown            |")
   print(" ----------------- ")
        if num == 200 then
         pos = pos - 1
        end

        if num == 208 then
         pos = pos + 1
        end

        if num == 28 then
         menu = false
        end

        if pos > 4 then --------------------Endre
         pos = 4 --------------------------Endre
         elseif pos < 1 then
         pos = 1
        end
  if pos == 1 then ------------------------Lys på
         term.setCursorPos (19, 6)
         print("<--")
  end
  if pos == 2 then ------------------------Lys av
         term.setCursorPos (19, 7)
         print("<--")
  end
  if pos == 3 then ------------------------Maintenance door
         term.setCursorPos (19, 9)
         print("<--")
  end
  if pos == 4 then ------------------------Shutdown
         term.setCursorPos (19, 11)
         print("<--")
        if num == 28 then
           break --#change to break
          end
        end
  end
end
Bomb Bloke #7
Posted 02 November 2014 - 09:36 PM
Erm, that code chunk is still quite, quite broken. It'll still throw the original error, for starters. The original use of the "menu" variable was already quite sufficient.