Question about Turtles

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
OK, so after asking a lot of question from forum users concerning turtles and the programming of (which the program works great), I ran into a major problem: when I log back on, the turtle has stopped and I have to remove it, place it back at its starting position and terminate the program. Is this normal and do I need a Chunk Loader?
 

Henry Link

Popular Member
Dec 23, 2012
2,601
553
128
USA - East Coast
There are a couple of solutions here. Easiest is on a SMP server you can use the chunkloader or use a chunkloaded turtle. If you are playing single player then you need to rewrite your programming so that it tracks where the turtle is at all times. Also, add a program called startup to the turtle that will run your program on world load.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Hello again, this is normal and it is one of the limitations of turtles. However, there is a method of dealing with it that I deliberately didn't tell you about on the other thread because I didn't want to give an information overload (which I probably did anyway, but oh well).

There is an api called fs: http://computercraft.info/wiki/Fs_(API) and one particularly important command in it called fs.open()

You can use this to make save files that will persist after you have closed the game. When you log back in you can code the turtle to read that save file. What I normally do is have a returnHome() function which will read the savefile and use it to make a judgement about where it is. Then I can code it to return to its starting position before running the program proper. Whenever the turtle does a movement that drastically affects its position and orientation ( a change of elavation or a change of direction maybe) you can have the turtle update the save file.
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
Hello again, this is normal and it is one of the limitations of turtles. However, there is a method of dealing with it that I deliberately didn't tell you about on the other thread because I didn't want to give an information overload (which I probably did anyway, but oh well).

There is an api called fs: http://computercraft.info/wiki/Fs_(API) and one particularly important command in it called fs.open()

You can use this to make save files that will persist after you have closed the game. When you log back in you can code the turtle to read that save file. What I normally do is have a returnHome() function which will read the savefile and use it to make a judgement about where it is. Then I can code it to return to its starting position before running the program proper. Whenever the turtle does a movement that drastically affects its position and orientation ( a change of elavation or a change of direction maybe) you can have the turtle update the save file.


How would I be able to do this as now it's getting beyond my scope of understanding...the gerbil part of my brain is starting to take over.

Also, how much EU can the turtle store? It keeps sucking up energy and gaining more movement points.
 

tompy97

New Member
Jul 29, 2019
85
0
0
I would also be interested in learning how the fs API works if there is anyone kind enough with enough patience to explain it (or better yet, point us in the direction of a semi-decent guide).

As far as how much EU a turtle can hold, it's limitless as far as I can tell...
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
There are a couple of solutions here. Easiest is on a SMP server you can use the chunkloader or use a chunkloaded turtle. If you are playing single player then you need to rewrite your programming so that it tracks where the turtle is at all times. Also, add a program called startup to the turtle that will run your program on world load.

What's a chunkloaded turtle?
 

tompy97

New Member
Jul 29, 2019
85
0
0
A chunkloader turtle is a turtle crafted with any chunk loader (I use chicken chunk spot loaders because of their cheap cost).
The turtle will now keep the chunk it is in loaded and will not restart unless the server restarts.
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
Will that make the turtle continue to move if I logged of and then back on again while it was moving?
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
No, it didn't. Just tested it and the turtle stopped traveling. I desperately need the info on the command line (fs.open()) and what code I have to use.
 

tompy97

New Member
Jul 29, 2019
85
0
0
A chunkloader turtle will keep the chunk it is in loaded, and will continue to operate even if players are not present. The only thing that will stop it from working is if the server resets. If you are playing on single player, than the server will reset every time you log out...
 

Hoff

Tech Support
Oct 30, 2012
2,901
1,502
218
Chunkloading turtles are broken in the current version of unleashed. If you wish to use them wait until the next update in which the mod they are from is being rolled back to a more stable version.
 

tompy97

New Member
Jul 29, 2019
85
0
0
Well, that's a bit of a setback then.

I'm currently trying to get my head around the fs API, all the info can be found on the wiki: http://computercraft.info/wiki/Fs_(API)

It is not easy for me to understand, i'm getting there though. I'll post help if I end up making any progress...
 

Furious1964

Well-Known Member
Nov 10, 2012
1,436
70
63
Still using the Ultimate Pack (my nephew and I will be manually updating it WITHOUT RedPower) and I'm playing in a Creative Single Player Test World. So, guess I'll have to wail for that code.
 

Hoff

