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

Storage automation for a lua newb

Started by Jsb, 09 December 2013 - 07:19 PM
Jsb #1
Posted 09 December 2013 - 08:19 PM
I was playing FTB Mindcrack Pack (a minecraft mod pack with Computer Craft) and thought to myself, I really want some storage automation. I have minimal knowlege of lua and turtles, but i do know thier basic functions/Api (and the rednet API) and have looked at basic lua. I am a programmer; I have a lot of experience in c++, however, am hopeless every time i try lua and CC :)/> . However, I would like to know what the best way to tackle auto retrieving and pushing things to a storage system would be.

The way I would do it in c++ (i only understand c++, lua looks like gobbledeegook) :

// - double slash = comment in c++/java for lua-only people :)/>/>/>
// computer 1
string itemType = " "
string amount = " "
std::cout << "type Item type"; // standard c++ output
std::cin >> itemType; //standard c++ input
rednet.open ("right");
rednet.broadcast (itemType);
id, message = rednet.recieve();

if (message == "has item")
{
	 std::cout << "Amount?";
	 std::cin >> amount;
	 rednet.broadcast (amount);
	 id, message = rednet.recieve();
	 if (message == "has amount")
	 {
		   std::cout << "Everything clear. Will recieve items.";
	 }
	 if (message != "has item") // != is the not operator in c++ / any language w/ boolean operators
	 {
		  std::cout << "Not enough Items, sorry.";
	 }
}
if (message != "has item")
{
	 std::cout << "No item of that type in storage, sorry."
}

I have been using c++ for so long, I don't even begin to understand lua. Everything I try to write is semicoloned and with {} brackets :)/> If someone will give me pointers as to how i could convert this to lua, i would be very happy. Ty. :D/>
Edited on 09 December 2013 - 08:15 PM
Bomb Bloke #2
Posted 09 December 2013 - 10:26 PM
-- - double dash = A comment in Lua.
-- computer 1
local itemType,amount   -- Data types can be changed at will and so aren't specified.
                        -- Variables are also globalised unless you specify otherwise, hence good practise is to localise them.
print("Type in the desired item type:") -- Standard Lua output.
itemType = read()       -- ComputerCraft's general "get text from user" function.
rednet.open("right");   -- Note that semi-colons may be used, but are optional.
rednet.broadcast(itemType)
local id, message = rednet.receive()  -- "Receive" is spelt with an "ei".

if message == "has item" then  -- Brackets are optional (depending on the complexity of your conditional), braces aren't used, "then" is mandatory.
         print("Amount?")
         amount = tonumber(read())  -- "read()" returns strings. "tonumber()" converts type where possible.
         rednet.broadcast (amount)
         id, message = rednet.receive()
         if message == "has amount" then
                   print("Everything clear. Will recieve items.")
         elseif message ~= "has item" then    -- Use "~=" instead of "!=".
                  print("Not enough Items, sorry.")
         end   -- For all intents and purposes, "end" is the same as "}".

         -- Presumably the other system can return "has amount", "has item", or something else...?
else  -- I'll assume you're familiar with "else" and "elseif".
         print("No item of that type in storage, sorry.")
end

rednet.close("right")
theoriginalbit #3
Posted 09 December 2013 - 10:33 PM

if (message == "has item")
Unrelated to the problem at hand but, technically in C++ shouldn't that be

if (strcmp(message, "has item"))
or

if (message.compare("has item"))

Everything I try to write is semicoloned
You can end lines in Lua with a semi-colon, it is valid syntax :)/>
Jsb #4
Posted 10 December 2013 - 05:17 PM

if (message == "has item")
Unrelated to the problem at hand but, technically in C++ shouldn't that be

if (strcmp(message, "has item"))
or

if (message.compare("has item"))

Everything I try to write is semicoloned
You can end lines in Lua with a semi-colon, it is valid syntax :)/>

Yay semicolons! :P/>

Thank you :)/> I now know some basic lua through your translation.

Thanks for the help, but it looks like i need a little more. For some reason it doesen't like my "end" statements even though i think i put enough:
function allTheThings ()
		if numbItems > 64 then
	  turtle.suckUp()
	  numbItems = numbItems - 64
	  end
	elseif numbItems < 64 then
	  turtle.suckUp()
	  itemCount = turtle.getItemCount(1)
	  amountBack = 64 - itemCount
	  turtle.dropUp(amountBack)
	  end
	turtle.turnLeft()
	turtle.turnLeft()
	turtle.drop()
	turtle.turnRight()
	turtle.turnRight()
end

rednet.open("right")
id, message = rednet.recieve()
if id == "pc1" and message == "cobble" then
  id, message = rednet.recieve()
  if id == "pc1" then
  numbItems = message
  while numbItems > 0 do
		allTheThings()
end
It thinks that there is no "end" to close the first function, I put one in there. Is there some special requirement rather than just putting it at the end?
Csstform #5
Posted 10 December 2013 - 10:59 PM
I doubt this is correct, but whenever I get that error, if I add 3 or 4 more 'end' statements, it works about 50% of the time. I'm interested in a proper answer to this as well.
theoriginalbit #6
Posted 10 December 2013 - 11:26 PM
an if statement doesn't need an end if there's an elseif.


if [condition] then
 -- statement
end


if [condition] then
 -- statement
else
 -- statement
end


if [condition] then
 -- statement
elseif [condition] then
 -- statement
end
AgentE382 #7
Posted 13 December 2013 - 12:03 AM
Remember that in Lua,
else if
is different from
elseif
That was one of the things that got me when transitioning to Lua.

This is what it needs to look like if you leave that whitespace between else and if:
if [condition] then
  -- statement
else
  if [condition] then
    -- statement
  end
end

If you leave any space, it treats the second if as the opening of another branching statement, instead of the continuation of the previous branching statement.
Edited on 12 December 2013 - 11:06 PM