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

Cant edit a variable from API

Started by ichim, 09 January 2013 - 07:00 AM
ichim #1
Posted 09 January 2013 - 08:00 AM
Sorry to be here again but i just cant get it working.

I have a Variable and i want to edit it from a API, but get the Info:

"turtleAPI:117: attempt to perform arithmetic __add on nil and number"

The operation im diong is PosZ = PosZ + 1 (Line 117 in API)

If i do print(tostring(PosZ)) i get the correct number shown.

Here are the Code parts:

....
Ausstattung = "Axt"
PosX, PosY, PosZ = 0, 0, 0
--Blickrichtung = nil
.....
.....
-- Erste Bewegungen
print(tonumber(PosZ))
turtleAPI.up()
sleep(5)
........

And in the API:

......
function up()
	if turtle.up() then
		PosZ = PosZ + 1
		return true
	else
		return false
	end
end
.......


Full Code Turtle (Warning bad written and not sorted by now)
SpoilerAusstattung = "Axt"
PosX, PosY, PosZ = 0, 0, 0
–Blickrichtung = nil
– Turtle Testing
– API laden
os.loadAPI("turtleAPI")
– Rednet Modem einschalten
rednet.open("right")
– Debuging ID anzeigen, kleine Tests
shell.run("id")
–turtleAPI.Test()
– Variablen erstellen / Beschreiben
EigendeID = turtleAPI.getID()
print("Habe Eigende ID "..EigendeID.." vermerkt")
– Kurze Pause um rednet Flutung zu vermeiden
print("Gehe schlafen")
print("Habe "..tostring(turtleAPI.waitRandom()).." Sekunden geschlafen")
print("Frage Server ID an")
ServerID = turtleAPI.getServerID()
print("Habe ServerID: "..ServerID)
print("Frage Position an")
PosX, PosY, PosZ = turtleAPI.getPosition()
–PosX, PosY, PosZ = gps.locate(2, true)
print("Habe Position X: "..tostring(PosX)..", Y: "..tostring(PosY)..", Z: "..tostring(PosZ).." vermerkt")
Himmelsrichtung = turtleAPI.getRichtung(PosX, PosY)
print("Schaue in Richtung "..tostring(Himmelsrichtung))
– Erste Bewegungen
print(tonumber(PosZ))
turtleAPI.up()
sleep(5)
turtleAPI.down()

– Benötigte Daten gesammelt. Anmeldung an Server
–[[ Startverzögung Random
print("Bin kurz schlafen")
Num = math.random(1,10)
sleep(Num / 10)
print("Habe geschlafen "..(Num / 10).." Sekunden")
]]–


Full Code API (Not as bad as the Turtle but still bad)
Spoiler– API für die Turtles

– Test API (drehen des Turtle)
function Test()
i = 0
for i=1, 10 do
turtle.turnRight()
end
return "true"
end

function waitRandom()
local Random = (math.random(1, 10) / 10)
sleep(Random)
return Random
end

function getID()
return os.getComputerID()
end

function getServerID()
– Sollte bereits jemand den Server angefragt habe an Request ranhängen
Server, Nachricht = rednet.receive(1)

– Wenn noch niemand Server angefragt selber anfragen
local i = 0
while Nachricht ~= "Ich bin Server!" do
if i == 0 then
rednet.broadcast("Wer ist hier Server?")
i = 25
end
i = i - 1
Server, Nachricht = rednet.receive(0.2)
end
return Server
end

function getPosition()
local tPosX, tPosY, tPosZ = gps.locate(2, true)
return tPosX, tPosY, tPosZ
end

