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

[Lua] Problem with custom program just quitting when drawing onto monitor

Started by cooprocks123e, 14 April 2012 - 07:59 PM
cooprocks123e #1
Posted 14 April 2012 - 09:59 PM
Hello! I am trying to write a game for a computer. I am trying to make it so that not only will it display on the terminal, but also a monitor. My issue is that my program here just exits when run with monitor=true, but draws the screen when monitor=false.

scale=1
monitor=true
side="left"

disp=nil
w,h=0
screenArr={}

function draw()
for dY=0,h do
  disp.setCursorPos(1,dY+1)
  for dX=0,w do
   disp.write(screenArr[dX+(dY*w)])
  end
end
end
function tsleep(length)
os.setAlarm(os.time()+length/1000)
local tExit=true
while tExit do
  local event=os.pullEvent()
  if event=="alarm" then
   tExit=false
  end
end
end

if monitor then
disp=peripheral.wrap(side)
disp.setTextScale(scale)
else
disp=term
end
print(disp)
disp.clear()
w,h=disp.getSize()
for i=1,w+(h*w) do
screenArr[i]="#"
end

for loc=1,w+(h*w) do
print("Starting!")
screenArr[loc]="/"
print("Start drawing!")
draw()
print("Done drawing!")
tsleep(1)
print("Ending!")
end
print("Done!")

Most of the print lines in there are meant to show when it stops running. It stops running during the draw.
Please help, and thanks in advance.
Cloudy #2
Posted 14 April 2012 - 10:35 PM
To be honest, the easiest way to make your program exclusively print to a monitor is term.replace(disp) and then when you're done, term.restore(). That way you can use existing commands like print() and write() with no problems.
Wolvan #3
Posted 14 April 2012 - 10:46 PM
wasn't it term.redirect?

monitor = peripheral.wrap(side)
term.redirect(monitor)
Cloudy #4
Posted 14 April 2012 - 11:24 PM
Yes, yes it was. Sorry, I must have got confused when I had a replace term mode when designing the monitors :)/>/>
cooprocks123e #5
Posted 15 April 2012 - 03:47 AM
Thanks for telling me about that. However, my problem still persists. I will post a video of it.

EDIT: Video.

EDIT the 2nd: My code looks like this (has descriptions that will hopefully be helpful.
cant_delete_account #6
Posted 15 April 2012 - 04:59 AM
Try this:

scale=1
monitor=true
side="left"
w,h=0
screenArr={}
function draw()
for dY=0,h do
  disp.setCursorPos(1,dY+1)
  for dX=0,w do
   disp.write(screenArr[dX+(dY*w)])
  end
end
end
function tsleep(length)
os.setAlarm(os.time()+length/1000)
local tExit=true
while tExit do
  local event=os.pullEvent()
  if event=="alarm" then
   tExit=false
  end
end
end
if monitor then
disp = peripheral.wrap(side)
term.redirect(peripheral.wrap(side))
disp.setTextScale(scale)
print(disp)
term.clear()
term.setCursorPos(1,1)
w,h=term.getSize()
for i=1,w+(h*w) do
screenArr[i]="#"
term.restore()
end
for loc=1,w+(h*w) do
print("Starting!")
screenArr[loc]="/"
print("Start drawing!")
draw()
print("Done drawing!")
tsleep(1)
print("Ending!")
end
print("Done!")
MysticT #7
Posted 15 April 2012 - 02:41 PM
The problem is in the way you do the for loops in the draw function. You start them from 0, so the first term.write() will be given the value of the screenArr at the position 0 (dX = 0, dY = 0, dX + (dY * w) = 0), wich is nil and term.write needs a string.
cooprocks123e #8
Posted 15 April 2012 - 04:03 PM
The problem is in the way you do the for loops in the draw function. You start them from 0, so the first term.write() will be given the value of the screenArr at the position 0 (dX = 0, dY = 0, dX + (dY * w) = 0), wich is nil and term.write needs a string.

Thank you very much! I made the changes and it worked. But it still puzzles me why it worked for the terminal and not the monitor.