r/ComputerCraft • • Sep 22 '22

I made self Replicating Turtles!

https://youtu.be/MXYZufNQtdQ
70 Upvotes

9 comments sorted by

6

u/fatboychummy Sep 22 '22

Nice, self replicating turtles are always cool to see.

I also see you do tutoring for CC... Never thought I'd see that honestly, haha.

As an aside, had a cursory glance (at work, don't have a lot of time to look atm) around your code there, I will say that you definitely need to localize things. I see a few locals in there, but it's a good practice to localize every variable you use in Lua.

3

u/MerlinLikeTheWizard_ Sep 22 '22

Haha yep, I try but I probably forget like half the time. That'll be one improvement I'll make as I update the code.

As for CC tutoring, it's pretty niche! I was already tutoring Python and Scratch, and I figured CC is just as fun / good for learning to code so why not, right?

1

u/Existing-Strength-21 Sep 23 '22

I see people post about the local thing a lot. Is they a technical memory allocation reason for this? Or is it just a general good practice?

2

u/fatboychummy Sep 23 '22

General good practice as it changes how the variables are scoped.

Without locals, it sets a global variable (mitigated slightly as of 1.12.2 since it creates an env variable instead). These are slower to access and can cause issues by changing variables in scopes you wouldn't expect them to.

For example

function increment()
  x = x and x + 1 or 0
end

 -- pretend a ton of code is here referencing x

function doThing()
  x = "not a number"
  -- ...
end

So we get coding, and in our code are these two functions, spread out quite far. We wrote increment pretty early on, then forgot about having a variable named x when we later got to doThing.

So; x should be a number, but in doThing we set it to a string. Now, if we call increment(), it'll give us a lovely error: attempt to perform arithmetic on a string value.

Thus it can cause confusion.

The alternative is to localize the x in doThing. This creates a new variable, and will not change anything previously defined.

It's also a good idea to localize functions.

local function doThing()
  local x = "not a number"
  -- ...
end

1

u/stick_404 Sep 22 '22

I just have to say this is super impressive! And after watching a few older videos of yours, are you planing to make a fully autonomous bot base?

3

u/MerlinLikeTheWizard_ Sep 22 '22

Yep! In my wild dreams I am picturing something very like that. Though I would just add "fully autonomous expanding bot base."

1

u/Lower_Practice2564 Oct 10 '22

Hello Sir, if you wouldn't mind, I have some questions?

  1. I had given up on this project, seeing it as impossible, for a number of reasons. One is that, in my version, a mining turtle could not aquire wood, only an axe turtle could, and also I do not recall turtles being able to craft anything at all? Are you using a different version which allows mining turtles to craft/ collect wood?

  2. I found turtles quite obnoxious about randomly turning off. When they do this, they tend to be in some obscure and essentially impossible location when they are turtles whom I have given some autonomy. Do you have a work-around for this issue, or do turtles inherently de-activate somewhat randomly?

  3. I noticed the one turtle literally turned on another turtle, can turtles use turtles?

Thank you. This is a project I always wanted to do. I have built autonomous turtles in the past and used them to procedurally generate bases for me, however, I always thought a project of this complexity, where a turtle can 'survive' like this was out of the scope of ComputerCraft!

1

u/MerlinLikeTheWizard_ Nov 22 '22

Great questions!

  1. In the version I was using (CC: Tweaked for 1.18), mining turtles can mine wood, and crafty turtles can be made using a crafting table. I thought this was the case in all versions, but maybe you were using one I'm not familiar with.

  2. Yes, it's true turtles turn off when in unloaded chunks, and this was a problem. My (slightly cheating) workaround was to write a data pack to continuously load them in, or otherwise to follow them as the player.

  3. Yes! If I remember right, turtles can use peripheral.call('bottom', 'turnOn') to switch turtles / computers on. They then run startup.lua from any neighboring disk drive. This was the big feature that allowed the whole program to be possible in the first place, and I was so happy when I found it.

Thanks, it was fun putting all the pieces together to make it work.

1

u/robotboredom Mar 16 '24

THE FARO PLAGUE!!!