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

[Question] program that creates a program

Started by godsrock, 10 September 2012 - 04:06 PM
godsrock #1
Posted 10 September 2012 - 06:06 PM
I'm trying to create a program that does something simlilar to buildcraft blueprints.

I want to make a program that detects the block and destroys a rectangle of <"Forward"> <"Up"> <"Right"> then automatically creates a program and writes code in the programs as you would normally do with the shell.run("edit","program") to replicate what was broken though a new command the program made.

I was looking at the fs api but I can't get it to write programs with it.

I tried this program
–Writer–

h = fs.open("builder", "w")
h.write(print("text"))
h.close()

All it does is make builder in the directory.
I thought h.write() would put text into the builder but it doesn't do anything but print on the main monitor

I also tried to read the builder file after
–Reader–

h = fs.open("builder","r")
h.readLine()

effectively does nothing because I can't get –Writer– to write code for builder.

I just need help with the –Writer– code the rest i can figure out.
Thanks very much in advance.
D3matt #2
Posted 10 September 2012 - 06:28 PM
write() takes a string input, so you need to put quotes around the text you're writing. Since you need quotes inside of your program it may be easier to use a multiline string.

IE:

h.write([[print("text")]])
godsrock #3
Posted 10 September 2012 - 06:43 PM
Thanks D3matt that is just what I needed.
godsrock #4
Posted 10 September 2012 - 09:53 PM
Is there any other way to write it because doing it this way writes all the code on one line? It works but i can't read what it is doing.
GopherAtl #5
Posted 10 September 2012 - 10:01 PM
Just add "n" to the end of each line; print does this automatically, but write, whether on a file or through term or monitor, does not.

ex, h.write([[print("text")n]])

I was dumb a couple of weeks ago.
godsrock #6
Posted 11 September 2012 - 03:49 AM
How do you fix an error saving to "filename"?
godsrock #7
Posted 12 September 2012 - 06:02 PM
Okay, I'm practically done with my program. It does everything that I wanted it to. The only problem is it requires a clear flat space to run the programs, well very little needs cleared for the write.

I wanted to know if there was a way to shorten it. I think there must be a way to shorten my block(1-16) functions. Any ideas how?
Edited on 14 September 2012 - 02:51 PM
icehaunter #8
Posted 12 September 2012 - 10:42 PM
Here, instead of all these functions, you can do simple one:

function block()
	for i = 1, 16, 1 do
		turtle.select(i)
		
		if turtle.compare() then
			h = fs.open("builder", "a")
			h.write("
			turtle.select("..i..")
			n
			turtle.placeDown()"
			)
			h.close()
			print("block"..i)
			turtle.dig()
		else
			a=i
		end
	end
end

I don't quite get what do you need these a-b-c-d variables for..
but if you still need them, you can always write them into the array, writing

arr = {}

at the start and replacing "a" in my code with arr

Hope that helped^^
godsrock #9
Posted 13 September 2012 - 04:49 AM
I like that idea icehunter but i can't get it to write a number instead of ("..i..")
I think it is because h.write function has to be a string. Also the a-b-c-d… variables are just for telling the turtle that there is a space because it doesn't detect any of the things in it's inventory. If I could get your block function to work i wouldn't need all of them.


Anyway I was trying to add more functionality to my program so now you can ask that it be saved in another location then 'builder'. The problem is you only get one chance to use the save. If you try and make it in an invalid directory you have to rename the file/move it manually. if it fails it will say copy failed. Is there any thing I can do to fix that. I was trying to make it not go to the next function if it fails..



Imput = "read()"
fs.makeDir("blueprints")
fs.copy("builder",read())

I also am looking for a way to have a function that you can type into that will write everything to the program inside of a print that way you will not accidentally interfere with the program and can call the data on program start.

I feel this is needed because turtles can't remember what is in there slots so if the user can write so they know what it was and in what slot then the program can be very useful.

I think i got this program from someone else… anyway whenever I try to alter it a little it doesn't write at all…


file = io.open("builder", "a")
print("Block Copier 1.0")
print("Enter debuging/write() txt only")
print("Write slot# and type for all blocks")
print("This is for your reference later")
print("DON'T ENTER COMMANDS")
print("Type 'exit' to continue")
local text

while true do
  text=io.read()
  h = fs.open("builder", "a")
  if text ~= 'exit' then
	file:write(text.."\n")
	else
	  break
	end
end
file:close()
h.close()

I made so some more printed text to help the user not much different…
icehaunter #10
Posted 13 September 2012 - 08:01 AM
Look, I have got my own code, that needs to write iteger variable to file. It works just fine with int..
Anyway, try to put everything in a variable before writing it down:

