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

[HELP] To long without yielding

Started by FUCKCOMPUTERCRAFT!"£, 20 February 2013 - 03:41 AM
FUCKCOMPUTERCRAFT!"£ #1
Posted 20 February 2013 - 04:41 AM
I'm working on a project and was using a function i didn't make. I'm not to sure what is wrong with it but the error says, "26: To long without yield" or something along those lines. I was calling the function from my program, ill include that below.


Read File Function

------------------------------------------------
-- BY TOXIC WOLF
------------------------------------------------
function WriteFile(sText, sPath, nLineNum)
local tFile = {}
local hHandle = fs.open(sPath, "r")
repeat
local sLine = hHandle:readLine()
table.insert(tFile, sLine)
until not sLine
hHandle:close()
tFile[nLineNum] = sText
local hHandle = fs.open(sPath, "w")
for tNum = 1, #tFile do
hHandle.writeLine(tFile[tNum])
end
hHandle:close()
return
end
function ReadFile(sPath, nLineNum)
local tFile = {}
local hHandle = fs.open(sPath, "r")
repeat
local sLine = hHandle:readLine()
table.insert(tFile, sLine)
until not sLine
hHandle:close()
return tFile[nLineNum]
end


My Program

MapToLoad = "test"
shell.run("X_F/GUI/GUI.exe")
x = 1
y = 1
LineNumber = 0
ChangeFromOrginialPostion  = 0
MaxiumLineNumber = 0
MaxiumLineNumber = 19 + ChangeFromOrginialPostion
MaxiumLineNumber = MaxiumLineNumber * 51
DoLoop = true
while DoLoop do

LineNumber = y + ChangeFromOrginialPostion
LineNumber = LineNumber - 1
LineNumber = LineNumber * 51
LineNumber = LineNumber + x
 
LineString = ReadFile(MapToLoad , LineNumber)
 
if LineString == "w" then
  col = colors.white
elseif LineString == "o" then
  col = colors.orange
elseif LineString == "m" then
  col = colors.magenta
elseif LineString == "lbl" then
  col = colors.lightBlue
elseif LineString == "y" then
  col = colors.yellow
elseif LineString == "l" then
  col = colors.lime
elseif LineString == "p" then
  col = colors.pink
elseif LineString == "g" then
  col = colors.gray
elseif LineString == "lg" then
  col = colors.lightGray
elseif LineString == "c" then
  col = colors.cyan
elseif LineString == "p" then
  col = colors.purple
elseif LineString == "bl" then
  col = colors.blue
elseif LineString == "br" then
  col = colors.brown
elseif LineString == "g" then
  col = colors.green
elseif LineString == "r" then
  col = colors.red
elseif LineString == "b" then
  col = colors.black
end
   
paintutils.drawPixel(x,y,col)
x = x + 1
 
if x == 51 then
  y = y + 1
  x = 1
end

if  LineNumber == MaxiumLineNumber then
  DoLoop = false
end
 
end
FuuuAInfiniteLoop(F.A.I.L) #2
Posted 20 February 2013 - 04:45 AM
I'm working on a project and was using a function i didn't make. I'm not to sure what is wrong with it but the error says, "26: To long without yield" or something along those lines. I was calling the function from my program, ill include that below.


Read File Function

------------------------------------------------
-- BY TOXIC WOLF
------------------------------------------------
function WriteFile(sText, sPath, nLineNum)
local tFile = {}
local hHandle = fs.open(sPath, "r")
repeat
local sLine = hHandle:readLine()
table.insert(tFile, sLine)
until not sLine
hHandle:close()
tFile[nLineNum] = sText
local hHandle = fs.open(sPath, "w")
for tNum = 1, #tFile do
hHandle.writeLine(tFile[tNum])
end
hHandle:close()
return
end
function ReadFile(sPath, nLineNum)
local tFile = {}
local hHandle = fs.open(sPath, "r")
repeat
local sLine = hHandle:readLine()
table.insert(tFile, sLine)
until not sLine
hHandle:close()
return tFile[nLineNum]
end


My Program

MapToLoad = "test"
shell.run("X_F/GUI/GUI.exe")
x = 1
y = 1
LineNumber = 0
ChangeFromOrginialPostion  = 0
MaxiumLineNumber = 0
MaxiumLineNumber = 19 + ChangeFromOrginialPostion
MaxiumLineNumber = MaxiumLineNumber * 51
DoLoop = true
while DoLoop do

