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

MiningProgramTurtle - Tips and advice please

Started by maxo10, 04 November 2015 - 10:29 PM
maxo10 #1
Posted 04 November 2015 - 11:29 PM
After some hard days as newbie, i finally dare to post this around here!
It's not a very clean/neat script but that will come the more i work on this script :D/>

http://pastebin.com/Tqzn3B8M

I'm updating this script multiple times a day, and i would like to have you guys to have a look and tell me your opinion/advice/tips to me!

Thanks for reading and your time!

ps.
Am trying to make it monkey-proof :)/>
KingofGamesYami #2
Posted 05 November 2015 - 12:07 AM
In your start function, I'd consider user os.pullEvent directly, rather than read.

Eg:


while true do --#loop forever
  local e, char = os.pullEvent( "char" ) --#wait for the user to press an alphanumeric / punctuation key
  if char == "y" then --#if they pressed y
    chestCheck()
    break --#exit the loop here
  elseif char == "n" then --#if they pressed n
    mining()
    break --#exit the loop here
  end
  --#if they didn't press y or n, the program will simply wait for another key to be pressed
end

Edit: Also, for getting numbers, there's a really nice repeat loop.


local n
repeat
  print( "I need a number: " )
  n = tonumber( read() )
until n

tonumber returns either a number (if possible) or nil (when not possible to get a number). The repeat loop waits until n is not nil. The print statement will keep bugging the user until they enter a valid number

Edit #2: You should really localize all your functions - it's good practice. It won't affect you much here, since the delay is negligible, but optimizing things like this is a good habit.
Edited on 04 November 2015 - 11:11 PM
maxo10 #3
Posted 05 November 2015 - 12:20 AM
Wow thanks, and really nice explanation! Both really nice for making it more simple, and monkey-proof.
But what do you mean with your last edit#2?
KingofGamesYami #4
Posted 05 November 2015 - 01:33 AM
Instead of doing

function func()

end

You should use

local function func()

end

Whenever possible. The same is true for every other type of variables, but I didn't comment since most of your variables are local (except functions).
maxo10 #5
Posted 05 November 2015 - 08:06 AM
Aaaah, yeah will do that, thanks for the tip!

Edit:
For some reason the turtle gives back errors now, with the local func opdate.

mining:57: attempt to call nil
Anyone seeing what i wrote wrong there?

Edit#2:
For now, i remove it again… no other stuff is run on the turtles so no conflicts.
Edited on 05 November 2015 - 12:11 PM
KingofGamesYami #6
Posted 05 November 2015 - 03:30 PM
chestcheck() is defined after it's called in start()
maxo10 #7
Posted 05 November 2015 - 08:40 PM
chestcheck() is defined after it's called in start()
Oh thats the reason… well lets clean up my code into a more correct order, thanks for pointing that out.
KingofGamesYami #8
Posted 05 November 2015 - 11:47 PM
You're welcome. If you have a point where you can't define a function before it is used, you can pre-declare it local, like this:


local lastcheck

--#stuff

function lastcheck()

end