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

How to make an "editable" variable

Started by eddyman26, 10 January 2016 - 04:39 AM
eddyman26 #1
Posted 10 January 2016 - 05:39 AM
I'm new to LUA and I want to make a program where I set a quantity to drop for the command turtle.drop(x).
basically I want to type drop and then have it say Type amount of items to drop: , and before or after that Which slot to drop: .
I understand how to make that pop up and do a constant check on it, but I dont know how to set the variable on how much to make the turtle drop.
Please help!
HPWebcamAble #2
Posted 10 January 2016 - 06:17 AM
You could do it like this:


print("How many items should I drop?")

turtle.drop( tonumber( read() ) ) --# Call 'tonumber' on the value that read() returns, to convert the string to a number
No variables needed!

But if you really want to use one:

print("How many items should I drop?")

local toDrop = tonumber( read() ) --# Again, tonumber converts the string to a number

turtle.drop( toDrop )
Edited on 10 January 2016 - 05:18 AM
H4X0RZ #3
Posted 10 January 2016 - 01:08 PM
I suggest

local num
repeat
  num = read()
until tonumber(num)
num = tonumber(num)
(I might've messed something up here. If so, please correct me. I'm on my phone right now)
Dog #4
Posted 10 January 2016 - 05:11 PM
To build on H4X0RZ's example, this would be a tiny bit more efficient as it does a tonumber conversion only once…

local num --# declare and localize our 'number' variable
repeat --# start an infinite loop
  num = tonumber(read()) --# read the user's input and convert it to a number (non-numbers will be converted to nil)
until num --# stop the infinite loop when num holds a number
turtle.drop(num) --# tell the turtle to drop the number of items selected
eddyman26 #5
Posted 10 January 2016 - 07:31 PM
Wow, that was fast, thanks for the help, and I'm assuming to make it select the slot it would be roughly the same,
print("Select Slot")
turtle.select(tonumber(read()))
print("Type amount")
turtle.drop(tonumber(read()))
Lupus590 #6
Posted 10 January 2016 - 07:58 PM
your code would crash if I accidental typed in anything that is not a number, but (without user errors) it will work

if you use the repeat until loops in the style above, you can get your program to not crash if I type in "one"
Edited on 10 January 2016 - 06:59 PM
Waitdev_ #7
Posted 11 January 2016 - 12:52 AM
here's a neat thing you could do by the way: parameters.


args = {...}
slot = args[1]
drop = args[2]
turtle.select(tonumber(slot))
turtle.drop(tonumber(drop))
after taking a break from lua, i've probably messed something up. please forgive me and if you can correct me if i did ;)/>
i'm also guessing there are more efficient ways to do this
Dog #8
Posted 11 January 2016 - 02:01 AM
Wait_ brings up a good point. With Wait_'s suggestion you could call the program like so:
myProgram 3 5 (this would launch the program and tell it to select slot 3 and drop 5 items)

To ensure you don't suffer any crashes you could add a little more code like so…

local args = { ... } --# this captures all command line arguments in a local table - you can use any name: args, tArgs, etc.
if not args[2] then --# if the user didn't supply at least 2 arguments then error out
  error("You must supply the slot number and the amount to drop")
end
local slot = tonumber(args[1]) --# convert argument number 1 to a number type (or a nil if a number wasn't supplied) and store it in a local variable
local drop = tonumber(args[2]) --# convert argument number 2 to a number type (or a nil if a number wasn't supplied) and store it in a local variable
if slot and drop then --# if slot and drop are both valid numbers do the following
  turtle.select(slot)
  turtle.drop(drop)
else --# if slot and drop are not both valid numbers then error out
  error("You must supply the slot number and the amount to drop")
end

Doubtless there are better ways to do this, but hopefully this gives you an idea of what's possible.
Edited on 11 January 2016 - 01:04 AM
Lupus590 #9
Posted 11 January 2016 - 09:07 AM
If you combined this code:

local num --# declare and localize our 'number' variable
repeat --# start an infinite loop
  num = tonumber(read()) --# read the user's input and convert it to a number (non-numbers will be converted to nil)
until num --# stop the infinite loop when num holds a number
turtle.drop(num) --# tell the turtle to drop the number of items selected
with this code:

local args = { ... } --# this captures all command line arguments in a local table - you can use any name: args, tArgs, etc.
if not args[2] then --# if the user didn't supply at least 2 arguments then error out
  error("You must supply the slot number and the amount to drop")
end
local slot = tonumber(args[1]) --# convert argument number 1 to a number type (or a nil if a number wasn't supplied) and store it in a local variable
local drop = tonumber(args[2]) --# convert argument number 2 to a number type (or a nil if a number wasn't supplied) and store it in a local variable
if slot and drop then --# if slot and drop are both valid numbers do the following
  turtle.select(slot)
  turtle.drop(drop)
else --# if slot and drop are not both valid numbers then error out
  error("You must supply the slot number and the amount to drop")
end

you could get a robust program which asks for the missing command line arguments