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

Creating A Turtle Elevator (Guidance Needed!)

Started by ByteZz, 17 February 2014 - 12:52 PM
ByteZz #1
Posted 17 February 2014 - 01:52 PM
Hi you all! I'm glad to be part of the community for this wonderful mod. This mod has finally started my push to force myself to learn how to code. Started coding back in '09 using Scratch (if you can call that coding) and because of that, I think I am learning Lua quite quickly.

I did all the hello worlds and whatnot and now have a new program I am making for FTB in my house. It is an elevator. Since you can travel up and down in the elevators, it would be a cool and efficient way of getting the task done without the need for making other expensive items to get the same task done. In the future, I plan to expand it when I have a deeper understanding of the language by adding auto refuel, detects when a player is about to get on the turtle to be lifted (pressure plates) and more. That comes later. It's late at night right now but I have got some draft code. It may not work and it may be buggy. That's where I hope you all can help me. If you could, please check out my code to see where I can improve it and what I can do to make the system better.


Thanks!

http://pastebin.com/gUiucegq
Lyqyd #2
Posted 17 February 2014 - 02:45 PM
Last I checked, turtles couldn't push players. Have you tested and confirmed that they can do this?
CometWolf #3
Posted 17 February 2014 - 03:52 PM
They can't push players directly, no, but he could stand ontop of it and jump as it's going up. Althoug his code wouldn't work properly in that instance, as it dosen't check the result of the turtle's movements.

The code in itself is an utter mess. There is no logical flow to it at all. And large portions of it just wouldn't work.
First and foremost, localize your variables and functions. An important thing to note however when making local declarations, is to declare them before you use them.

local function derp()
  print(herp)
end
local herp = "derpderp"
derp() -- this will error

local herp = "derpderp"
local function derp()
  print(herp)
end
derp() -- this will print "derpderp"
In the top code, there is no local herp variable when the function is defined, thus it will instead point to the global herp. Which ofcourse isn't defined, as we only define a local one, but that's after the function declaration in this case. If you look closely, there's a couple of cases of the same issue in your code.

Secondly, use function arguments to define their parameters, not variables

elevator = function(elevateCount) --this is called an argument, which is now passed onto the function when it's called
  print("Elevating "..elevateCount.." blocks")
  for elevateLoop = 1, elevateCount do
  turtle.up()

  end
  menuScreen()
end
elevator(20) -- this would make the functions local elevateCount be 20, but only for this call.
This makes your code more readable and adaptable for later use.

And your menu function… why is it split in 2?

menuScreen = function()
  print("ByteElevator vA0.1")
  print("Current Elevate Count: "..elevateCount")
  print("Change Elevate Count: 1")
  print("Help Me!: 2")
  print("Elevate Now! : 3")
  print("Choose answer using '1', '2' or '3': ")
  menuSelect = read() -- This should be local, since this function is the only place this variable is used anyway.
  tonumber(menuSelect) -- this line dosen't do anything, because you don't store the resulting number.
  term.clear()
  term.setCursorPos(1,1)
  if menuSelect == 1 then --because of that, the number is still a string, and all these if's will fail.
  print("The current elevate count is "..elevateCount")
  print("Would you like to change the elevate count? (y/n)")
  helpMenuSelect = read()
    if helpMenuSelect == "y" then
	  print("Input new elevate count in numbers:")
	  elevateCount = read()
	  tonumber(elevateCount)
	  term.clear()
	  term.setCursorPos(1,1)
	  menuScreen()
    elseif menuSelect == 2 then
	  helpScreen()
    else
	  elevator()
    --missing an end here
end

Next time you ask for help here, please try running your own program and confirm that it actually works first. If it dosen't, give us the error code.
ByteZz #4
Posted 18 February 2014 - 05:21 AM
Thanks for the help. I will see if it works! If not, I'll rewrite keeping what you've said in mind.