Computer Craft Help

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Mango845

New Member
Jul 29, 2019
9
0
0
I am making a program in computercraft and it's not working for some reason. "turtle.select" and "turtle.drop" are the commands that don't seem to be working. It does the prints but skips over them. Everything else works fine and it can receive the messages from other computers. here is my code for the turtle. I am very new to computercraft so sorry for my bad code. Thanks!

rednet.open("right")
local x
local amount
senderId,message=rednet.receive()
x = message
os.sleep(1)
senderId,message=rednet.receive()
amount = message
print(x)
print(amount)
if x == "iron" then
turtle.select(1)
turtle.drop(amount)
end
if x == "cobalt" then
turtle.select(2)
turtle.drop(amount)
end
os.sleep(5)
os.reboot()
 

Eyamaz

New Member
Jul 29, 2019
2,373
0
0
Tech support is for issues with the launcher or ftb packs not running. Help with a specific mod should go in mod discussion. Requesting this thread be moved to the appropriate area.
 

gattsuru

Well-Known Member
May 25, 2013
364
103
68
As a code cleanliness thing, avoid x (and y, and z, and i) as variables outside of situations that specifically call for them. You also generally don't need to sleep between receives: an individual modem should only parse an individual message once per modem. You can also drop return values from a method directly into the variable you want, without holding an intermediary variable -- in LUA, only the order matters for return values, not type or name.

LUA is a duck-typed language : it'll let you throw any 'type' of variable into a local or global, but it'll only react to that variable correctly if it matches the expected type. So it'll print the number out quite reliably because strings are valid arguments for print, but it won't drop anything because turtle.Drop() expects a number. Sometimes it'll convert implicitly, but this is /never/ a good idea, not least of all because it risks having unsanitized data enter your fields, which can lead to unpredictable results -- most often, it'll give "Expected Number" errors, but sometimes it'll do other things instead. In a production environment, you'd probably want to serialize the data and use some sort of unique identifier for message type, as well as have safety checks for sane numbers. For a simple set-up, that's not necessary, and the number tonumber(string) function will handle things. So

Code:
rednet.open("right")
senderId,dropType=rednet.receive()
senderId,amount=rednet.receive()
print(dropType)
print(amount)
if dropType == "iron" then
   turtle.select(1)
   turtle.drop(tonumber(amount))
end
if dropType == "cobalt" then
  turtle.select(2)
  turtle.drop(tonumber(amount))
end