Jump to content

ComputerCraft Mod - Basics


XMedders
 Share

Recommended Posts

Hello All! :)

 

I've scoured this tutorials forum for ComputerCraft tutorials, to see if anyone's done one. I think I saw only one. So, with that said, today I will be doing a tutorial on ComputerCraft. ComputerCraft is a mod that requires creativity. We all have and show this in different ways. With this being said, if you can't grasp the whole ComputerCraft topic the first time around, don't worry! I'm pretty sure first time, it takes everyone a little bit to get used to the whole thing. This tutorial will have screenies of the whole process :))

 

ComputerCraft Computer Fundamentals

First, there are two types of computers in versions pre 1.7 (We're playing Tekkit main, so there are no command computers in 1.6.4.) These are Regular and Advanced Computers. The only real difference between the two is that Advanced computers can draw text and backgrounds in color, and recieve the events mouse_click, and mouse_drag. However, these aren't important right now x). If you can afford an advanced computer, you can use it, but if you can't a regular computer will do just fine for what we will be doing in this tutorial. (img)

 

When you have your computer, just plop it down anywhere in the world (preferably in a protected spot), and it will boot right into the shell. (img)

 

To get an idea of how programs run, type in

hello

this command will write out the words 'Hello World!'. When this program has finished printing it out, you will notice that you can type again. This is because the function that prints the words is still running, so you can't type while it is writing out. (img)

 

The language that the computers use is called lua. If I'm not mistaken, this version of ComputerCraft uses lua version 5.1, maybe 5.2. So, if you want to use a function from the lua language, check out this page! However, let's not get ahead of ourselves. In this post, I will go over some basic lua snippets, so that you know some of the basic things that you can do with computercraft.

 

Lua Fundamentals

Variables: There are two basic variables in lua. These are Strings and Numbers. Think of strings as Words, and Numbers as.. Well.. Numbers. You can't add strings and numbers, because in the alphabet, you can't do 5+v can you? 

 

Strings are like this:

stringname = "Hello world!"

Numbers are like this:

numname = 5

If you try to print them, they will return what is on the right side of the equals sign, because when you set a variable, that variable is equal to what you set it to.

print(stringname) -- will return Hello world!
print(numname) -- will return 5

If you try to add a string and a number, you will get an error like

attempt to perform arithmetic on string

However if you add two numbers, it will return like so:

num1 = 5
num2 = 3
print(num1+num2) -- will return 8, as 5+3 is 8

If you want to add a number to a string, you would do it with concatenation, or putting together of variables. you concatenate with the operator '..'

string = "Number of Cookies in the jar: "
number = 5
print(string..number) -- will return Number of Cookies in the jar: 5

If you want to make a number a string, or a string a number, you would do this like so.

string = "5"
number = 7
print(tonumber(string)) -- makes the 5 operable by arithmetic (math)
print(tostring(number)) -- makes the 7 unoperable by arithmetic (math)

If you want to get the user's input via them typing, you have to use the function

input = read() -- variable input can be whatever you want to name it, stores as a string.
print(input) -- will print what the user types

Harder Functions

The best basic function of lua that I know is the if statement. Without this, you wouldn't be able to ask questions based on what the computer is experiencing, and therefore wouldn't have much of a querying system at all. Think of this code as regular english, because it is regular english.

name = read() -- get the user's input

if name == "fisher" then
  print('OMG GUYS ITS THAT COOL GUY WE KNOW NAMED XMEDDERS OMGOMGOMG')
else
  print('who dat is?')
end

There are also elseifs. These are like

if name == "fisher" then
  print('OMG GUYS ITS THAT COOL GUY WE KNOW NAMED XMEDDERS OMGOMGOMG')
elseif name == "slit" or name == "rmtworks" then
  print('omg its '..name..' a staff member who is actually tolerant of me lol')
else
  print('who dat is?')
end

When you want to do things with the shell (terminal window), you must interface it using the term api. This api has lots of useful functions.

term.clear() -- will clear the screen, but won't put the cursor to the top left (1,1)
term.setCursorPos(x,y) -- sets the position of the cursor on the screen (where you will print or write next)
term.write("sometexthere") -- will write text on the screen, but won't go to the next line after.
term.setBackgroundColor(color) -- sets the background color, but for it to take effect, term.clear() must be called.
term.setTextColor(color) -- sets the color of the text that you write with.

Math is an api that you might not use alot while starting out as a beginner, but it's worth mentioning x). I have only included to functions, because these are the only two I'd ever imagine a beginner would need.

math.random(lownumber,highnumber) -- returns a random number between the two specified ones.
math.sqrt(number) -- returns the square root of a number (can't be a string.)

Writing your own Program!

To write your own program, go to your computer and type in

edit <anynamehere>

the anynamehere part is the name of your program, and caps matters, I would just keep it lowercase.

another thing I should mention is that if you edit the file startup, the code in that file will happen when the computer is clicked to start or rebooted.

 

From Here, it is just a regular text editor. I will include some sample code out of the basics that we just talked about down below. To exit this editor, you must hit control, then right, then right arrow, then enter. You have successfully viewed your first file in the editor :P

 

For our basic program, we will create a locking door mechanism customized to your needs :)

To set this up, you will need to place a door down beside the computer that you are using, like so. (img)

 

Next, click on the computer, and type this in.

edit startup

You will be taken to the file, and from there, enter in this code. All lines have an explination. You don't need to include what's after the -- or the --. e.g. you don't have to put

print("hi") -- this prints hi

just

print("hi")

So, with that said, this is the code for the door!

password = "ilovecookies" -- password to get in
time = 3 -- time in seconds to get in the door
side = "left" -- usable sides are top, bottom, left, right, front, and back
while true do -- loop that runs forever
  rs.setOutput(side,false) -- close door
  print("What is the password to get in?") -- ask question
  input = read("*") -- get user's answer
  if input == password then -- you got the pass right
    rs.setOutput(side,true) --open door
    print("You got the password right!") -- congrats
  else -- if wrong
    rs.setOutput(side,false) -- close the door
    print("Sorry, but that's incorrect!") -- nope lol
  end -- end the if statement
  sleep(time) -- wait for 'time'
end -- end the forever loop.

and if you want to disable termination of your door lock, at line 1, put

os.pullEvent = os.pullEventRaw

to download this code, click here

or enter in your computer's shell: pastebin get XcmCyFEZ startup

 

Thanks for viewing this. This took alot of effort on my part (if you count moving your fingers for an hour), or if you like this, please drop a like, and if you have any questions/suggestions/comments/snide-remarks, please leave them down in the comments section and I'd be happy to address them x)

 

Thanks-a-million,

XMedders

Link to comment
Share on other sites

  • brunyman changed the title to ComputerCraft Mod - Basics

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Guidelines.