LineNumber = y + ChangeFromOrginialPostion
LineNumber = LineNumber - 1
LineNumber = LineNumber * 51
LineNumber = LineNumber + x

LineString = ReadFile(MapToLoad , LineNumber)

if LineString == "w" then
  col = colors.white
elseif LineString == "o" then
  col = colors.orange
elseif LineString == "m" then
  col = colors.magenta
elseif LineString == "lbl" then
  col = colors.lightBlue
elseif LineString == "y" then
  col = colors.yellow
elseif LineString == "l" then
  col = colors.lime
elseif LineString == "p" then
  col = colors.pink
elseif LineString == "g" then
  col = colors.gray
elseif LineString == "lg" then
  col = colors.lightGray
elseif LineString == "c" then
  col = colors.cyan
elseif LineString == "p" then
  col = colors.purple
elseif LineString == "bl" then
  col = colors.blue
elseif LineString == "br" then
  col = colors.brown
elseif LineString == "g" then
  col = colors.green
elseif LineString == "r" then
  col = colors.red
elseif LineString == "b" then
  col = colors.black
 [b]sleep(0)[/b]
end
  
paintutils.drawPixel(x,y,col)
x = x + 1

if x == 51 then
  y = y + 1
  x = 1
end

if  LineNumber == MaxiumLineNumber then
  DoLoop = false
end

end
Look at the bold
FUCKCOMPUTERCRAFT!"£ #3
Posted 20 February 2013 - 04:47 AM
Huh?
FuuuAInfiniteLoop(F.A.I.L) #4
Posted 20 February 2013 - 04:49 AM
add sleep(0) to the while loop
theoriginalbit #5
Posted 20 February 2013 - 04:50 AM
Look at the bold
Lol. fail. code tags remove formatting :P/>
FUCKCOMPUTERCRAFT!"£ #6
Posted 20 February 2013 - 04:55 AM
The sleep(0) makes the painting the screen incredibly slow. Maybe I'm being stupid as I haven't programmed in CC in a while but why when the screen is painted why doesn't it let me type in the terminal? Surely that's the end of the program as the loop stopped as it exited the loop as it got the maximum line number?
Lyqyd #7
Posted 20 February 2013 - 06:01 AM
Maybe it's incredibly slow because you're doing I/O completely incorrectly. Why on earth are you opening and closing the file for each pixel?! That's just mind-boggling.
FUCKCOMPUTERCRAFT!"£ #8
Posted 20 February 2013 - 06:13 AM
Maybe it's incredibly slow because you're doing I/O completely incorrectly. Why on earth are you opening and closing the file for each pixel?! That's just mind-boggling.

I/O, what am i doing incorrectly? I'm just using a function already made, ill looking into improving it.
Lyqyd #9
Posted 20 February 2013 - 06:18 AM
Just read the file once and load it up into a table.


local pixelTable = {}
local handle = io.open("filename", "r")
if handle then
  for line in handle:lines() do
    table.insert(pixelTable, line)
  end
else
  error("Could not read file!")
end
theoriginalbit #10
Posted 20 February 2013 - 06:20 AM
By the looks of the code this could be greatly optimised by storing the colours as hexadecimal (0-F) in the file. then you can do 2^tonumber(char,16) to make it into a number. then this means that each line of the gui could be stored in one line in file.
FUCKCOMPUTERCRAFT!"£ #11
Posted 20 February 2013 - 06:28 AM
Just read the file once and load it up into a table.


local pixelTable = {}
local handle = io.open("filename", "r")
if handle then
  for line in handle:lines() do
	table.insert(pixelTable, line)
  end
else
  error("Could not read file!")
end

Lua isn't my strongest lanuage, fancy explainging you're code as im confused on what it does and how i could implement it.

By the looks of the code this could be greatly optimised by storing the colours as hexadecimal (0-F) in the file. then you can do 2^tonumber(char,16) to make it into a number. then this means that each line of the gui could be stored in one line in file.

I was going to bulid a base version then work on optimising.
Lyqyd #12
Posted 20 February 2013 - 08:48 AM
It opens the file in read mode, then reads every line and inserts each line into a table. You can then use a for loop instead of a while loop to read through and get the values for each pixel. You would also be better off storing the values in hex and putting a whole row of pixels in one line of the file.

You'd implement that code by removing the call that reads from a file, putting my code before the loop, changing the loop to `for i = 1, #pixelTable do` and using pixelTable in each iteration of the loop.