Tech Support
Oct 30, 2012
2,901
1,502
218
Then the turtle must still be able to withstand a server restart or crash which a chunkloading module does not aid in. The only way to do that is to learn the code to save the information of the turtle to a file.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
As far as I know, a chunkloaded turtle is the same as a turtle in a chunk loaded area in that it won't restart unless the game restarts (or if you program it reboot, I don't know what will happen then). Making a chunk loader turtle is only necessary for things like mining turtles. If the turtle is in a chunkloaded space at all times it will also be fine.

The fs api is what you can use to work around with the file system, hence the name. Every program you write gets saved by default in a directory that you can access by typing "dir" into the command line (without the speech marks). You should see a list of all the programs you have saved, and depending on your mods there might be other stuff (particularly if you have open peripherals).

You aren't limited to saving programs to files, you could even write a story and save it as though you were using a word processor. More importantly, you can code the computer or turtle to read and write files. I am fairly new to this but I have now written a few working programs. I might not be doing it the very best way, but it is functional and I will try and explain it as best as I can.

I have found the two most useful functions in the fs api are as follows

Code:
fs.exists([I]string [/I]name of the file you are checking inside speech marks) -- this checks to see if a file exists in your directory. Useful for checking if a save file is being created for the first time and other similar stuff
fs.open([I]string[/I] name of the file again in speech marks,[I] string[/I] mode also in speech marks) -- the name of the file business is the same as with the other command. the mode allows you to specify whether you are opening the file in read only mode or writing mode or appending mode.

An important feature of fs.open is if the file you specify doesn't exist, it will create it (at least it does in write mode I haven't tested it in the other modes because the only reason I would ever open a new file is to write to it. I use fs.exists() to check if it needs creating)

fs.open is quite a complicated command because it doesn't do too much on its own, its more like wrapping a peripheral, only in this case the peripheral isn't a modem ore a chunk loader it is a file in the file system.

There are new commands (well they aren't really knew, they are very similar to read() and write()) that are exposed when you open a file with fs.open so you can manipulate the file, but first you need a handle on that file in the form of a variable (sorry, that was garbled I hope it makes sense).

Code:
local file = fs.open("saveData", "w") --[[ this attaches the handle to the variable file. you call later commands using this variable. the "w" shows that we have opened it in write mode]]
 
--now we can write a line to the file
file.write("data, 42, mac and cheese")
 
-- [[that line will appear on that file (which you can access by typing dir in the command line later). But before it gets saved
you need to close the file like so]]
 
file.close() --and it is as simple as that
 
--reading a file is very similar except you replace the "w" with an "r"
file = file.open("saveData", "r")
then we can say
local data = file.readAll() -- this saves the contents of the file to a variable that the current program can use. *(edited by me because I derped. I forgot to save it to a variable last time)
file.close()
 
-- which will return
 
>data, 42, mac and cheese

There is a bit more to it than that but there are the basics. fs.open() and all its sub commands are explained very well on this link to a part of the computercraft wiki

http://computercraft.info/wiki/Fs.open

Also, I find it is helpful to save serialized tables (you can serialize a table using the textutils api). They can be unserialized when you load them and I think they are practical at carrying your data.

When I write save files they usually just contain some important variables that can't be generated/discovered by the turtle
without my input and if it is a moving turtle I will add a short string that a returnHome() function in my program can interpret so the turtle can discover its starting position and go their to reset so it doesn't bung up its routing.

Sorry if that was a bit of a chore to read, but I hope it is at least clear enough to introduce the basics so you can get the most from the links.

Edit: Thats my second long rambling on this subject, sorry ladies and gentlemen, I will be quiet(er) now

Second edit: I made a mistake (sue me I'm very tired :)). I just wanted to let people know what it is so they don't make the same mistake. I have marked the mistake in the text with an asterisk(*). What I did was forgot to save the result of reading the file to a variable. It was a very simple fix though, you simply have to wrote local variableName = file.readAll(). Sorry if this led to confusion
 
  • Like
Reactions: Hydra and tompy97

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
I am posting again because I made a mistake in some of the code I wrote. I have edited the last post, but I am making a new one to trigger the alert of people who have already read it and possibly acted on incorrect information. the mistake was in the file.readAll() part of the second code block. My edits contain further details. Also, I just noticed in the first code block that there are randoms everywhere. That is not part of the code. That was me trying to italicise the stuff inbetween, namely the word string.

Shouldn't post stuff like that after a very very long day I suppose