Jump to content


okok99haha's Content

There have been 13 items by okok99haha (Search limited from 10-February 22)


By content type

See this member's

Sort by                Order  

#186688 Giving error when calling function

Posted by okok99haha on 03 July 2014 - 08:52 AM in Ask a Pro

View PostTheOddByte, on 02 July 2014 - 08:28 PM, said:

It seems you're overriding the function newID so that it becomes a number
function newID()
	...
	newID = lastID + 1
	...
end
so you can fix this error by giving the variable in your function another name
function newID()
	...
	new_id = lastID + 1
	...
end
You should look into using locals aswell since it's bad to have globals unless you plan on accessing these variables/functions from other files
http://www.lua.org/pil/4.2.html

Not sure but did some changes and works now
Thanks! :)



#186685 Giving error when calling function

Posted by okok99haha on 03 July 2014 - 08:24 AM in Ask a Pro

View PostTheOddByte, on 02 July 2014 - 08:28 PM, said:

It seems you're overriding the function newID so that it becomes a number
function newID()
	...
	newID = lastID + 1
	...
end
so you can fix this error by giving the variable in your function another name
function newID()
	...
	new_id = lastID + 1
	...
end
You should look into using locals aswell since it's bad to have globals unless you plan on accessing these variables/functions from other files
http://www.lua.org/pil/4.2.html

Changed function name to new_ID but does not work.
attept to call nil. Any solution?



#186550 Giving error when calling function

Posted by okok99haha on 02 July 2014 - 03:15 PM in Ask a Pro

Hello,
Continuing the same problem from the last post, it gives me error when I am calling the function newID.
I am not sure what change I made after last time running the function, and for me it does not seem wrong.
the error is attempt to call number on line 96, which is in the function of mainMenu, newID().
http://pastebin.com/buswPGHh, if you think it is easier to see it on the pastebin.
Thanks in advance,

local diskDriveSide = "left"

function RandomString()
  length = 16
  if length < 1 then return nil end
  local array = {}
  for i=1, length do
	array[i] = string.char(math.random(32,126))
end
  return table.concat(array)
end

function compare()
  local disk_f = io.open("disk/pw.txt", "r")
  if disk_f == nil then
	term.setCursorPos(1,4)
	print("File or code does not exist.")
	sleep(3)
   else
  local disk_pw = disk_f:read("*l")
  disk_f:close()
  local lastID_f = io.open("lastid.txt", "r")
  local lastID = lastID_f:read("*l")
  lastID_f:close()
  while true do
  term.setCursorPos(1,4)
  print("Checking..")
  sleep(0.3)
	for fileNumber = 1, lastID do
	  print("Checking : "..fileNumber..".txt")
	  local check_f = io.open(fileNumber..".txt","r")
	  local line = check_f:read("*l")
	  sleep(0.1)
	if line == disk_pw then
	  local nick = check_f:read("*l")
	  print("Username: " ..nick)
	  print("Press any key to exit")
	  os.pullEvent()
	  return
	end
	end
	print("Invalid, press any key to exit.")
	os.pullEvent()
	return
end
end

function newID()
  local genCode = RandomString()
  if disk.isPresent(diskDriveSide) == false then
	term.setCursorPos(1,4)
	print("Insert Disk!")
	sleep(3)
   else
	term.setCursorPos(1,4)
	term.write("Please type in the player name : ")
	local playerName = read()
	disk.setLabel(diskDriveSide, "SPCard - " ..playerName)
	local disk_pw = io.open("disk/pw.txt", "w")
	disk_pw:write(genCode)
	disk_pw:close()
	local root_lastID = io.open("lastid.txt","r")
	local lastID = root_lastID:read("*l")
	root_lastID:close()
	root_lastID = io.open("lastid.txt","w")
	newID = lastID+1
	root_lastID:write(newID)
	root_lastID:close()
	local root_newID = io.open(newID..".txt","w")
	root_newID:write(genCode)
	root_newID:write("\n")
	root_newID:write(playerName)
	root_newID:close()
	term.setCursorPos(1,6)
	print("Generated!")
	sleep(2)
  end
  end
end

function mainMenu()
  term.clear()
  term.setCursorPos(1,1)
  print("1. Verify")
  print("2. Make New ID Card")
  local event, key = os.pullEvent("char")
  if key == "1" then
	 if disk.isPresent(diskDriveSide) == false then
		term.setCursorPos(1,4)
		print("Insert Disk!")
		sleep(3)
	  else
	   compare()
	 end
   elseif key == "2" then
	 newID()
   else
	 sleep(0.1)
end
end

while true do
  mainMenu()
end



#186528 Need help on opening txt file

Posted by okok99haha on 02 July 2014 - 08:12 AM in Ask a Pro

Thank you everyone, solved the problem! :)



#186490 Need help on opening txt file

