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

My code failed to work, please help!

Started by maydaylinus, 08 October 2012 - 02:48 AM
maydaylinus #1
Posted 08 October 2012 - 04:48 AM
Computercraft and lua experts,
Sorry for asking another question so fast, I am a good noob.
Is this code wrong? they ask for "then" at the first line and I am confused @.@

if curdirec= "4" then
	turtle.turnRight()
print("1")
elseif curdirec = "1" then
   print("1")
elseif curdirec = "2" then
   turtle.turnLeft()
   print("1")  
elseif curdirec = "3" then
   turtle.turnLeft()
   print("0.5")
   turtle.turnLeft()
   print("1")
end  
Thanks!


This my full code:
local tArgs = {...}
local dx, dy, dz
local dx = tonumber(tArgs[1])
local dy = tonumber(tArgs[2])
local dz = tonumber(tArgs[3])
local x, y, z, dif
local nx, ny, nz
local gdcx, gdcy, gdcz
local mm = 1
local curdirec
local xtm, ytm, ztm
print("Target corrdinents: "..dx)
rednet.open("right")
x, y, z = gps.locate(5)
nx = x-0	  --fixing the slight inaccuray
ny = y-1	--fixing the slight inaccuray
nz = z-2	--fixing the slight inaccuray
print("Current coordinents: "..nx)
--movement Script
if turtle.forward() then
	  turtle.forward()
	  gdcx, gdcy, gdcz = gps.locate(5)
	  print("New cooridinents: "..gdcx)
	 turtle.back()
	 turtle.back()
else
	 turtle.dig()
	 turtle.forward()
	 gdcx, gdcy, gdcz = gps.locate(5)
	 print("New cooridinents: "..gdcx)
	 turtle.back()
	 turtle.back()
end
--checking Script
if gdcx > nx then
	 curdirec = "4"
	 print("Current direction: West")
elseif gdcx < nx then
	 curdirec = "2"
	 print("Current direction: East")
	
elseif gdcz < nz then
	 curdirec = "1"
	 print("current direction: North")
elseif gdcz > nz then
	 curdirec = "3"
	 print("Current direction:South")
  
else
	 print("error detected, repeat setup")
end

if curdirec= "4" then
	turtle.turnRight()
print("1")
elseif curdirec = "1" then
   print("1")
elseif curdirec = "2" then
   turtle.turnLeft()
   print("1")  
elseif curdirec = "3" then
   turtle.turnLeft()
   print("0.5")
   turtle.turnLeft()
   print("1")
end [/font]

chiloxsan #2
Posted 08 October 2012 - 05:54 AM
Use = when setting the contents of a variable and use == when comparing the equality of two variables.
hego555 #3
Posted 08 October 2012 - 06:01 AM
When using if you have to use 2 = signs

so if(i == 5) then

end

also try some formatting!


if (curdirec == "4" )then
	    turtle.turnRight()
print("1")
elseif (curdirec == "1") then
   print("1")
elseif (curdirec == "2") then
   turtle.turnLeft()
   print("1") 
elseif (curdirec == "3") then
   turtle.turnLeft()
   print("0.5")
   turtle.turnLeft()
   print("1")
end 
sjele #4
Posted 08 October 2012 - 06:36 AM
= sets a value
== compares 2 values
maydaylinus #5
Posted 08 October 2012 - 07:11 AM
very helpfu, thats a real silly mistake…. Thank you very much! :D/>/>
Doyle3694 #6
Posted 08 October 2012 - 04:54 PM
This is very wrong section to post this in btw….
maydaylinus #7
Posted 09 October 2012 - 01:35 PM
Yeah I accidentally post it here and don't know how to change…
sjele #8
Posted 09 October 2012 - 02:01 PM
You can't move posts, only an admin or mod can. EDit topic to [move me] and report post, with the reason move me too "x". Thats how i do it atleast, or contact cloudy in IRC.
rhyleymaster #9
Posted 10 October 2012 - 12:57 AM
Maybe you should format your code? Its very messy.
rhyleymaster #10
Posted 10 October 2012 - 01:01 AM
Also, For the first line error try

[CODE]if curdirec == "4" then
I added two equal signs, As you want it to compare to values, Not set the value
For example,

curdirec = "4"
Would set the value of curdirec to 4 as

if curdirec == "4" then
would check the value cirdirec to see if its 4, Which would require a else to see what to do if its not 4.
It would also have to end in "end" to close the if function. But you already have that.