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

LUA Question how to make it count for filename

Started by guamie, 29 January 2013 - 05:13 PM
guamie #1
Posted 29 January 2013 - 06:13 PM
Hello! What I am trying to do, is have one turtle start different programs on multiple other turtles. the files will be names "1" "2" etc. Here is waht I was trying to do:


count=1
x=count
while count <= 10 do
if turtle.detect() then
fs.copy("disk/path/x","/path/x")
else
sleep(.1)
end
local t= peripheral.wrap
t.turnOn()
count = count +1
sleep(3)
end

How do I get it to see x as a changing variable? the sleep(3) is to allow turtles to move out of the way. I wanted to find a better way to do this but I couldn't come up with anything myself. All help making x a changing variable, and possible refining how the main turtle makes sure the last trutle loaded moves out of the way would be awesome.

I really appreciate it guys! I just got into computercraft a month ago and still feeling pretty lost!
NeverCast #2
Posted 29 January 2013 - 06:19 PM
Concat the string with the number
"disk/path/"..count


The .. joins strings and other values together.
So "disk/path/"..count puts the number on the end :)/>
guamie #3
Posted 29 January 2013 - 06:26 PM
You. Are. Awesome!

Any help you can give about a better way to get the main turtle to be able to detect when the turtles it is placing has moved out of the way for the next one?
slabb3r #4
Posted 29 January 2013 - 06:34 PM
You can also use this syntax:

 string.format ("disk/path/%s",count) 

If you code in C you will be familiar with it, and it is more adapted if you want for example to print numbers inside your messages. In this case you will have to use %d instead of %s.


For your other problem you could make the other turtles send a message back when they have moved.
guamie #5
Posted 29 January 2013 - 06:38 PM
well here's what i was doing. I am using 36 turtles all working in tandem to build a castle. I've written all 36 turtle's programs and wanted a way to have one of them start the others basically. what do you mean print numbers inside my messages?
guamie #6
Posted 29 January 2013 - 06:43 PM
If I was going to use rednet to send back a signal, would this work:

from turtle just deployed:

rednet.send(1, blarg, true)

main turtle:

count=1
x=count
while count <= 10 do
if turtle.detect() then
fs.copy("disk/path/"..count,"/path/"..count)
else
sleep(.1)
end
local t= peripheral.wrap
t.turnOn()
count = count +1
rednet.receive()
end

this is assuming main turtle is turtle 1
slabb3r #7
Posted 29 January 2013 - 06:52 PM
for example if you have a function count() that returns the number of turtles who have answered, you can write

print(string.format ("currently, %d turtles have answered",count())) 
instead of

print("currently "..toString(count()).." turtles have answered")
But it's not really important.

You program should work if the other programs send a response to turtle1 when they are activated.
guamie #8
Posted 29 January 2013 - 07:03 PM
thanks for the help, I really appreciate it from everyone who replied!