Posted by okok99haha on 02 July 2014 - 12:12 AM in Ask a Pro

View Postsjonky, on 01 July 2014 - 02:30 PM, said:

If i am counting correctly line 15 is the "disk_pw = disk_f...." and since disk_f is your file handle im guessing it fails beacause it cant find the file, or something. You should make it check first if the file is actually there. And check if the file handle creation was successful

Thanks, I will try.

View PostLink149, on 01 July 2014 - 02:45 PM, said:

As sjonky said, it's probably because the program failed to open the file.
An easy way to check if a file has been openned correctly is to make sure it's handle isn't nil:

function read(sFilename)
  local hFile = io.open(sFilename, "r")

  #If file has been created then return it's content, otherwise
  #return nil (implicit)
  if hFile then
	local sBuffer = hFile:read("*a")
	hFile:close()
  
	return sBuffer
  end
end

Wonder why it fails to open the file.
I will check when I get back home.

View PostBomb Bloke, on 01 July 2014 - 02:46 PM, said:

What's that "write" call doing there on line 15...?

Yeah was testing something and it was supposed to be line.
Sorry, mistake when uploading the code. Thanks.

View Postelectrodude512, on 01 July 2014 - 04:40 PM, said:

Line 15 says "disk_fi:close()" when you meant "disk_f:close()". Get rid of the i in disk_fi and it should work.

