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

Help needed

Started by Timo_Ho, 06 October 2014 - 01:14 PM
Timo_Ho #1
Posted 06 October 2014 - 03:14 PM
Program:
shell.run("clear")
function checkforWalls()
x = 1
while true do
  if walls[x][1] == wanttox and walls[x][2] == wanttoy then return false end
  if walls[x] == nil then return true end
  x = x + 1
end
end
function move()
wanttodir = math.random(1,4)
if wanttodir == 1 then wanttox = xpos  wanttoy = ypos - 1 end
if wanttodir == 2 then wanttox = xpos  wanttoy = ypos + 1 end
if wanttodir == 3 then wanttox = xpos - 1  wanttoy = ypos end
if wanttodir == 4 then wanttox = xpos + 1  wanttoy = ypos end
if checkforWalls() then
  xpos = wanttox
  ypos = wanttoy
end
end
function load()
shell.run(read())
end
function draw()
term.setBackgroundColor(colors.green)
term.clear()
term.setCursorPos(1,1)
x = 1
while true do
  term.setBackgroundColor(colors.red)
  term.setCursorPos(walls[x][1],walls[x][2])
  term.write("X")
  x = x + 1
  if walls[x] == nil then break end
end
end
function drawBug()
term.setCursorPos(xpos,ypos)
term.setBackgroundColor(colors.blue)
term.write("#")
end
load()
while true do
term.setBackgroundColor(colors.black)
draw()
term.setBackgroundColor(colors.black)
drawBug()
term.setBackgroundColor(colors.black)
move()
sleep(0,5)
end

level program:

walls = {{1,1},{1,2},{1,3},{1,4},{1,5},{2,5},{3,5},{4,5},{5,5},{5,4},{5,3},{5,2},{5,1},{4,1},{3,1},{2,1}}
xPos = 2
yPos = 2

I started the program and entered the name of the level program. and then this error came:
[string "window"]:247: bad argument #1 to 'floor' (number expected, got nil)

Here's a short video:
Download
Edited on 07 October 2014 - 05:38 PM
Doyle3694 #2
Posted 06 October 2014 - 04:13 PM
Use ['code'] tags

shell.run("clear")
function checkforWalls()
  x = 1
  while true do
	if walls[x][1] == wanttox and walls[x][2] == wanttoy then return false end
	if walls[x] == nil then return true end
	x = x + 1
  end
end
function move()
  wanttodir = math.random(1,4)
  if wanttodir == 1 then wanttox = xpos wanttoy = ypos - 1 end
  if wanttodir == 2 then wanttox = xpos wanttoy = ypos + 1 end
  if wanttodir == 3 then wanttox = xpos - 1 wanttoy = ypos end
  if wanttodir == 4 then wanttox = xpos + 1 wanttoy = ypos end
  if checkforWalls() then
	xpos = wanttox
	ypos = wanttoy
  end
end

function load()
  shell.run(read())
end

function draw()
  term.setBackgroundColor(colors.green)
  term.clear()
  term.setCursorPos(1,1)
  x = 1
  while true do
	term.setBackgroundColor(colors.red)
	term.setCursorPos(walls[x][1],walls[x][2])
	term.write("X")
	x = x + 1
	if walls[x] == nil then break end
  end
end
function drawBug()
  term.setCursorPos(xpos,ypos)
  term.setBackgroundColor(colors.blue)
  term.write("#")
end

load()

while true do
  term.setBackgroundColor(colors.black)
  draw()
  term.setBackgroundColor(colors.black)
  drawBug()
  term.setBackgroundColor(colors.black)
  move()
  sleep(0,5)
end

Ill get to try fixing the problem, just need the code clear in front of me
Edited on 06 October 2014 - 02:14 PM
KingofGamesYami #3
Posted 06 October 2014 - 04:16 PM
[string "window"]:247: bad argument #1 to 'floor' (number expected, got nil)

This means:
The window API erred on line 247, because when it passed a "number" to math.floor, the number was nil. term.setTextColor and term.setBackgroundColor are probable culprits.
Doyle3694 #4
Posted 06 October 2014 - 04:22 PM
It will also malfunction because you have written sleep(0,5) with a comma, its supposed to be sleep(0.5)
KingofGamesYami #5
Posted 06 October 2014 - 05:01 PM
It will also malfunction because you have written sleep(0,5) with a comma, its supposed to be sleep(0.5)

That will not error, simply act as sleep(0). Lua ignores additional arguments.
Doyle3694 #6
Posted 06 October 2014 - 07:03 PM
Exactly, it will malfunction, IE not work as he intended it to
Bomb Bloke #7
Posted 07 October 2014 - 02:20 AM
"xpos" is not the same thing as "xPos". Ditto for ypos/yPos. Capitalisation matters.

As of CC 1.6, the window API is used automatically by advanced computers whenever most term functions are called.
Timo_Ho #8
Posted 07 October 2014 - 07:27 PM
[string "window"]:247: bad argument #1 to 'floor' (number expected, got nil)

This means:
The window API erred on line 247, because when it passed a "number" to math.floor, the number was nil. term.setTextColor and term.setBackgroundColor are probable culprits.

"xpos" is not the same thing as "xPos". Ditto for ypos/yPos. Capitalisation matters.

