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

Frequently Asked Questions

Started by Bubba, 18 September 2012 - 10:00 PM
Bubba #1
Posted 19 September 2012 - 12:00 AM
Updated to the latest version of CC (1.5) and all questions that might be pertinent to that version.

Lately I've noticed that a lot of the same questions are being asked over and over… and over again. So I've made this to hopefully stop the 'spam' so that the real questions can shine :)/>/>

Q: My turtle is not moving! Why?
SpoilerTurtles require fuel. That means anything you can burn in a furnace, you can use as fuel for the turtle. Use the built-in program "refuel [amount]" to refuel your turtle, or use the function turtle.refuel() to do so manually in a program. NOTE: if you do it manually with the function, the slot which contains the fuel must first be selected with turtle.select(slot).

Alternatively you can disable this option in the CCTurtle.cfg config file.

Q: Events. How do I use them and/or what are they?
SpoilerAn event is, essentially, anything that happens which ComputerCraft deems worthy to notify the user of (for example, key presses or redstone signal changes). You can detect these events by calling the os.pullEvent([event]) function. For example, if I wanted to pull a key press, I could write os.pullEvent("key") and only key presses would be detected. You could also leave the area inside the parentheses blank, and then distinguish different events later. Further details can be found on the wiki.

Q: Where do I put programs I download?
SpoilerPut them in .minecraft/mods/ComputerCraft/lua/rom/programs/

Q: What are peripherals and how do I use them?
SpoilerPeripherals are, at the most basic level, addons for computercraft which extend its functionality in some shape or form. An example of this is the printer peripheral. To access the functions of peripherals, you must first 'wrap' them by calling peripheral.wrap([side]). There is more info at the wiki.

Q: How do I make an api?
SpoilerWrite a program with functions such as this one:

function sayHi()
print('Hi!')
end
Save that file to .minecraft/mods/ComputerCraft/lua/rom/apis/ or load it with os.loadAPI(filename)
Access the functions in the api with filename.function(). For the example above if I saved it as exampleAPI, I could access it by writing 'exampleAPI.sayHi()'.

Q: How can I detect mouse clicks?
SpoilerUse os.pullEvent("mouse_click"). This returns the mouse button that was pressed, and the x and y coordinates of the mouse_click event.

Q: Can I start a computer/turtle remotely?
SpoilerDepends on what you mean by remotely. You can do it with peripherals if you have another turtle or computer right next to the one you want to start. But at this point in time you can't turn on a turtle or computer from a distance.

Q: How do I write to files or how do I read from files?
SpoilerThere is in fact a computercraft wiki. A very useful tool if you do not know how to use something. The relevant link for file access is here.

Q: My file writing program is writing everything on the same line. How do I fix this?
SpoilerTwo methods: one is to use fs.writeLine() rather than fs.write(). Another is to use fs.write(string.."\n"). The "\n" signifies that you would like to start a new line. This can be useful if you wanted to break a string in the middle and write the second half to the next line.

Q: How do I write a program that will accept arguments such as "excavate 3"?
SpoilerAt the beginning of your program, create a variable like this:

tArgs = {...}
That will take any arguments given to the program and put them in the table tArgs.

Q: Programs that access the (real) internet are not working
SpoilerDid you check the config file to enable HTTP api? Yes? You have another problem and you should feel free to ask. No? Well I would suggest you do that by going to your .minecraft/config folder and opening mod_ComputerCraft.cfg and editing the line that says "enableAPI_http=0" to "enableAPI_http=1".

Q: If I'm playing on a server and I want a program how can I get that without typing the whole thing out?
SpoilerIf the server has http enabled, use
pastebin get <code> <name of downloaded code file>
If it doesn't there are some handy utilities such as this one or this one that will type them out for you. Or you could just ask the server owner to upload the file for you. They're generally pretty nice guys ;)/>/>

Q: How can I get the ID of an item?
SpoilerShort answer: you can't with just default computercraft. Long answer: You can download something such as OpenCCSensors and have it do the dirty work for you, or you can do it the hardcore way and just do it by of comparing items. Nitrogenfingers has written a great tutorial to help you do this.

Q: How can I save tables to a file and then load that table later?
SpoilerUse textutils.serialize(table) and textutils.unserialize(string). Write the string returned by textutils.serialize(table) to a file and then retrieve it later and use it in textutils.unserialize(string). Awesome huh?

