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

Using ~= with a Runtime Argument Not behaving

Started by lysit, 23 May 2016 - 02:01 PM
lysit #1
Posted 23 May 2016 - 04:01 PM
Full program text, offending/misbehaving/misused code below

function refreshItems()
	for ai=1,16 do
		if turtle.getItemCount(ai)~=0 then
			turtle.select(ai)
			break
		end
	end
end

function placeBlock()
	refreshItems()
	turtle.placeDown()
end

function placeLine(lineNum)
	for bi=1,lineNum do
		if bi~=1 then
			turtle.forward()
		end
		placeBlock()
	end
end

function moveNextRowRight()
	turtle.turnRight()
	turtle.forward()
	turtle.turnRight()
end

function moveNextRowLeft()
	turtle.turnLeft()
	turtle.forward()
	turtle.turnLeft()
end

function buildPlatform(x,y)
	for ci=1,y do
		placeLine(x)

		if ci ~= y then
			print ("ci: "..tostring(ci))
			print ("y: "..tostring(y))
			if ci % 2 == 1 then
				moveNextRowRight()
			else
				moveNextRowLeft()
			end
		end

	end
end

local runtimeArgs = { ... }

intX = runtimeArgs[1]
intY = runtimeArgs[2]

turtle.forward()
buildPlatform(intX,intY)

The issue Im having is within function buildPlatform(x,y), the if statement

		if ci ~= y then
			print ("ci: "..tostring(ci))
			print ("y: "..tostring(y))
			if ci % 2 == 1 then
				moveNextRowRight()
			else
				moveNextRowLeft()
			end
		end

Always behaves as ci and y are not equal, but will print them out as being equal, see below.


I suspect Im misunderstanding about LUA here but Im can't find it on my own.
Lyqyd #2
Posted 23 May 2016 - 06:25 PM
Command line arguments are always strings. The string "3" and the number 3 will never be equivalent. You'd probably want to pass your command line arguments to tonumber() and use the result, as that will convert a numeric string to a number.