function getRichtung(altPosX, altPosY)
if tostring(altPosY) == "nil" then
altPosX, altPosY = gps.locate(2, true)
print("Habe Position angefragt")
end
– Sich um einen Block in belibige Richtung bewegen
local altPosZ = 0
local neuPosZ = 0
local Richtung = 0
local BenHilfe = 0
i = 0
while turtle.detect() and BenHilfe == 0 do
if i == 4 then
if Richtung == 0 then
if turtle.up() then
i = 0
neuPosZ = neuPosZ +1
else
while neuPosZ ~= altPosZ do
turtle.down()
neuPosZ = neuPosZ - 1
end
if turtle.down() ~= true then
BenHilfe = 1
end
end
else
if turtle.down() then
i = 0
neuPosZ = neuPosZ +1
else
BenHilfe = 1
end
end
end
turtle.turnRight()
i = i +1
end
if BenHilfe == 0 then
print("Habe Bewegungsmöglichkeit gefunden")
turtle.forward()
neuPosX, neuPosY = gps.locate(2, true)
if neuPosX == altPosX then
if neuPosY < altPosY then
dreturn = 1
else
dreturn = 3
end
else
if neuPosX < altPosX then
dreturn = 0
else
dreturn = 2
end
end
turtle.back()
while neuPosZ ~= 0 do
if Richtung == 0 then
turtle.down()
neuPosZ = neuPosZ -1
else
turtle.up()
neuPosZ = neuPosZ -1
end
end
else
rednet.broadcast("Hilfe, bin eingegraben")
end
return dreturn
end

function up()
if turtle.up() then
PosZ = PosZ + 1
return true
else
return false
end
end

function down()
if turtle.down() then
PosZ = PosZ - 1
return true
else
return false
end
end


Need more Info: Please ask, i will try to help you helping me :)/>
NeverCast #2
Posted 09 January 2013 - 08:14 AM
PosZ doesn't exist in the API, the API executes in it's own function environment and is unaware of your other code in the turtle.
You need to put the Position variables in to your API and provide a getter and setter for them. :)/>

Also, Up and Down is the Y axis, not the Z, did you mean PosY = PosY + 1 in the up function?

Yeah you've fetched X and Y from the gps.locate but not the Z, you need to get the x and z as that is the north/south/east/west directions, y is up and down.
ichim #3
Posted 09 January 2013 - 08:18 AM
PosZ doesn't exist in the API, the API executes in it's own function environment and is unaware of your other code in the turtle.
You need to put the Position variables in to your API and provide a getter and setter for them.

Also, Up and Down is the Y axis, not the Z, did you mean PosY = PosY + 1 in the up function?

Yeah you've fetched X and Y from the gps.locate but not the Z, you need to get the x and z as that is the north/south/east/west directions, y is up and down.

Hmm … ok. thanks for the Information, then i will just store the Infos in the API. Should work or am i wrong again?

About the X, Y, Z: Thanks my bad. Would have been fun when i would send it away the first time … like Y + 1000 :)/>
NeverCast #4
Posted 09 January 2013 - 09:26 AM
Considering that y only goes to 255/256 that would be been very fun :P/>
D3matt #5
Posted 09 January 2013 - 01:20 PM
A better alternative would be to pass the X/Y/Z variables to the API as parameters in the functions.
ChunLing #6
Posted 09 January 2013 - 06:27 PM
Yeah, arguments and returns, that's the way with API functions. You can have a persistent value in the API for some things (and coordinates are actually good candidates), but if you can at all maintain those values in the local scope of your programs rather than having them in the API then you should.
NeverCast #7
Posted 10 January 2013 - 09:51 AM
I just figured they were being used as tracking variables instead of constantly querying the gps, so you would want them to be static throughout the entire API so that if you restarted the program the API would still hold the correct values.
ChunLing #8
Posted 10 January 2013 - 10:54 AM
Yeah, but the most common problem with coordinates is losing them to a chunk unload forced shutdown. Having them in the API doesn't help at all with that, you need to save them in a file.

Of course, having coordinates stored in the API and accessible globally doesn't hurt anything either (or shouldn't), which is why they are good candidates for that.
Zeph #9
Posted 06 August 2013 - 07:15 AM
You need to put the Position variables in to your API and provide a getter and setter for them. :)/>

How does one go about this? Preferably without passing (many) function args.