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

Can't figure out why my pathfinding is not working...

Started by renjestoo, 31 January 2018 - 12:50 AM
renjestoo #1
Posted 31 January 2018 - 01:50 AM
Hello,

I tried to create a pathfinding script for my turtle, so he can find his way back "home"
its a long time for me since I coded in LUA and especially within Computercraft, so I'm a little bit rusty.
Now… when I execute the program, I get the EXACT following errormessage:

==============================
sort.lua:190: attempt to compare string
with number expected, got string
==============================

I created a GPS tower and I already created 3 files with Coordinate locations.
he can read it out fine, but since I tried to write a file and read the file and assign it to a variable, I get this message.

I think the code is correct, but hey…. I didn't code LUA in about 6 years. Again… I am rusty :)/>
Could somebody please help me with the code?

The help would be appreciated :)/>

PS: don't mind the messy code, I will fix that later.

Link to code: https://pastebin.com/Jhy5Uu08
Bomb Bloke #2
Posted 31 January 2018 - 02:28 AM
As the error's telling you, you're attempting to compare a string to a number.

You could change this sort of thing:

homeX = XLoc.readLine()

… into this sort of thing:

homeX = tonumber( XLoc.readLine() )

… but it'd be much more efficient to put all the values you want to record into a single table, textutils.serialise() that to disk, and then textutils.unserialise() it back again when you read it later.
renjestoo #3
Posted 31 January 2018 - 03:17 AM
now I get another issue….
CC throws an error again, but now (for some stupid reason) the turtle reboots when I perform the command "go home" after starting the script.
so I can't see the error in my screen :S

I also tried to get a refresh when performing the command "go home" (or call the function "goHome" if thats more clear :P/> )

can you please check out my updated script?
ps: I also can't get the textutils.serialise() and textutils.unserialis() to work. So I still have the different Variables.

If you could help me with that too? Pretty please?? :)/>
I think I messed something up, but its 4:28AM right now, so my attention is getting worse hahahaha

link to updated script: https://pastebin.com/KfRKnaK0
Lupus590 #4
Posted 31 January 2018 - 11:19 AM
Lines 4,5,6 do nothing.

Line 78, the comment says that this function does a thing but it doesn't fo that thing, either you did a non-descriptive function name/comment or you don't know how to return.

Lines 88-96 can be simplified, same with 101-109.

You may be interested in how LAMA works, it does some similar things to what you are doing. (Original)(Remake)

Line 271, return would be better here.

You could move your write prompt out of the if statements so that you only need it once.
Bomb Bloke #5
Posted 31 January 2018 - 11:37 AM
When a computer/turtle starts running code, ComputerCraft starts a ten second timer. If that code doesn't yield before that timer ends then ComputerCraft will either crash the script or the whole computer (depending on the nature of the functions your script is calling). After each yield, any other systems waiting to run code may do so, then after they've all yielded, processing continues with a new time limit.

The reason why is that running your code chews up valuable server processing power, and so it shouldn't be able to monopolise it. In fact, only ONE CC device can run code at a time: While one is doing something, none of the others can do anything until it yields.

Whether or not it takes more than ten seconds for your code to execute has a little to do with the power of the Minecraft server it's running on, and a lot to do with how often you allow your code to yield. Pulling events (eg getting typed characters or checking timers) triggers a yield, and many commands (eg turtle movements, sleeping, or getting text input from the user) have to pull events to work anyway. Basically, anything that triggers a pause is pulling an event in order to do it, and in order to pull an event the code yields.

In your code, the loop you've got going between lines 273 through to 283 prints content to the screen, clears it, and then immediately repeats - without ever stopping in order to, say, actually move the turtle. If you bundled that loop off into a separate function and introduced a sleep() call into it, then you could run it alongside your goHome() function using the parallel API.

Regarding table usage:

local myTable = {}

myTable.homeX = 1
myTable.homeY = 2
myTable.homeZ = 3

local output = fs.open("home", "w")                 --# Open file to take text output.
output.writeLine( textutils.serialise( myTable ) )  --# Write text representation of the table's content.
output.close()

local input = fs.open("home", "r")
local myLoadedTable = textutils.unserialise( input.readAll() )
input.close()

print( myLoadedTable.homeX ) --# 1
print( myLoadedTable.homeY ) --# 2
print( myLoadedTable.homeZ ) --# 3

You can, of course, stick more stuff in your table than that.
Edited on 31 January 2018 - 10:41 AM