Jump to content




[LUA] Global vector keeps getting reset?

lua turtle

9 replies to this topic

#1 wackozacko

  • New Members
  • 6 posts

Posted 08 September 2012 - 02:03 AM

So, I've just started with ComputerCraft and LUA, but I come from a programming background so it hasn't been too hard to pick up. Until now.

As a part of learning, I'm trying to write a simple program for a mining turtle so that, starting from a chest, it will dig a shaft N by M down. When it runs low on fuel or fills it's inventory up, it will return to the chest and dump everything before refuelling. I'm doing my path-finding with vectors. The start location is vector <0,0,0>, the mineshaft starts at vector <2,0,0> (2 blocks away from the chest), and every time I move I increase the vector appropriately.

The plan was, when the turtle does need to return to the chest, it can record it's current location as a vector, navigate to <0,0,0> to drop everything, and then use the previously recorded position to return to where it was last.

The problem is that reading the vector in, that I thought I'd stored, always returns <0,0,0> no matter what, so the turtle starts from where it's at and destroys the chest it just dumped everything into.

I think it's a scoping issue, but despite researching it for the last couple of hours, I haven't figured it out.

The relevant code is as follows:
Spoiler


#2 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 08 September 2012 - 02:09 AM

I'm unsure if vectors work in ComputerCraft. I know tables do, try using that. You can find a lot of ComputerCraft stuff on the wiki. Just Google it. Tables are pretty simple.

#3 wackozacko

  • New Members
  • 6 posts

Posted 08 September 2012 - 02:12 AM

Vectors do work, though they are somewhat basic. I'm more just using them to store x,y,z coordinates than any complex mathematics.

http://computercraft...tle=Vector_(API)

#4 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 08 September 2012 - 02:26 AM

Well, I don't see any part that modifies the vectors. I guess you do it in the movement functions (forward, back, etc.), so you should post them.

#5 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 08 September 2012 - 02:32 AM

I see. Personally, I've never used them. Learn something new everyday, eh? I like variables, and I have done the same things with them.
(x, y, z coordinates) If you are dead-set on vectors, I can't help you, I'm inexperienced with them. Someone else will eventually pick this up. But if you are willing to use a table, I'll be happy to lend what knowledge I do have. I'm not incredibly experienced, but I know enough to get most of my programs running.
--Lettuce

#6 wackozacko

  • New Members
  • 6 posts

Posted 08 September 2012 - 02:33 AM

View PostMysticT, on 08 September 2012 - 02:26 AM, said:

Well, I don't see any part that modifies the vectors. I guess you do it in the movement functions (forward, back, etc.), so you should post them.

The only vector that is regularly being modified is the location vector to keep track of where the turtle is. All the code that references mineLocation has been posted above.

For reference I'm including my movement functions and a couple of the supporting functions below.
Spoiler


#7 wackozacko

  • New Members
  • 6 posts

Posted 08 September 2012 - 02:35 AM

View PostLettuce, on 08 September 2012 - 02:32 AM, said:

I see. Personally, I've never used them. Learn something new everyday, eh? I like variables, and I have done the same things with them.
(x, y, z coordinates) If you are dead-set on vectors, I can't help you, I'm inexperienced with them. Someone else will eventually pick this up. But if you are willing to use a table, I'll be happy to lend what knowledge I do have. I'm not incredibly experienced, but I know enough to get most of my programs running.
--Lettuce
I figure it's possible to do it with tables and other variables, but I don't know if that would solve the problem I've run into. My other vectors seem to be working fine so I don't know what the problem is. I don't really want to go through a whole rewrite of the code just cause a single vector doesn't want to work and the rest do.

#8 Xtansia

  • Members
  • 492 posts
  • LocationNew Zealand

Posted 08 September 2012 - 03:37 AM

I'm not sure if it might be a reference issue, where mineLocation just becomes a 'pointer' to the exact same object as location instead of a copy
Try changing:
mineLocation = location
to:
mineLocation = vector.new(location.x,location.y,location.z)


#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 08 September 2012 - 04:02 AM

First, if you're not opposed to using an external API, you could check out my Location API, which is very similar to the vector API with a few added things to make turtle tracking much easier. The latest version can be found here; the discussion topic on these forums is found here. Even if you don't choose to use it, please feel free to take a look through the code. It's not very long and it may help you tighten up some of your code. If you have any questions about it, feel free to ask here or PM me.

Also, when printing vectors (and locations, if using my API), passing the vector or location to tostring() will output a reasonably formatted (comma-separated) string output.

I would most likely suspect that mineLocation = location line, that does assign mineLocation to simply point to whatever table location points to already, so they are then linked until either of them is set to a new value.

#10 wackozacko

  • New Members
  • 6 posts

Posted 08 September 2012 - 09:23 AM

View Posttomass1996, on 08 September 2012 - 03:37 AM, said:

I'm not sure if it might be a reference issue, where mineLocation just becomes a 'pointer' to the exact same object as location instead of a copy
Try changing:
mineLocation = location
to:
mineLocation = vector.new(location.x,location.y,location.z)

SUCCESS!! Thanks so much for your insight. Wish I had asked on these forums sooner.

View PostLyqyd, on 08 September 2012 - 04:02 AM, said:

First, if you're not opposed to using an external API, you could check out my Location API, which is very similar to the vector API with a few added things to make turtle tracking much easier. The latest version can be found here; the discussion topic on these forums is found here. Even if you don't choose to use it, please feel free to take a look through the code. It's not very long and it may help you tighten up some of your code. If you have any questions about it, feel free to ask here or PM me.

Also, when printing vectors (and locations, if using my API), passing the vector or location to tostring() will output a reasonably formatted (comma-separated) string output.

I would most likely suspect that mineLocation = location line, that does assign mineLocation to simply point to whatever table location points to already, so they are then linked until either of them is set to a new value.

I'm not opposed to an external API, but the purpose of this exercise was learning LUA, not actually getting a working miner. I did take a look at the API and might well use it, or at least integrate part of it, in future. Very clean. Going to have to learn LUA coding standards as well as just the language.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users