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

[LUA] [Error]

Started by Kaatoz, 06 December 2012 - 05:04 PM
Kaatoz #1
Posted 06 December 2012 - 06:04 PM
First off, I have read and watched numerous tutorials, I feel I have a common understanding of how this all is working:

Ok so I'm been racking my brain at just the simplest thing i feel…I have tried to establish a function and then call upon that
function in a while loop untill I confirm that the dimensions are correct in which case i'll then have a repeat loop building the bridge but i'm not even to that point yet. Can someone dissect my code and tell me what i'm doing wrong?

Here's my code:

http://pastebin.com/W44qVYyp

THANKS!!!!

- Kaatoz

sorry i almost forgot here's the error recieved: bios:338: [string "bridge"]:25: '=' expected
Edited on 06 December 2012 - 05:06 PM
MemoryLeak21 #2
Posted 06 December 2012 - 06:12 PM
First of all, on line 27, I predict an error. You don't use 'then' before 'do'.

Second of all, your 'answer' value doesn't change. Your function dim() has to return answer, because it only modifies it in the same function and not in the public space. Here's the new code:


--VARIABLES

local length = ""
local width = ""
local answer = ""
local correct = "y"
local x = 1

-- Functions
function dim()
term.write("How long is bridge? :")
   length = read()
term.write("How wide is bridge? :")
   width = read()
  
print("These are the dimensions of the Bridge:")
print("Length = "..length)
print("Width = "..width)
print("Shall I proceed? : y/n")
answer = read()
return length, width, answer
End

--Main Program

length, width, answer = dim()

while answer ~=correct do
   if answer == correct then
		 print ("Awesome!! Let's build!!")
    else
		 print ("Well, then I'm not sure what to do let's start over.")
		 dim()
    end
end

end
Kaatoz #3
Posted 06 December 2012 - 06:33 PM
i'm not understanding the returns like i said supremely noob at this and am trying to pick it up the coding totally works, thank you, btw; i'm just trying to understand how lol
MemoryLeak21 #4
Posted 06 December 2012 - 07:14 PM
It's difficult to explain this, but the function should actually loop until the user types 'y'. So something like this would work better:



--Variables

local length
local width
local answer

--Functions

function dim()
  repeat
  term.clear()
  term.setCursorPos(1,1)
write("Length: ")
  length = read()
  write(" Width: ")
  width = read()
  write("Proceed? (y/n): ")
  answer = read()
  until answer == 'y'
  return length, width
end

function build(length, width)
  --code where you tell your turtle to build goes here
end

--Main

length, width = dim()

build(length, width)

Remember that I don't have ComputerCraft open right now, so this might not work. You get the basic idea, though.