Q: How do I use printers in my code?
SpoilerPrinters are peripherals, meaning that you have to "wrap" them before you can access any of their functions. Once you have wrapped the printer, you can then use any of its 9 functions. The functions and their usage can be found at the wiki.

Q: I'm having trouble with basic lua knowledge such as while loops or for loops
SpoilerGoogle is your friend, my friend. Type 'lua ' + basic problem you are having trouble with. For example: "Lua while loops" will give you this excellent tutorial.

Q:How do I use monitors?
SpoilerMonitors work much like printers in that they are peripherals and you must wrap them first before usage. Their functions are outlined at the wiki.

Q: I'm getting this error…
SpoilerBefore you ask how to fix your error, try checking this and seeing if your error is listed there. If not, keep reading.

Q: My question is not answered here… What do I do?
A: First, check out the CC wiki. If your question is not answered there, read this AND check through the first three pages of the Ask a Pro/Tutorials forums. If you question remains unanswered, come and ask on Ask a Pro. People are always willing to help, and don't be afraid of sounding 'stupid'. We all have to go through the same learning process as you, so please, ask away… Unless of course your question is answered in the list above. In which case, please don't ask :)/>/>

If anyone has suggestions or problems with this post, please tell me!
Cranium #2
Posted 19 September 2012 - 12:14 AM
I like this. This should be the first thing that new coders/scripters see when they come asking for help.
Lettuce #3
Posted 19 September 2012 - 02:49 AM
This is incredible. I have seen and answered these same questions, sometimes multiple times. You've been 1 upped, and like Cranium said, I hope people come here and see this, as it answers several actually common questions. If I think of any more I see regularly, I'll bring it to your attention.

I try to help people on the "Ask a Pro" section very often, so I'm sure I'll find one.

Hold on, I did just think of one. Parenthesis. People forget one or both, and get '=' expected. That's confusing, and asked often.

–Lettuce
Bubba #4
Posted 19 September 2012 - 03:43 AM
This is incredible. I have seen and answered these same questions, sometimes multiple times. You've been 1 upped, and like Cranium said, I hope people come here and see this, as it answers several actually common questions. If I think of any more I see regularly, I'll bring it to your attention.

I try to help people on the "Ask a Pro" section very often, so I'm sure I'll find one.

Hold on, I did just think of one. Parenthesis. People forget one or both, and get '=' expected. That's confusing, and asked often.

–Lettuce

Thanks for the praise and giving me another to add to the list :)/>/> I updated my post with a link to Codex of Error Slaying as that should cover the majority of commonly made errors.
KaoS #5
Posted 19 September 2012 - 06:21 AM
I would advise them to pin this thread to the top of the Ask a Pro section, it could significantly decrease post count. good job there
GopherAtl #6
Posted 19 September 2012 - 07:10 PM
another one that gets asked a lot (though not often clearly, it's something people want to know how to do anyway)

http://www.computercraft.info/forums2/index.php?/topic/4232-lua-how-do-i-write-a-program-that-takes-an-argument-from-me-at-runtime/

basically, "how do I make my program take parameters when run from the shell?"
Bubba #7
Posted 19 September 2012 - 09:51 PM
another one that gets asked a lot (though not often clearly, it's something people want to know how to do anyway)

http://www.computercraft.info/forums2/index.php?/topic/4232-lua-how-do-i-write-a-program-that-takes-an-argument-from-me-at-runtime/

basically, "how do I make my program take parameters when run from the shell?"

Added
Bubba #8
Posted 29 October 2012 - 02:52 AM
Updated the FAQ to the most recent version of ComputerCraft
Lyqyd #9
Posted 27 January 2013 - 07:52 AM
I'm not planning on pinning this (as I'd copy the content out first and create a new post), but this is linked to from the Ask a Pro "Useful Tutorials &amp; Resources" topic. :)/>
Bubba #10
Posted 27 January 2013 - 08:14 AM
I'm not planning on pinning this (as I'd copy the content out first and create a new post), but this is linked to from the Ask a Pro "Useful Tutorials &amp; Resources" topic. :)/>

Sweet :)/> I'm glad to hear that it's not been entirely forgotten XD
TeaPartyIdiot #11
Posted 12 May 2013 - 03:50 PM
How do I get a program to detect wether it is running on a Turtle or a computer?
KaoS #12
Posted 12 May 2013 - 04:05 PM

if turtle then
  --is turtle
else
  --is not turtle
end