local tempStr = "turtle.select("..i..")
n
turtle.placeDown()"
h:write(tempStr)

Also

Anyway I was trying to add more functionality to my program so now you can ask that it be saved in another location then 'builder'. The problem is you only get one chance to use the save. If you try and make it in an invalid directory you have to rename the file/move it manually. if it fails it will say copy failed. Is there any thing I can do to fix that. I was trying to make it not go to the next function if it fails..

I really don't get this. What exactly are you trying to achieve?
godsrock #11
Posted 13 September 2012 - 05:27 PM
Thanks a lot icehunter now i can reduce the size of the files and have it function much faster by having a variable for the number of slots "s" so i don't have to go though all the slots if not needed. I don't quite understand the local variables… I keep getting expected number for the function block() line…

function block()
        for j = 1, s, 1 do
                turtle.select(j)               

if turtle.compare() then
local tempStr = turtle.select("..j.") 
h = fs.open("builder", "a")
h:write(tempStr) 
                        h.close()
                        print("block"..j)
                        turtle.dig()
                else
                        a=1
                end
        end
end


"I really don't get this. What exactly are you trying to achieve? " (icehunter).
that was me trying to make it so that when I copy to a invalid location it repeats the function. I'm trying stuff like:


local file = fs.copy("builder",Imput)
Imput = "read()"
fs.makeDir("blueprints")
while (file==nil) do
fs.copy("builder",read())
until (file~=nil)
end
icehaunter #12
Posted 13 September 2012 - 10:47 PM
To help with such an error I need couple lines of code just before that function.

About doing anything till valid input:

--Create a flag to handle this:
local flag = false

while not flag do
--here is the input part (btw, iNput, not iMput. I made same error then i was learning english (look at my location) )
--validating input here and if everything is ok, changing the flag (flag = true)
--this will break the loop and allow you to carry on with good and needed input :)/>/>
end
icehaunter #13
Posted 13 September 2012 - 10:51 PM
Also, is your "s" variable declared not on function? I do not know, if there is any use for it other than this function, so maybe it would be better to do function block( s )?

AND there is an error in your code, which might have coused errors:

--ERROR
local tempStr = turtle.select("..j.")
--YOU NEED
local tempStr = "turtle.select("..j..")"
Cranium #14
Posted 13 September 2012 - 11:22 PM
Also, is your "s" variable declared not on function? I do not know, if there is any use for it other than this function, so maybe it would be better to do function block( s )?

AND there is an error in your code, which might have coused errors:

--ERROR
local tempStr = turtle.select("..j.")
--YOU NEED
local tempStr = "turtle.select("..j..")"
Another way you can change this into a string, would be to have the variable the same, but call back to it by tonumber(tempStr).
This is called Coersion, and it will try to make whatever is in () into a string if possible. There is also a tonumber function as well, and it does just what it says, and tries to make something a number/integer.
godsrock #15
Posted 14 September 2012 - 04:10 AM
I'm just going to post my messed up program. I'm getting the function to run but it is writing table values which I really don't want or understand: My program was not built with this in mind. When i try to run the program my program makes it just says for string "5e395c". I'm sorry if I'm annoying you because i just don't get it…


table: 5e395ctable: 5e395c
table: 146d222table: 146d222
table: 4ec8e2table: 4ec8e2
godsrock #16
Posted 14 September 2012 - 04:40 PM
okay, I feel really stupid I just had some really simple mistakes… Thanks for your help guys. It works so much better now.
icehaunter #17
Posted 14 September 2012 - 07:47 PM
Dude, really. Get yourself notepad++ and use TAB) look:
Here is your file, just with tabulation. And there is a little modified. Isn't it easier to read?
godsrock #18
Posted 15 September 2012 - 12:35 AM
ya… that's much better. I haven't been using spaces because I thought it might mess things up.

Thanks for cleaning up my program icehunter. I really didn't have any organization to it.

I'm just posting this one because for some reason arguments must come first in the code and the two posted before needed minor fixes. Also I think I just fixed a bug my created program had


repeat
turtle.down()
until turtle.detectDown()
turtle.up()

for some reason it would repeat the function a random number of times. I would have to say 'yes' for the next function to happen but it would continually do turtle.down/turtle.up. It was like it forgot it detected down. So you would have to keep typing 'yes' and hope it worked the next time. Anyway I'm hoping a simple sleep(1) after turtle.up() would fix the problem.
icehaunter #19
Posted 15 September 2012 - 12:40 AM
anyway, is everything working now? have you got any more questions? If everything is solved, I am glad and unfollowing this topic :)/>/>

Good luck with coding^^
godsrock #20
Posted 15 September 2012 - 01:22 AM
Everything works thanks icehunter :)/>/>