This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
turtle.getSelectedSlot() as variable
Started by lare290, 13 December 2014 - 10:55 AMPosted 13 December 2014 - 11:55 AM
How to get variable from turtle.getSelectedSlot()?
Posted 13 December 2014 - 11:57 AM
Just like you catch variables returned by any Lua function:
local slot = turtle.getSelectedSlot()
Posted 13 December 2014 - 12:14 PM
It just says "bios:366: [string "turtlebutler"]:8: unexcepted symbol"
What am I doing wrong now?
What am I doing wrong now?
local slot = turtle.getSelectedSlot()
write ("Do sir want some drinks? ")
variable = read()
if variable == ("yes") then
if turtle.getItemCount(turtle.getSelectedSlot())>0 then
turtle.drop()
else
turtle.select(local slot+1) then
turtle.drop()
end
elseif variable == ("no") then
print ("Very well")
end
I just try to get it use next inventory slot when current one is emptyEdited on 13 December 2014 - 11:15 AM
Posted 13 December 2014 - 12:27 PM
When you declare a local variable, you use the word local.
When you're using said variable, do not attach local to it. So you're code for that would be
When you're using said variable, do not attach local to it. So you're code for that would be
turtle.select(slot+1)
Edited on 13 December 2014 - 11:29 AM
Posted 13 December 2014 - 12:28 PM
merged threads. it is best to keep code related to the same program in the same thread for better and more accurate assistance.
Posted 13 December 2014 - 12:29 PM
I did it like that, still same error :(/>When you declare a local variable, you use the word local.
When you're using said variable, do not attach local to it. So you're code for that would beturtle.select(slot+1)
Edited on 13 December 2014 - 11:46 AM
Posted 13 December 2014 - 12:51 PM
I did it like that, still same error :(/>When you declare a local variable, you use the word local.
When you're using said variable, do not attach local to it. So you're code for that would beturtle.select(slot+1)
I just realized, you have a then attached on to the end of that line. You can't have that there.
I'll rewrite your code:
local slot = turtle.getSelectedSlot()
write("Does sir want some drink?")
local variable = read() --#I suggest using a different variable name
if variable == "Yes" then
if turtle.getItemCount(turtle.getSelectedSlot))>0 then
turtle.drop()
else
turtle.select(slot + 1)
turtle.drop()
end
elseif variable == "no" then
print("Very well.")
end
Edited on 13 December 2014 - 11:52 AM
Posted 13 December 2014 - 01:10 PM
Why it needs local for drinks-variable? It works right without it
Posted 13 December 2014 - 02:09 PM
Generally, you always want to define your variables locally. They are accessed faster and other programs cannot mess with them. Your program will work with global variables, but it is bad practice to do so and will probably give you problems when you get into more advanced programs.Why it needs local for drinks-variable? It works right without it
Posted 13 December 2014 - 03:04 PM
Hi, i have seen your code and i rewrote it in a way that will check each slot, and if it doesnt find any drinks it tells you. Reads the comments i put with it.
function checkForDrinks() --#Function to check if any of the slots have any items in it
for i = 1,16 do --#For Loop
turtle.select(i)
if i == 16 then --#When then for loop reaches 16 it will go in to this if statement
if turtle.getItemCount() == 0 then --#Then it will see if the item count is equal to 0 if so it will break out of loop
print("No drinks in Inventory.")
end
end
if turtle.getItemCount(turtle.getSelectedSlot())>0 then -- You wrote this
turtle.drop()
print("Here you go sir.")
break --#Break means that it will break out of the loop as soon as it has enterd this if statement
end
end
end
write ("Do sir want some drinks? ")
variable = read()
if variable == ("yes") then
checkForDrinks() --#Put the function here so when the variable is read as yes it will run it
elseif variable == ("no") then
print ("Very well")
end
Edited on 13 December 2014 - 02:05 PM