Jump to content




Cant edit a variable from API

api lua

  • You cannot reply to this topic
8 replies to this topic

#1 ichim

  • New Members
  • 10 posts

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)
Spoiler


Full Code API (Not as bad as the Turtle but still bad)
Spoiler


Need more Info: Please ask, i will try to help you helping me :)

#2 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

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.

#3 ichim

  • New Members
  • 10 posts

Posted 09 January 2013 - 08:18 AM

View PostNeverCast, on 09 January 2013 - 08:14 AM, said:

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. Posted Image

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 :)

#4 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 09 January 2013 - 09:26 AM

Considering that y only goes to 255/256 that would be been very fun :P

#5 D3matt

  • Members
  • 830 posts

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.

#6 ChunLing

  • Members
  • 2,027 posts

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.

#7 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

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.

#8 ChunLing

  • Members
  • 2,027 posts

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.

#9 Zeph

  • Members
  • 3 posts

Posted 06 August 2013 - 06:15 AM

View PostNeverCast, on 09 January 2013 - 08:14 AM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users