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

Variables....

Started by deactivated1712, 05 September 2012 - 04:00 PM
deactivated1712 #1
Posted 05 September 2012 - 06:00 PM
Hi,
I was wondering how you could assign a variable from lots of numbers, for example, "gps locate", how would you assign x y z variables to the three numbers returned from the gps locate. Thanks!

H
sjele #2
Posted 05 September 2012 - 06:19 PM
 x, y, z = gps.locate 
I belive that works idk haven't used gps locate before.

On another note: When storing var from something just do var1, var2 = (some thing)
deactivated1712 #3
Posted 05 September 2012 - 06:25 PM
When using the x, y, z = gps.locate, when I print the variables individually, it returns function: 34d04408
MysticT #4
Posted 05 September 2012 - 06:47 PM
You need to call the function, otherwise you are assigning it to the variables (only to the first one, since it's only one value):

local x, y, z = gps.locate()
print("You are at ", x, ", ", y, ", ", z)
deactivated1712 #5
Posted 05 September 2012 - 06:58 PM
When running MysticT's code, it returns error:

gps:69: attempt to compare nil with number
MysticT #6
Posted 05 September 2012 - 07:07 PM
gps.locate needs a timeout argument:

local x, y, z = gps.locate(1) -- 1 second timeout
deactivated1712 #7
Posted 05 September 2012 - 07:12 PM
No, sorted it, needed to put a number in the brackets at end of gps.locate(). Thanks :D/>/>

Oops, didn't refresh page before posting
adalonus #8
Posted 03 December 2012 - 02:50 PM
I know this topic is a bit old, but I have a similar problem, yet the solution doesn't seem to be the same. If I use the code below, It cannot find my location.



local x, y, z = gps.locate(1) -- it needs a timeout in seconds, you can change it to whatever you want
if x and y and z then -- check if it could get the position (when it times out the position is nil)
  print(x, ", ", y, ", ", z)
else
  print("Couldn't get position")
end

The odd thing is if I use:

gps locate
it works just fine and gives me my position. I'm very confused.


EDIT: in addition to this I use a GoTo program pulled from this site that is based off the GPS signal and it works just fine.
Spoiler

--To goto coordinates, use: "goto <x> <y> <z>" or "goto <x> <z>"
--To goto a saved location, use: "goto <location>"
--To add a location to the saved list, use: "goto add <location> <x> <y> <z>"
--To change the locations directory name, change all instances of "locations" to whatever you want to call it (find/replace). Just make sure the directory is in the same place as the program. I may make this easier soon.
args = {...}
if not fs.isDir("locations") then
	fs.makeDir("locations")
end
local function getDirection()
	local f = 0
	local x, y, z = gps.locate(5,false)
	y = nil
	if args[1] == "special" then
		if turtle.detect() then
			if not turtle.dig() then
				error("Tried to dig bedrock.")
			end
		end
		while not turtle.forward() do
			turtle.attack()
		end
	else
		if not turtle.forward() then
			error("I am against a wall.")
		end
	end
	local newx, newy, newz = gps.locate(5,false)
	newy = nil
	turtle.back()
	if newz > z then
		f = 0
	elseif newx < x then
		f = 1
	elseif newz < z then
		f = 2
	elseif newx > x then
		f = 3
	end
	return f
end
local function gotox(x,newx,f)
	if f == 0 then
		turtle.turnRight()
	elseif f == 2 then
		turtle.turnLeft()
	elseif f == 3 then
		turtle.turnRight()
		turtle.turnRight()
	end
	if newx < x then
		local j = x - newx
		for i=1,j do
			if args[1] == "special" then
				if turtle.detect() then
					turtle.dig()
				end
				while not turtle.forward() do
					turtle.attack()
				end
			else
				if not turtle.forward() then
					error("Path blocked.")
				end
			end
		end
	elseif newx > x then
		local j = newx - x
		turtle.turnRight()
		turtle.turnRight()
		for i=1,j do
			if args[1] == "special" then
				if turtle.detect() then
					turtle.dig()
				end
				while not turtle.forward() do
					turtle.attack()
				end
			else
				if not turtle.forward() then
					error("Path blocked.")
				end
			end
		end
		turtle.turnLeft()
		turtle.turnLeft()
	end
		if f == 0 then
		turtle.turnLeft()
	elseif f == 2 then
		turtle.turnRight()
	elseif f == 3 then
		turtle.turnLeft()
		turtle.turnLeft()
	end
end
local function gotoy(y,newy)
	if newy > y then
		local j = newy - y
		for i=1,j do
			if args[1] == "special" then
				if turtle.detectUp() then
					turtle.digUp()
				end
				while not turtle.up() do
					turtle.attackUp()
				end
			else
				if not turtle.up() then
					error("Path blocked.")
				end
			end
		end
	elseif newy < y then
		local j = y - newy
		for i=1,j do
			if args[1] == "special" then
				if turtle.detectDown() then
					turtle.digDown()
				end
				while not turtle.down() do
					turtle.attackDown()
				end
			else
				if not turtle.down() then
					error("Path blocked.")
				end
			end
		end
	end
end
local function gotoz(z,newz,f)
	if f == 1 then
		turtle.turnLeft()
	elseif f == 2 then
		turtle.turnLeft()
		turtle.turnLeft()
	elseif f == 3 then
		turtle.turnRight()
	end
	if newz > z then
		local j = newz - z
		for i=1,j do
			if args[1] == "special" then
				if turtle.detect() then
					turtle.dig()
				end
				while not turtle.forward() do
					turtle.attack()
				end
			else
				if not turtle.forward() then
					error("Path blocked.")
				end
			end
		end
	elseif newz < z then
		turtle.turnRight()
		turtle.turnRight()
		local j = z - newz
		for i=1,j do
			if args[1] == "special" then
				if turtle.detect() then
					turtle.dig()
				end
				while not turtle.forward() do
					turtle.attack()
				end
			else
				if not turtle.forward() then
					error("Path blocked.")
				end
			end
		end
		turtle.turnLeft()
		turtle.turnLeft()
	end
	if f == 1 then
		turtle.turnRight()
	elseif f == 2 then
		turtle.turnRight()
		turtle.turnRight()
	elseif f == 3 then
		turtle.turnLeft()
	end
end
local function findDistance(x,y,z,newx,newy,newz)
	local distance = 0
	local xDist = 0
	local yDist = 0
	local zDist = 0
	if x > newx then
		xDist = x - newx
	elseif x < newx then
		xDist = newx - x
	end
	if y > newy then
		yDist = y - newy
	elseif x < newx then
		yDist = newy - y
	end
	if z > newz then
		zDist = z - newz
	elseif z < newz then
		zDist = newz - z
	end
	distance = xDist + yDist + zDist
	return distance
end
if args[1] ~= "special" then
	rednet.open("right")
end
x, y, z, f, newx, newy, newz, newf = 0, 0, 0, 0, 0, 0, 0, 0
if #args == 1 then
	local location = args[1]
	if fs.exists("locations/"..location) then
		local fLocation = fs.open("locations/"..location,"r")
		newx = tonumber(fLocation.readLine())
		newy = tonumber(fLocation.readLine())
		newz = tonumber(fLocation.readLine())
		fLocation.close()
		print("Going to "..location.."...")
	else
		error("Unknown location.")
	end
elseif #args == 2 and tonumber(args[1]) and tonumber(args[2]) then
	newx, newz = tonumber(args[1]), tonumber(args[2])
	print("Going to x: "..newx..", z: "..newz.."...")
elseif #args == 3 and tonumber(args[1]) and tonumber(args[2]) and tonumber(args[3])then
	newx, newy, newz = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
	print("Going to x: "..newx..", y: "..newy..", z: "..newz.."...")
elseif #args == 4 and tonumber(args[1]) and tonumber(args[2]) and tonumber(args[3]) and tonumber(args[4]) then
	newx, newy, newz, newf = tonumber(args[1]), tonumber(args[2]), tonumber(args[3]), tonumber(args[4])
	print("Going to x: "..newx..", y: "..newy..", z: "..newz..", f: "..newf.."...")
elseif #args == 9 and args[1] == "special" and tonumber(args[2]) and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]) and tonumber(args[6]) and tonumber(args[6]) and tonumber(args[7]) and tonumber(args[8]) and tonumber(args[9]) then
	newx, newy, newz, newf = tonumber(args[2]), tonumber(args[3]), tonumber(args[4]), tonumber(args[5])
	x, y, z, f = tonumber(args[6]), tonumber(args[7]), tonumber(args[8]), tonumber(args[9])
elseif #args == 5 and args[1] == "add" and tonumber(args[3]) and tonumber(args[4]) and tonumber(args[5]) then
	local location, xname, yname, zname = args[2], args[3], args[4], args[5]
	if not fs.exists("locations/"..location) then
		local fLocation = fs.open("locations/"..location,"w")
		fLocation.writeLine(xname)
		fLocation.writeLine(yname)
		fLocation.writeLine(zname)
		fLocation.close()
		print("Location \""..location.."\" added.")
	else
		print("This location already exists. Would you like to replace it?\n\(y/n\)")
		while true do
			event, character = os.pullEvent()
			if event == "char" and character == "y" then
				local fLocation = fs.open("locations/"..location,"w")
				fLocation.writeLine(xname)
				fLocation.writeLine(yname)
				fLocation.writeLine(zname)
				fLocation.close()
				print("Location changed.")
				break
			elseif event == "char" and character == "n" then
				print("Location not changed.")
				break
			end
		end
	end
	error()
else
	print("To goto coords, use: \"goto <x> <z>\" or \"goto <x> <y> <z>\"")
	print("To goto a set location, use: \"goto <name>\"")
	print("To set a new location, use: \"goto add <name> <x> <y> <z>\"")
	error()
end
if args[1] ~= "special" then
	x, y, z = gps.locate(5,false)
end
if not x or not y or not z then
	error("Out of GPS range")
end
local distance = findDistance(x,y,z,newx,newy,newz)
local fuelLevel = turtle.getFuelLevel()
if type(fuelLevel) == "string" then
	fuelLevel = 9001e9001
end
if distance > fuelLevel then
	error("Not enough fuel to travel so far!")
end

if args[1] ~= "special" then
	turtle.up()
	turtle.up()
	turtle.up()
	f = getDirection()
end
if newy > y then
	gotoy(y,newy)
end
if newx ~= x then
	gotox(x,newx,f)
end
if newz ~= z then
	gotoz(z,newz,f)
end
if newy < y then
	gotoy(y,newy)
end
if newf ~= f then
	if f == 1 then
		turtle.turnLeft()
	elseif f == 2 then
		turtle.turnLeft()
		turtle.turnLeft()
	elseif f == 3 then
		turtle.turnRight()
	end
	if newf == 1 then
		turtle.turnRight()
	elseif newf == 2 then
		turtle.turnLeft()
		turtle.turnLeft()
	elseif newf == 3 then
		turtle.turnLeft()
	end
end
if args[1] ~= "special" then
	turtle.down()
	turtle.down()
	turtle.down()
	print("Done traveling!")
end
if args[1] ~= "special" then
	rednet.close("right")
end
args, x, y, z, f, newx, newy, newz, newf = nil, nil, nil, nil, nil, nil, nil, nil, nil
Orwell #9
Posted 03 December 2012 - 05:00 PM
Are you sure that you have a proper gps set up? I.e. a modem against your computer/turtle, 4 gps hosts, …
Orwell #10
Posted 03 December 2012 - 05:02 PM
Are you sure that you have a proper gps set up? I.e. a modem against your computer/turtle, 4 gps hosts, …

Edit:
The odd thing is if I use:

gps locate
it works just fine and gives me my position. I'm very confused.
This pretty much answers my question. I should read first and post later. <_</>

EDIT: and I should hit the Edit button, not the Quote button. Hmmf. :angry:/>
adalonus #11
Posted 03 December 2012 - 06:35 PM
Also if I could just get around using the function and use the program as a shell.run() function, I'd be happy.
Lyqyd #12
Posted 03 December 2012 - 06:38 PM
Try calling it as local x, y, z = gps.locate(1,true) to get debug info displayed.
adalonus #13
Posted 03 December 2012 - 07:15 PM
Try calling it as local x, y, z = gps.locate(1,true) to get debug info displayed.

Just tried this. Still got "Could not determine position".
adalonus #14
Posted 03 December 2012 - 07:41 PM
So I copied the code from the GoTo program and for some reason, it works now. I'm sitting here in Notepad++ hitting Ctrl+Z and Ctrl+Y over and over trying to figure out how exactly it is different, but it looks the exact same to me.

Code Before/After

--Before

 print("Man I am so lost right now!")
 local x, y, z = gps.locate(5,true)
 if x == nil then
   print("Nope, still lost :(/>")
 else
   print("I am at ",x," ",y," ",z)
 end

--After

 print("Man I am so lost right now!")
 local x, y, z = gps.locate(5,true)
 if x == nil then
   print("Nope, still lost :(/>")
 else
   print("I am at ",x," ",y," ",z)
 end

ChunLing #15
Posted 04 December 2012 - 08:42 AM
The timeout is longer. The gps program (you used in "locate" mode) uses a timeout of 2, this uses a timeout of 5, you were using a timeout of 1. Occasionally that does make a difference.