Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
AshThatBurns

Make a robot return home no matter where it is

Question

Heyy!

I'm trying to create a program that will make a robot return to its 'home coordinates' no matter where it is in the world. 

 

What I intend to do is to create a function that moves one step per call. As long as the current coordinates are not equal to the home coordinates, it will keep calling the 'move' function recursively. (I'm still relatively new to this mod, but I've done a bit or programming before... ^^) 

 

I haven't taken into account about the direction that the robot is facing, Ill keep the direction constant while testing, and will work on that later...

For now, this is my code:

 

 

 

local robot = require("robot")

 

//these are the 'home coordinates'

x=-23

y=63

z=142

 

    function move(

        curX = debug.getX()

        curY = debug.getY()

        curZ = debug.getZ()

        if curX = x and curY = y and curZ = z then

            return

        elseif curX < x then

            robot.forward()

        elseif curX > x then

            robot back()

        elseif curY < y then

            robot.down()

        elseif curY > y then

            robot.up()

        elseif curZ < z then

            robot.left()

        elseif curZ > z then

            robot.right()

        return move

        end

    )

move()

 

 

 

This probably looks like lousy programming, but I'm still trying to learn Lua, I'm more used to Java programming ^^.

Anyways, my problem is that when I try to run this, it keeps giving me and error saying:

 

return:7: ')' expected near '='

(return is the name of the script)

 

Could anyone help correct this error and any other errors in this script please?

Much appreciated!!!

Ash :3

 

PS: Also, if you have any ideas as to how to go about programming the part which takes the robot's direction into account, then by all means please share :3

Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 0

You have a couple of problems here, the first isn't a major but worth noting:

 

1. Without cheating this will only work in creative (debug card is not available to craft so you can't get the world co-ordinates, although I'd love to be corrected on this)

 

2.

 

You are referring to the ability to perform path finding, the most common is A* Pathfinding (and is possible in Lua). The problem however is that you need to move the robot, detect which direction to go next, detect if it can go in that direction then move and repeat until you find a situation you are in where you can only move back. As this is extremely time consuming if you're planning on having the robot do this from anywhere in the world, it may find it's self stuck in a cave dead end for a very long time before it works it's way back. Additionally you'd need to keep the chunk loaded while it worked out the route.

 

I've done a quick Google and there are a lot of examples of "Lua A* Pathfinding" which should get you going however  :) I've not tried this in Lua but based on a the Google results there is nothing stopping this from working with some success.

 

To fix your code you've put the closing bracket on the function in the wrong place, set up a function like this:

function move()
  -- place code here to do when calling move()
end

-- Call move
move()

Also I'd recommend changing the way you handle your variables for home to something like, it allows you to give new home co-ordinates by manipulating the x, y and z values, whilst not needed for your example code above it may prove useful in A*:

local x = ##
local y = ##
local z = ##

function move(x, y, z)
  -- do movement stuff
end

move(x, y, z)
Edited by Harkole
Link to post
Share on other sites
  • 0


function move(

--// you're using incorrect syntax here.

curX = debug.getX()--

curY = debug.getY()

curZ = debug.getZ()-- You're setting values in the parameters field of a function call...

if curX = x and curY = y and curZ = z then

return

elseif curX < x then

robot.forward()

elseif curX > x then

robot back()

elseif curY < y then

robot.down()

elseif curY > y then

robot.up()

elseif curZ < z then

robot.left()

elseif curZ > z then

robot.right()

return move

end

)

Define functions like so..


-- declare local unless you intend global access.

local _x, _y, _z = getx(), gety(), getz() --get players pos. not real functions...

local function move(x, y, z) -- x, y ,z are parameters passed to a function

--// do movement...

end -- close the function declaration!

--// call with pos args..

move(_x, _y, _z) -- do the actual movement

Also note that lua is case sensitive. So Local ~= local. All lua standard libraries and keywords are lowercase. So 'local' not being the same as Local is giving you funny syntax errors...

Link to post
Share on other sites
  • 0

Thanks a bunch!!
I'll look into it and try it out :3
It'll take some time cuz I just lost my map because of a game crash which im about to report :(
Thanks again!!! :D


Okay never mind,
what happened was that I had a wall with a hole 2 blocks deep. I placed a computer case and added all it's components, then started the computer. This caused my game to crash whenever I started the map. I undid this by spamming left click in the direction of the computer whenever the world started :3 xD
Anyways, im gonna go try it now :D
Thanks again!!! :D


Hey Molinko!
Thanks a lot for your input, I'm fixing it as we speak :)
Btw, this script is meant to return to a home coordinate, not a player's position.
HOWEVER, that is a very interesting idea to pursue! :D
I might try it out later on after I get this script to work as perfect as possible :P


Okay so…
I’ve changed a few things with my code, and there is a new error which I can’t put my finger on >:/
Here’s my new code:

Local component = require(“component”)
local robot = component.robot
local nav = component.nav

//these are the 'home coordinates'
Local x = -22.5
Local y = 63.5
Local z = 14.5

Local function move(x,y,z)
Local curX, curY, curZ = nav.getPosition()
if curX == x and curY == y and curZ == z then
return
elseif curX < x then
robot.forward()
elseif curX > x then
robot.back() <-- error allegedly here
elseif curY < y then
robot.down()
elseif curY > y then
robot.up()
elseif curZ < z then
robot.left()
elseif curZ > z then
robot.right()
return move(x,y,z)
end
end
move(x,y,z)Okay so…
I’ve changed a few things with my code, and there is a new error which I can’t put my finger on >:/
Here’s my new code:

Local component = require(“component”)
local robot = component.robot
local nav = component.nav

//these are the 'home coordinates'
Local x = -22.5
Local y = 63.5
Local z = 14.5

Local function move(x,y,z)
Local curX, curY, curZ = nav.getPosition()
if curX == x and curY == y and curZ == z then
return
elseif curX < x then
robot.forward()
elseif curX > x then
robot.back() <-- error allegedly here
elseif curY < y then
robot.down()
elseif curY > y then
robot.up()
elseif curZ < z then
robot.left()
elseif curZ > z then
robot.right()
return move(x,y,z)
end
end
move(x,y,z)


The error message says:

Return:16: attempt to call field ‘back’
(a nil value)
Stack traceback:
/moves/return:16: in function ‘move’
/moves/return:28: in main chunk
(…tail calls…)

The problem is, I don't see what's wrong on line 16 :/
Please help!
Thanks!! :D

Sangar: merged your posts. Please don't quadruple-post. There's an edit function for a reason ;)

 

Sorry!! :o

Link to post
Share on other sites
  • 0

Hey Ash,

 

The nil result suggests the robot failed to move for some reason, the explanation of move from the Robot API reads:

 

Returns: true if the robot successfully moved, nil otherwise. If movement fails a secondary result will be returned describing why it failed, which will either be 'impossible move', 'not enough energy' or the description of the obstacle as robot.detect would return.
The result 'not enough energy' is rarely returned as being low on energy usually causes the robot to shut down beforehand.
The result 'impossible move' is kind of a fall-back result and will be returned for example if the robot tries to move into an area of the world that is currently not loaded.

 

 

I've not done anything with Robots yet so Molinko maybe a better person to help you, but in an attempt to keep you moving forward with your problem, I think it's returning "nil" because it can't move, it maybe worth catching the second returned value and checking if it has a value?

-- catch movement errors
success, reason = robot.back()

-- debug output
if(success) then
  print("I moved back")
else
  print("I can't move because "..reason)
end

Hope that helps move you forward so to speak!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.