Jump to content




turtle.getSelectedSlot() as variable



  • You cannot reply to this topic
9 replies to this topic

#1 lare290

  • Members
  • 53 posts
  • LocationFinland

Posted 13 December 2014 - 11:55 AM

How to get variable from turtle.getSelectedSlot()?

#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 13 December 2014 - 11:57 AM

Just like you catch variables returned by any Lua function:

local slot = turtle.getSelectedSlot()


#3 lare290

  • Members
  • 53 posts
  • LocationFinland

Posted 13 December 2014 - 12:14 PM

It just says "bios:366: [string "turtlebutler"]:8: unexcepted symbol"
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 empty

Edited by lare290, 13 December 2014 - 12:15 PM.


#4 ebernerd

  • Members
  • 262 posts
  • LocationBoston, MA

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

turtle.select(slot+1)

Edited by Pseudonymous, 13 December 2014 - 12:29 PM.


#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#6 lare290

  • Members
  • 53 posts
  • LocationFinland

Posted 13 December 2014 - 12:29 PM

View PostPseudonymous, on 13 December 2014 - 12:27 PM, said:

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

turtle.select(slot+1)
I did it like that, still same error :(

Edited by lare290, 13 December 2014 - 12:46 PM.


#7 ebernerd

  • Members
  • 262 posts
  • LocationBoston, MA

Posted 13 December 2014 - 12:51 PM

View Postlare290, on 13 December 2014 - 12:29 PM, said:

View PostPseudonymous, on 13 December 2014 - 12:27 PM, said:

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

turtle.select(slot+1)
I did it like that, still same error :(

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 by Pseudonymous, 13 December 2014 - 12:52 PM.


#8 lare290

  • Members
  • 53 posts
  • LocationFinland

Posted 13 December 2014 - 01:10 PM

Why it needs local for drinks-variable? It works right without it

#9 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 13 December 2014 - 02:09 PM

View Postlare290, on 13 December 2014 - 01:10 PM, said:

Why it needs local for drinks-variable? It works right without it
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.

#10 The_Cat

  • Members
  • 119 posts

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 by The_Cat, 13 December 2014 - 03:05 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users