Jump to content




[lua][question] Create a program with a program


  • You cannot reply to this topic
10 replies to this topic

#1 Finnvos123

  • Members
  • 22 posts

Posted 23 April 2013 - 06:16 AM

I've tried different things but nothing worked. How do I make a program create another program?
Like:
fs.open("test", w)
code to write the program
fs.close


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 23 April 2013 - 06:18 AM

local handle = io.open("outfile", "w")
if handle then
  handle:write([[
print("Hello, World!")
]])
  handle:close()
end


#3 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 23 April 2013 - 06:19 AM

EDIT: Damn ninjas.

You can use this simple piece of code:
local f  = io.open("test", "w")

f:write([[
	-- program code here
	print("Hello from test!")
]])

f:close()


#4 Finnvos123

  • Members
  • 22 posts

Posted 23 April 2013 - 06:21 AM

Could you also put comments in the code so it is easier to understand?

EDIT: nvm, I figured it out. Thanks for the quick help

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 April 2013 - 06:23 AM

Yeah

local handle = io.open("outfile", "w")
if handle then
    handle:write(
[[
-- Declare a function
local function foo( s )
    print( s )
end

foo( "Hello!" )
]])
    handle:close()
end


#6 Finnvos123

  • Members
  • 22 posts

Posted 23 April 2013 - 06:31 AM

local f = fs.open("startup", w)
f:write([[
	print"Starting...."
	sleep(1)
]])
f:close()
With this code I get:
test:1: Expected string, string

#7 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 23 April 2013 - 06:32 AM

Put quotes around w.

#8 Finnvos123

  • Members
  • 22 posts

Posted 23 April 2013 - 06:37 AM

Somehow the program/file it creates is completely empty.
After some testing I found out that it doesn't do anything to the file.

#9 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 April 2013 - 06:49 AM

Hmmm...if your full code is the above with just the "w" replacement. It really should work...

Have you tried restarting the PC? Lol typical IT solution. Might you have edited io somehow.

Try fs?
local f = fs.open("startup","w")
w.write([[print"Starting...."
sleep(1)]]
)
w.close()

Really the same thing...
Have you changed any ComputerCraft files?

#10 Finnvos123

  • Members
  • 22 posts

Posted 23 April 2013 - 06:51 AM

And by restarting it suddenly worked.
HOW!?

#11 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 April 2013 - 07:06 AM

Haha. You probably overwrote something sometime...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users