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

Some Basic Things To Learn About CC

Started by cheekycharlie101, 20 October 2012 - 03:26 PM
cheekycharlie101 #1
Posted 20 October 2012 - 05:26 PM
First of all, i would like to state that im still learning lua. i have much to learn and will update this as often as i can.
the first thing im going to show you is functions.
this guide is assuming you know how to create programs ete.

Functions

There are 2 types of functions. local and global. local ones will only be able to be called in a program. how ever if you are making an api then you will want to use global. global functions can be called on from any program as long as the function is in a file on that computer.
here is an example of a local function.


local function Test()  -- creates the function.
  print("Hey,  this is my new testing function that i have made")
end -- ends the function
Test() -- calls the function

this will make a local function that can only be called from that program.

also a note that you can add notes to code and make the computer ignore them when the program is run. so you can put mini tutorials in your code and they wont affect or break what your code does. here is an example of a global function with notes.


function hello() -- This is a note. this also creates a function
  print("Hello") --This text after the '--' will be ignored
end -- Ends a function
so basically anything if you put "–" at the end of a line it will be ignored when the code is run.
this is all im saying now but i will update with things like Infinite loops, small rednet tutorials and things like controling turtles.
thanks -Cheeky

if you have made a global function you can test if it works by doing the following.
type "lua" to go to the lua prompt. this is where you can test raw functions from api's and things you have made. now we made a global function earlier. lets try and run it from the lua prompt. type hello() in the lua prompt.
it should have returned "hello" because we told the function to print "hello" when its called. you can also call a function from a program by putting hello() or your function name ONLY after the function is created. there are these "()" for a reason. this is because you can pass arguements through it if you want.

here is a basic way of passing arguments through a function. in this example i will show some basic code that will print the word hi a number of times you tell it to.
here is the code:

write("Number: ") -- prints the the screen with a input opotion
a = io.read()-- this takes what was wrote into a varible to use in the function. il make a tutorial on io.read() later
local function printTest(a)
  for i = 1,a do -- makes a loop. note in for loops you can use vairbles as shown here.
   print("hi")-- Prints the word
   sleep(1)-- adds a 1 second interval.
  end -- ends the loop
end -- ends the function
printTest(a) -- calls the function

–varibles–


--Variables Help code of Jediknightkrazy.
local Variable = 1 --Local defines that we can only use this in our program, and "Variable" is our variable name. "1" is it's numeric value.
print(Variable) --Notice how we don't use ""? That's because we're printing the numeric value of a variable.
if Variable == 1 then --This line will only be executed if Variable equals 1. In this case, it does.
print("Variable is "..Variable.."!") --We can use multiple tricks to add variable values midway in our output.
end

–Explaining Read–

--[    Code by Jediknightkrazy.
--		    READ()
--]
--We will use the read() functions to ask the user for input. We will use our example of Sleep() as base.

local input = read() --Read() asks for user input, "input" is variable, you can call that whatever.
while true do
print(input) --I have explained this before, but we are printing the value of input, which is whatever the user wants.
sleep(0)
end

–Lesson 2–
infinite loops / while loops / for loops.
to start of with a basic infinite loop will use a while loop. this is the best way of making one. now to jump right in, il do an example of a for loop and an infinite loop.

a for loop

a basic for loop will repeat some strings of code for a set ammount of times. here is an example


local function basicLoop()
  for a = 1,10 do
   print("Hello World")
   sleep(5)
  end
end

this will make a loop that will print Hello World 10 times with a 5 second interval between each one. now if you put that in a program it would not do anything. this is because we put it in a function and the function has not been called. so lets do that then.


local function basicLoop()
  for a = 1,10 do
   print("Hello World")
   sleep(5)
  end
end
basicLoop()


there, now when the program is run it will start the loop.
now to explain how it works.


local function basicLoop() -- Creates The Function
  for a = 1,10 do -- creates a loop. a is a varible. this can be anything.  so for z = 1,10 do would also work as for the numbers. you will usaly keep the 1 the same. change the 10 to the number u want to repeat. so for a = 1,200 do will repeat 200 times
   print("Hello World") --Prints the text to the screen.
   sleep(5)-- adds 5 second intervals between the prints
  end -- Ends the for loop
end -- ends the function
another note is that i space out my code, and indent it. this is not necessary, just space it out to your liking.

–While loop–

first lets start of with an infinite loop example.


while true do
print("Hi")
sleep(1)
end
basic loop, note that the program will crash if no sleep is present though the timing is optional. the reason the while true do, makes an infinite loop is because it will return true by default. then it will keep repeating until returned false. and in this program it will never return false. so thats a basic infinite loop and while loop. you can replace the true with anything to make it fit your program. on lesson 3 i will show you basic rednet commands and a basic rednet listner to pick up incoming messages.
thanks again -Cheeky


EXTRA SPECIAL THANKS TO Jediknightkrazy For Helping Me With The Code !! =D
If you wanna help then pm me :)/>/>
Orwell #2
Posted 21 October 2012 - 04:32 AM
Are you going to add a part about arguments and return paths later? I think it's a must have for a tutorial on functions. Other than that, good job!
cheekycharlie101 #3
Posted 21 October 2012 - 11:35 AM
Are you going to add a part about arguments and return paths later? I think it's a must have for a tutorial on functions. Other than that, good job!
thanks and yes. im still kinda practising coding with functions and arguments. but as soon as i feel that what i can do is good enough i wil also share. i have other things planned as well like rednet tutorials controling turtles ete
aSdtIHN0dXBpZA== #4
Posted 22 October 2012 - 04:29 PM
Good guide on functions. If you want, I can help write out some code, I did program a basic API/Os/whatever you call it/Program that has editable commands and home. And, I have a very fast typing skill, so I can type out a good program in minutes (example: digging straight tunnel, placing torches, progress meter)
cheekycharlie101 #5
Posted 22 October 2012 - 09:34 PM
Good guide on functions. If you want, I can help write out some code, I did program a basic API/Os/whatever you call it/Program that has editable commands and home. And, I have a very fast typing skill, so I can type out a good program in minutes (example: digging straight tunnel, placing torches, progress meter)
would be great if you could help me out, its gonna take me ages to right out everything i know on here anyway :)/>/>
aSdtIHN0dXBpZA== #6
Posted 23 October 2012 - 04:04 PM
Good guide on functions. If you want, I can help write out some code, I did program a basic API/Os/whatever you call it/Program that has editable commands and home. And, I have a very fast typing skill, so I can type out a good program in minutes (example: digging straight tunnel, placing torches, progress meter)
would be great if you could help me out, its gonna take me ages to right out everything i know on here anyway ^_^/>/>

Sure, I can help with code. PM me with anything you need help with.
aSdtIHN0dXBpZA== #7
Posted 24 October 2012 - 04:41 PM
I'm done ;D