Some typos to fix :(
Thanks for noticing that



#186373 Need help on opening txt file

Posted by okok99haha on 01 July 2014 - 01:16 PM in Ask a Pro

So, I was making a program that included editing .txt file.
It was doing well on opening the file, editing, and saving and etc.
However when it gives me the error on the line 15 of "attempt to call nil"
I have tried all of the things but does not work.
What is wrong with it? I just did the same thing that I did to other parts of the code that included opening the file.

local diskDriveSide = "left"

function RandomString()
  length = 16
  if length < 1 then return nil end
  local array = {}
  for i=1, length do
    array[i] = string.char(math.random(32,126))
end
  return table.concat(array)
end

function compare()
  local disk_f = io.open("disk/pw.txt", "r")
  local disk_pw = disk_f:write("*l")
  disk_fi:close()
  local lastID_f = io.open("lastid.txt", "r")
  local lastID = lastID_f:read("*l")
  lastID_f:close()
  while false do
    for fileNumber = 1, lastID do
	  local check_f = io.open(fileNumber.. ".txt","r")
	  local line = check_f:read("*l")
    if line == disk_pw then
	  local nick = check_f:read("*l")
	  print(nick)
	  return true
    else
	  return false
    end
  end
end
end

function newID()
  local genCode = RandomString()
  if disk.isPresent(diskDriveSide) == false then
    print("Insert Disk!")
    sleep(3)
   else
    term.setCursorPos(1,4)
    term.write("Please type in the player name : ")
    local playerName = read()
    disk.setLabel(diskDriveSide, "IDCard - " ..playerName)
    local disk_pw = io.open("disk/pw.txt", "w")
    disk_pw:write(genCode)
    disk_pw:close()
    local root_lastID = io.open("lastid.txt","r")
    local lastID = root_lastID:read("*l")
    root_lastID:close()
    root_lastID = io.open("lastid.txt","w")
    newID = lastID+1
    root_lastID:write(newID)
    root_lastID:close()
    local root_newID = io.open(newID..".txt","w")
    root_newID:write(genCode)
    root_newID:write("\n")
    root_newID:write(playerName)
    root_newID:close()
  end
    term.setCursorPos(1,6)
    print("Generated!")
    sleep(2)
end

function mainMenu()
  term.clear()
  term.setCursorPos(1,1)
  print("1. Verify")
  print("2. Make New ID Card")
  local event, key = os.pullEvent("char")
  if key == "1" then
    compare()
   elseif key == "2" then
	 newID()
   else
	 sleep(0.1)
end
end

while true do
  mainMenu()
end



#141776 Turtle Placing Stairs

Posted by okok99haha on 26 August 2013 - 07:35 AM in Ask a Pro

I was making a program that makes turtle to place stairs.
However, turtle would only place the stairs facing the certain direction.
Is there a way to make turtle to place stairs on the way its facing?



#140571 Turtle Ignore the last move in the loop

Posted by okok99haha on 18 August 2013 - 02:14 AM in Ask a Pro

View Postreububble, on 11 August 2013 - 06:54 AM, said:

Yes just change the if statement to a while statement.
while turtle.getFuelLevel() < 10 do

Thanks!



#139353 Turtle Ignore the last move in the loop

Posted by okok99haha on 11 August 2013 - 01:43 AM in Ask a Pro

View Postreububble, on 10 August 2013 - 05:23 AM, said:

There exists a function called turtle.getFuelLevel Obviously, it returns the turtle's fuel level... You could create a part of your turtle's routine to check this function. A suitable snippet of code could be
 while true do -- Lots of turtley things -- including digging -- and moving -- then running out of fuel if turtle.getFuelLevel() < 10 then turtle.select(whatever slot the coal should be in) turtle.refuel(1) end end 
Do be careful though, I used an if statement here, which means that it won't repeat the refueling process if it didn't work the first time. That means that if there is no fuel in the slot, the turtle will continue to roll around in a paraplegic heap on the ground. No offense to any paraplegic persons, it's a serious condition. That should be the most of it you need to know.

Well, I want it to repeat the refuelling process if it fails.
Is there any way to do that?



#139167 Turtle Ignore the last move in the loop

Posted by okok99haha on 10 August 2013 - 12:29 AM in Ask a Pro

View Postreububble, on 09 August 2013 - 11:28 PM, said:

function line()
  for aa = 2, tArgs[2] do
	down()
	up()
	forward()
  end
  down()
  up()
end

Should do the trick but only if args[2] is never less than 2. Your problem could be because when you use arguments they are entered in string form, so you might need to either change args[2] to a number by using
args[2] = tonumber(args[2])
or by using tonumber(args[2]) in the line you check it.

Thanks, I used tArgs[2] = tonumber(tArgs[2]) (not sure if I did it right) and modified other parts and it works!
Another question, is there a way to make turtle to stop and until it gets fuel, and then continue the move?
I tried, but I couldn't find a way to do that.
Thanks!



#139143 Turtle Ignore the last move in the loop

Posted by okok99haha on 09 August 2013 - 09:23 PM in Ask a Pro

View PostLyqyd, on 08 August 2013 - 07:24 PM, said:

Split into new topic.

You could check if the current iteration is less than the maximum and only call the function if it is. A simple if statement around the function call in question will solve this nicely.

Could you please write me an example, I tried but failed.
This is what I tried:


function line()
for aa = 1, tArgs[2] do
down()
up()
if aa < tArgs[2] then
forward()
end
end
end



I get

cleararea:47: attempt to compare string with number expected, got string

where line 47 is if aa < tArgs[2] then
and I get this when it comes to the forward() function



#138771 Turtle Ignore the last move in the loop

Posted by okok99haha on 07 August 2013 - 08:47 PM in Ask a Pro

Title: Turtle Ignore the last move in the loop

So, I was making a program that would mine the certain area and I have problem that because of the loop, turtle goes one block further than it should go.
This is the code:
local tArgs = {...}

term.clear()
term.setCursorPos(1,1)
print("Digging:")
print("  "..tArgs[1].." blocks Down")
print("  "..tArgs[2].." blocks Forward")
print("  "..tArgs[3].." blocks Side")
term.setCursorPos(18, 9)
textutils.slowPrint("made by okok99haha")

function down()
for i = 1, tArgs[1] do
  while not turtle.down()
	do turtle.digDown()
end
end
end

function up()
for i = 1, tArgs[1] do
  while not turtle.up()
	do turtle.digUp()
end
end
end

function forward()
  while not turtle.forward()
	do turtle.dig()
end
end

function line()
  for aa = 1, tArgs[2] do
	down()
	up()
	forward()
end
end

function turn()
turtle.turnRight()
turtle.turnRight()
end

function back()
for ab = 1, tArgs[2] do
turtle.forward()
end
end

function moveLine()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
end

for i = 1, tArgs[3] do
  line()
  turn()
  back()
  moveLine()
end

term.setCursorPos(1,11)
print("Done!")
print("Print Any Key to Exit")
os.pullEvent("key")
term.clear()
term.setCursorPos(1,1)

I know where the problem is, it is the line function, but I couldn't find a way to solve it.
Simply, I want it to make ignore the last function, forward() on the last loop of the line function.
Please help!



#138432 Turtle wait for fuel

Posted by okok99haha on 06 August 2013 - 12:16 AM in Ask a Pro

Hello, I was trying to make a program for turtle to wait for fuel when the fuel is low, but the program that I made checked the fuel, tried to refuel (even though the slot didn't have any fuel) and continue the program. Is there any way to make turtle to wait for the fuel and resume the program when he gets fuel?

Also, I have problem with my program which turtle digs the area I asked. The problem is that when I ask turtle to dig 2 deep and 2x2 square, turtle would dig the first 2 blocks properly, but will go to the next and break the block in front of it, but won't dig the block down and come back. I don't want turtle to break the next block, but I couldn't find a way to fix it. Could anyone please help

The script for that part is:
function line()
  for aa = 1, tArgs[2] do
	down()
	up()
	forward()
end
end

function down()
for i = 1, tArgs[1] do
  while not turtle.down()
	do turtle.digDown()
end
end
end

function up()
for i = 1, tArgs[1] do
  while not turtle.up()
	do turtle.digUp()
end
end
end

function forward()
  while not turtle.forward()
	do turtle.dig()
end
end