As of CC 1.6, the window API is used automatically by advanced computers whenever most term functions are called.

Thank you for your answers. I think the Problem is xPos and yPos in the level program. I'll fix it tomorrow.
Timo_Ho #9
Posted 12 October 2014 - 12:33 PM
Program:
shell.run("clear")
function checkforWalls()
x = 1
while true do
  if walls[x][1] == wanttox and walls[x][2] == wanttoy then return false end
  if walls[x] == nil then return true end
  x = x + 1
end
end
function move()
wanttodir = math.random(1,4)
if wanttodir == 1 then wanttox = xpos  wanttoy = ypos - 1 end
if wanttodir == 2 then wanttox = xpos  wanttoy = ypos + 1 end
if wanttodir == 3 then wanttox = xpos - 1  wanttoy = ypos end
if wanttodir == 4 then wanttox = xpos + 1  wanttoy = ypos end
if checkforWalls() then
  xpos = wanttox
  ypos = wanttoy
end
end
function load()
shell.run(read())
end
function draw()
term.setBackgroundColor(colors.green)
term.clear()
term.setCursorPos(1,1)
x = 1
while true do
  term.setBackgroundColor(colors.red)
  term.setCursorPos(walls[x][1],walls[x][2])
  term.write("X")
  x = x + 1
  if walls[x] == nil then break end
end
end
function drawBug()
term.setCursorPos(xpos,ypos)
term.setBackgroundColor(colors.blue)
term.write("#")
end
load()
while true do
term.setBackgroundColor(colors.black)
draw()
term.setBackgroundColor(colors.black)
drawBug()
term.setBackgroundColor(colors.black)
move()
sleep(0,5)
end

level program:

walls = {{1,1},{1,2},{1,3},{1,4},{1,5},{2,5},{3,5},{4,5},{5,5},{5,4},{5,3},{5,2},{5,1},{4,1},{3,1},{2,1}}
xPos = 2
yPos = 2

I started the program and entered the name of the level program. and then this error came:
[string "window"]:247: bad argument #1 to 'floor' (number expected, got nil)

Here's a short video:
Download

ok i fixed it but there is a new error at line 8 it says attempt to call nil

main program:
wanttodir = 1
wanttox = 1
wanttoy = 1
shell.run("clear")
function checkforWalls()
x = 1
while true do
  if walls[x][1] == wanttox and walls[x][2] == wanttoy then return "false" end
  if walls[x] == nil then return "true" end
  x = x + 1
end
end
function move()
wanttodir = math.random(1,4)
if wanttodir == 1 then wanttox = xpos  wanttoy = ypos - 1 end
if wanttodir == 2 then wanttox = xpos  wanttoy = ypos + 1 end
if wanttodir == 3 then wanttox = xpos - 1  wanttoy = ypos end
if wanttodir == 4 then wanttox = xpos + 1  wanttoy = ypos end
state = checkforWalls()
if state == "true" then
  xpos = wanttox
  ypos = wanttoy
end
end
function load()
shell.run(read())
end
function draw()
term.setBackgroundColor(colors.green)
term.clear()
term.setCursorPos(1,1)
x = 1
while true do
  term.setBackgroundColor(colors.red)
  term.setCursorPos(walls[x][1],walls[x][2])
  term.write("X")
  x = x + 1
  if walls[x] == nil then break end
end
end
function drawBug()
term.setCursorPos(xpos,ypos)
term.setBackgroundColor(colors.blue)
term.write("#")
end
load()
while true do
term.setBackgroundColor(colors.black)
draw()
term.setBackgroundColor(colors.black)
drawBug()
term.setBackgroundColor(colors.black)
move()
sleep(0,5)
end

level:
walls = {{1,1},{1,2},{1,3},{1,4},{1,5},{2,5},{3,5},{4,5},{5,5},{5,4},{5,3},{5,2},{5,1},{4,1},{3,1},{2,1}}
xpos = 2
ypos = 2

please help me!!
Dragon53535 #10
Posted 12 October 2014 - 01:04 PM
That line would not be giving you an attempt to call a nil value, it might give you an attempt to index a nil value. in which case change

  if walls[x][1] == wanttox and walls[x][2] == wanttoy then return "false" end
  if walls[x] == nil then return "true" end
These lines to this:

  if walls[x] == nil then return "true" end
  if walls[x][1] == wanttox and walls[x][2] == wanttoy then return "false" end
Edited on 12 October 2014 - 11:05 AM
Timo_Ho #11
Posted 13 October 2014 - 02:46 PM
That line would not be giving you an attempt to call a nil value, it might give you an attempt to index a nil value. in which case change

  if walls[x][1] == wanttox and walls[x][2] == wanttoy then return "false" end
  if walls[x] == nil then return "true" end
These lines to this:

  if walls[x] == nil then return "true" end
  if walls[x][1] == wanttox and walls[x][2] == wanttoy then return "false" end

thanks! I'll try this
Timo_Ho #12
Posted 13 October 2014 - 02:51 PM
ok the program works fine now i'll add a level editor and then upload it.
you can download all my programs at timosmods.yolasite.com !
Edited on 13 October 2014 - 12:52 PM