[Tutorial] Basics of Computercraftprogramming

    • Official Post

    Due to popular demand (more than one person), i will make a short Basictutorial about CC-Programming, so you can program your Factory better than with Redpower (until Eloraam releases her Computertechnology). I hope you know the CC-Recipes, because i will not show them.


    Direwolf20 has made a Videotutorial for CC.


    The CC-Folder for programs you use in every Savegame/SMP-game is ".minecraft\mods\ComputerCraft\lua\rom\programs", the folder "Turtle" should contain only turtleprograms, and the Folder "Computer" should contain only Computerprograms. All the other Programs, which are for Turtle and Computer can be put directly in the "programs"-Folder.


    You can edit all these Files with Notepad or something similar even if they have no ending. And if they have an ending (if you downloaded it from here for example), then you should remove the ".txt" on the end of the File so you dont have to Type "Programname.txt" to run the program.


    If your program stucks then you can use "Ctrl + T" to terminate it (you must hold them for a moment). Holding "Ctrl + R" restarts the Computer.


    1.0 Disclamer for generic Commands
    "help" will display help for you.


    "apis" will list all the things with helpthemes (sometimes they are not very helpfull)


    "help 'Programname' " or "help 'API_Name' " will display the help for the Program/API.


    "programs" will list all the programs in this Computer.


    "edit 'Programname' " will edit the Program you have selected or will make a new one.
    when you are naming a File "startup" then the Program will be started together with the Computer.


    " 'Programname' " will start the Program, simple as that.


    "exit" shuts the Computer down until you rightclick it.


    "dj" if you have a Musicdisc in an adjacent Diskdrive then it will played.


    1.1 Setting Redstonesignals and other Basic commands
    Now you need your first non-Hello-World-Program, therefor you need to type "edit InsertYourProgramNameHere" in the Computer. You can also use Notepad if you find the Folder in your Savefile or the CC-Mainfolder (explained above where it is).
    _
    "rs.setOutput("left",true);" sets the Redstonsignal on the Left side of the Computer ON ("true").
    "rs.setOutput("left",false);" the "false" sets it OFF.
    _
    You can use the following other directions instead of "left":
    "right", "front", "back", "top" and "bottom".
    _
    "os.sleep(5);" stops the program for "5" Seconds (100 ticks).
    You can use this for Timers



    1.1.2 Syntax


    "while 'Condition' do
    'Insert Code Here'
    end"


    the Code between the "do" and the "end" will be processed as long as the condition is true.
    if you write "while true do" then it becomes an endless Loop, which is used for Timers.


    I know theres a for-Loop but i don't use it, because of it's diffrent behaviour in diffrent Programminglanguages
    _
    "if 'Condition' then
    'Insert Code Here'
    end"


    It will do the lines of code once if the condition is true.
    _
    "
    'Condition A' or 'Condition B' "
    will output "true" if ONE of the conditions is met.
    _
    "
    'Condition A' and 'Condition B' "
    will output "true" if BOTH of the conditions are met.
    _
    "not 'Condition' "
    will output "true" if the condition is NOT met.
    _
    "not ( 'Condition A' and 'Condition B' )"
    will output "true" if NOT BOTH conditions are met.


    1.1.3 Turtle your friend and helper
    Now we come to the mobile Computers with Miningabilitys (if crafted together with a Diamondpick).
    I've attached some good Turtleprograms i use myself so feel free to use and don't forget to delete the ".txt"-ending for easier use.


    What functions are available (copied from turtleAPI):


    turtle.forward()
    turtle.back()
    turtle.up()
    turtle.down()
    turtle.turnLeft()
    turtle.turnRight()
    turtle.select( slotNum ) // 1 - 9 are valid
    turtle.getItemCount( slotNum )
    turtle.getItemSpace( slotNum )
    turtle.dig()
    turtle.digUp()
    turtle.digDown()
    turtle.place()
    turtle.placeUp()
    turtle.placeDown()
    turtle.detect()
    turtle.detectUp()
    turtle.detectDown()
    turtle.compare()
    turtle.compareUp()
    turtle.compareDown()
    turtle.drop()
    turtle.drop( quantity )


    The most important ones are selfexplaining. The drop/place-commands are placing the stuff of the SELECTED Slot.


    1.2 The attached programs for your Turtle


    "chop" place in front of a 1x1 Tree and activate (The turtle must look at the trees everytime).
    "chop2" place in front of a 2x2 Tree and activate (left aligned frontside of the tree, maybe i will change that).
    "chop3" used to harvest the stem of a 3x3-RP-Rubbertree (place middly in front of it).
    "shaft" place 3 Cobbles in the first Slot and a few torches in the Last and it will mine a 3x1-Shaft for you.
    "Direwolf20_House" builds a larger Direwolf20-11x11-Standardhouse for you (the program will list everything what is used to build)



    I hope this explains the basics of CC, so you can build the Factory you maybe imagined.


    Edit: Forum failed at using ">" and "<" as normal letters (accidently thinks its HTML-Code).
    ">" and "<" should be used for smileys and such, so they would be less bugged like :Solar Panel: or :Personal Safe:


    Ask if you're not understand something, that's not a shame and my explainingskills are not the best (for people with social life).


    And never forget: Don't use Internetexplorer! :D

  • looks interesting except for one huge problem. This is the IC2 section of the forums, not one for Computer Craft.


    Stop doing my job dammit.


    Also Greg, you better start talking on HOW this relates to/works with IC2 if you want this sort of comments to lose any validity.

    • Official Post

    FenixR and other people who think CC doesn't really belongs to IC²:
    Computers are industrial, you can use them for Factorys and very precisely timed Nuclearreactors, which need an EXACT cooldowntime. Every Factory/Reactor-design is diffrent and i will post some Programs for Nuclearreactors and Factorys soon. But i need some testingtime for the Nuclearreactor before doing that.


    Edit: I'm posting it here, because nearby everyone uses IC² together with other mods, because IC² is the only Mod that makes automatisation really valuable.

  • The 'trolls' around here are getting out of hand, methinks... Every new post has people criticizing from the start. One person implies something is off-topic and away it goes to the Off-topic 'graveyard' with no consideration of how it /COULD/ apply.


    Do tutorials get moved to Off-Topic because it talks of how to use BC pipes with IC2 machines? I think a forum with a list of threads like:


    "[Tutorial] IC2 + ComputerCraft"
    "[Tutorial] IC2 + BuildCraft: Sorting and Processing"
    "[Tutorial] IC2 + " etc...


    ...would be a very handy resource. That said- this tut is still in need of serious work so it's a bit early to be jumping on it because it doesn't go into express detail of how it can work with IC2.


    FenixR - So you're saying that anything that isn't strictly vanilla IC2 has no place in the tutorial section? Also- You view sniping of ideas as your job? How classy.

    I know that you believe you understand what you think I said, but I'm not sure you realize that what you heard is not what I meant.

    • Official Post

    One person implies something is off-topic and away it goes to the Off-topic 'graveyard' with no consideration of how it /COULD/ apply.

    I've realised that the Thread is now here, i just thought it was deleted after i tried to add a simple Nuclearreactorcontrolprogram using CC, and not found my Thread.

  • Nice tutorial! Can't wait to see it done!
    If you add some functions and examples with IC2, I'm sure it'll be moved back!


    BTW, why don't we have an own tutorial section, instead of support and general discussion?


    One question: If I restart my server, will my systems (Computers and turtles) stop their current programs, restart or just shut down?

  • We do have a tutorial subforum, however this is ComputerCraft not IC2 so it belongs here.

    • Official Post

    BTW, why don't we have an own tutorial section, instead of support and general discussion?

    We have. Its a Subsection of the Industrial-Engineering-Subsection.

    One question: If I restart my server, will my systems (Computers and turtles) stop their current programs, restart or just shut down?

    It will just restart the whole device using the "startup"-File (only if it was ON before). I use that Function as Chunkloaddetector for my CASUC-Coolantsystem (see Maintutorial in my Signature).

  • "

    Quote


    Quoted from "Peter110"


    BTW, why don't we have an own tutorial section, instead of support and general discussion?

    We have. Its a Subsection of the Industrial-Engineering-Subsection. "



    If it's a subsection of a subsection, it's no wonder why I didn't find it! :D



    "

    Quote


    Quoted from "Peter110"


    One question: If I restart my server, will my systems (Computers and turtles) stop their current programs, restart or just shut down?


    It will just restart the whole device using the "startup"-File (only if it was ON before). I use that Function as Chunkloaddetector for my CASUC-Coolantsystem (see Maintutorial in my Signature)."



    So my wheat farm could actually mine a hole in the glass of my underwater base? :S



    EDIT: Frakkin' quotes!

    • Official Post

    So my wheat farm could actually mine a hole in the glass of my underwater base? :S

    Yes, things like that can happen. (only if its in the "startup"-File!)


    I would recommend you to use local Variables (Syntax: local x = 0) instead of normal Variables, because locals will get saved on restart.


    EDIT: Frakkin' quotes!

    Use "insert Quotation" instead of "Insert Quotes"

  • Two question:


    1. Is there a way for the turtle to know what block it detects? (My underwaterbase-roof)


    2. I want my turtle to mine the block infront of it everytime a redstone signal hits the top. What am doing wrong?


    if redstone.getInput("top", true) then
    turtle()dig


    EDIT: Yeah, my first day with Lua 8)


    EDIT: Or do I have to use while command?

    • Official Post

    1. Is there a way for the turtle to know what block it detects? (My underwaterbase-roof)

    No you cant detect the ID. Only if there is a Block or not, with turtle.detect()

    2. I want my turtle to mine the block infront of it everytime a redstone signal hits the top. What am doing wrong?


    if redstone.getInput("top", true) then
    turtle()dig

    Try this:


    if (rs.getInput("top")) then turtle.dig(); end

  • No you cant detect the ID. Only if there is a Block or not, with turtle.detect()

    Try this:


    if (rs.getInput("top")) then turtle.dig(); end



    It will only break one time if the redstone signal already is on. I wanted something I could hook up to a timer, wich break s the block in front everytime the signal hits.
    BTW, what does the ; mean behind the command?

    • Official Post

    It will only break one time if the redstone signal already is on. I wanted something I could hook up to a timer, wich break s the block in front everytime the signal hits.

    while(true) do if (rs.getInput("top")) then turtle.dig(); end end


    BTW, what does the ; mean behind the command?

    The ";" means that the Line ends. And the "end" means that the if-Condition/while-Loop ends.

  • while(true) do if (rs.getInput("top")) then turtle.dig(); end end

    The ";" means that the Line ends. And the "end" means that the if-Condition/while-Loop ends.


    Thanks for the ; tip!
    The turtle did only say

    Code
    bios:206: [string "mine"]:1:
    unfinished string


    I called the program mine.



    EDIT: Managed to write a good program with it's own timer. Thangs for all the help! :)

    • Official Post

    The turtle did only say

    Code
    bios:206: [string "mine"]:1:
    unfinished string

    You did hopefully the following:


    >open the GUI of the Turtle and type the following:
    edit mine
    while(true) do
    if (rs.getInput("top")) then
    turtle.dig();
    end
    end



    >Press "Ctrl" and then move to "Save" with the Cursors and then hit "Enter"
    >And now go out of the editmode and type:
    mine


    And now you have a Blockbreaker-Turtle