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

[Question] How do I run a program that I made?

Started by HavingPhun, 28 April 2012 - 11:35 PM
HavingPhun #1
Posted 29 April 2012 - 01:35 AM
Yes I know its a simple question. I thought I knew how too but I don't. I have a file located here:

rom/programs/RedControl/RedControl.lua

I typed this in:

cd rom/programs/RedControl

Then I typed:

RedConrol.lua

All this did was tell me if I had any programming errors. How do I run that program though? I tried something with shell.run() but I still couldn't get it to work. Can someone help me please? Thanks.
MysticT #2
Posted 29 April 2012 - 01:44 AM
Does it says "No such program" (or something like that), or just gives you an error?
If it gives you an error, you have to fix it to run the program, you can't run programs with errors.
HavingPhun #3
Posted 29 April 2012 - 01:52 AM
No errors, it just says "No such program".
MysticT #4
Posted 29 April 2012 - 02:07 AM
Well, you typed RedConrol.lua (missed a 't'), but I suppose that was just in the post.
It should work as you did. You can try running it like RedControl/RedControl.lua from the root directory, it should find it in the programs path.
Check if the file is in the correct directory and the directory exists.
Did you put the program in the rom folder outside the game, or with the edit program? cause in-game you can't put files in the rom directory.
HavingPhun #5
Posted 29 April 2012 - 02:22 AM
Well, you typed RedConrol.lua (missed a 't'), but I suppose that was just in the post. It should work as you did. You can try running it like RedControl/RedControl.lua from the root directory, it should find it in the programs path. Check if the file is in the correct directory and the directory exists. Did you put the program in the rom folder outside the game, or with the edit program? cause in-game you can't put files in the rom directory.
Yes that was just in the post. I put it in the rom folder from the outside of the game. ill try to run it again. Do I use 'shell.run()' or type the programs name after i type the directory with 'cd'?
MysticT #6
Posted 29 April 2012 - 02:25 AM
Just type in the terminal:
> RedControl/RedControl.lua
If that doesn't work, use:
> cd /rom/programs/RedControl
> RedControl.lua
it should run the program, if it doesn't there's some error in the path or some bug.
Luanub #7
Posted 29 April 2012 - 06:11 AM
Try putting it just in rom/programs, then type programs from the prompt and see if it shows up. If it is there and recognized as a program it should show up.
HavingPhun #8
Posted 29 April 2012 - 11:00 PM
MysticT: when I type the first thing in the terminal it says nothing and then if I type a function it says there is no such program. For the second one it doesn't say or do anything.

luanub: I tried putting it in rom/programs it shows up when I type 'programs'. But it doesn't do anthing when I run it and I can't run any functions, it just says no such program when I try to run them. Even if I save it as "RedControl" as all files so it has no file type it still shows up but I get the same result as the first when I try to run it.

What kind of bug could this be whats wrong with this? I want to run my programs.
Luanub #9
Posted 29 April 2012 - 11:12 PM
Well the fact that it see it and its in the list of programs is a good thing(I think… lol)

It might be something wrong with the program itself. Do you use shell.run() or anything similar that is calling another program?

Try making a hello world program and see if you can run that.

shell.run("clear")
print ("hello world")

This way your making sure you can execute your own program as well as a system program using the shell command.
HavingPhun #10
Posted 29 April 2012 - 11:49 PM
Ok I made a program with the code above and called it helloworld.lua. I typed programs and it showed up with the others. Then I typed it's name and it ran the program and said it hello world. My programs that I have made are just sets of functions should I see if adding some kind of prompt or have it say something will make it work? I'm gonna try it.
MysticT #11
Posted 30 April 2012 - 12:08 AM
You have to run the functions from the program, if there's just functions and no calls to them it won't do anything. If you want to make an API, you have to load it with os.loadAPI("path/to/API"). You can't run a program and then call it's functions from the terminal.
HavingPhun #12
Posted 30 April 2012 - 02:07 AM
I put my file in the apis folder and loaded it like you said. It said no such program. I tried removing the .lua extension and that didn't change anything either.

This is the code if it helps:


function activate(side)

    term.clear()
    term.setCursorPos(1,1)
    term.write "Which side would you like to activate? /n"
    side = read()
    rs.setOutput((side), true)
    term.write "Side activated. /n"
end

function deactivate(side2)

    term.clear()
    term.setCursorPos(1,1)
    term.write "Which side would you like deactivate? /n"
    side2 = read()
    rs.setOutput((side2), false)
    term.write "Side deactivated. /n"
end
 
MysticT #13
Posted 30 April 2012 - 02:12 AM
If you want to run it like a program, you have to add some function call, otherwise it would load the functions and exit, but you can't use the functions from outside the program.
To put the functions on an API just create a file with them (with no extension) and then use os.loadAPI("path/to/api") from another program, and use the functions in there like:

<apiname>.<function>()

Example:
API:

function sayHello()
  print("Hello!")
end

function blowUp()
  print("Boom!!")
end

Program:

os.loadAPI("testAPI")
testAPI.sayHello()
Luanub #14
Posted 30 April 2012 - 02:12 AM
Try this. Load it as an API, then either from the Lua shell of from within a program call it like RedControl.activate()


function activate(side) -- i dont think you need the argument for side since you will overwrite it with the read()
	term.clear()
	term.setCursorPos(1,1)
	write "Which side would you like to activate? /n"
	side = read()
	rs.setOutput(side, true)
	write "Side activated. /n"
end

function deactivate(side2) -- i dont think you need the argument for side since you will overwrite it with the read()
	term.clear()
	term.setCursorPos(1,1)
	write "Which side would you like deactivate? /n"
	side2 = read()
	rs.setOutput(side2, false)
	write "Side deactivated. /n"
end
Edited on 30 April 2012 - 12:14 AM
HavingPhun #15
Posted 30 April 2012 - 08:41 PM
Ok thanks guys. Also luanub good point about not needing side as an argument.