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

From noob to pro: Learn CC's luaj

Started by EveryOS, 10 November 2016 - 01:59 AM
EveryOS #1
Posted 10 November 2016 - 02:59 AM
Here is a guide for new programmers:
SpoilerThe very first thing you should learn is how to make an output. Most all programs use outputs, as they are very importantOne way to make an output is the 'print' function.
lua--'lua' opens the'try it' [lua] shell--'print' is a function--We will learn more about this later--For now, just write functions as follows--func 'sArg'print 'Hello, World!'--The '' represents the start and end of a line of text, or a string--Actually, you should use "", not ''exit ''
You should see something like:
Hello, World!2
The '2' is just a line count. It is not very important to us
SpoilerTry making the text 'ABC' appear
Spoilerprint 'ABC'
There are two other output functions: 'write' and 'term.write'Write will do the same as print, but won't add an extra line (print does that)term.write is like write, but unlike write and print, it is not auto-wrap (that's where text can't go offscreen, autwrap makes new lines when needed)
SpoilerBefore I continue, you must learn about data types. A data type determines what a value (such as 'Hello, World!') can do.Data types are as followed:Strings: the 'quoted thingies', these can be concentaited.Numbers (123) and Floats (1.2): these can be multiplied, divided, and so on. Remember PEMDAS?Tables (Arrays?): these store more data values!nil: Nothing, the absence of a valieFunctions: things like print and write.Booleans: true or false?
SpoilerThere are more advanced outputs:
print'1×2'--1×2--2print '1' × '2'--error: attempt to multiply two stringsprint '1' .. '2'--12--2
print 1 .. 2--error: attempt to concentate number and numberprint 1+2--3--2print {}--error: cannot print tableprint textutils.serialize({'hi', 'hi2'})--{--  hi--  hi2--}--5print 'hi\nhi'--hi--hi--3print 'hi&\\nhi'--hi\nhi--2print print--error: attempt to print function
SpoilerThere is a string API for thisYou can do many things in a string:
'\\' = '\''\' = error'\n' = newline'\t' = tab'stringA'..'stringB'= 'stringAstringB'tonumber '1' = 1tonumber 'k' = nil
SpoilerTry making various string arangments
SpoilerThese are true or false
SpoilerIt's easy, 5+5=10. There is also a math api for this. You cannot concentate numbers, unless you do this:
tonumber(tostring(1)..tostring(2))
SpoilerThere is a table API for thisA table stores mutiple vales.
t = {['k']=5,'hi'}t['x'] = 'hi'print t.xprint t[1]
SpoilerA function is a piece of code that is stored for repeated use:
function writeH(h)write h..'h'endwriteH 'haw'write 'k', '!'function secArg(...)  local args = {...}  write args[2]endsecArg(1,'gh')
SpoilerTry making and running a function
SpoilerTher is such a thing as scopes
var --Access anywhere in programlocal var --only access later in program_G['var']--Access anywhere, any fileenv['var'] --Goes to local env
SpoilerAnother useful thing is user input
edit loginargs = {...}print 'Your username is '..args1..' and your password is '..read('*')
Save and close the editor
login usr--Pss [enter]--See what happens
SpoilerTry interacting with the user
SpoilerIf yourr code takes a while, it may spit out a nasty error. Prevent this by inserting
coroutine.yield()
every here and there
SpoilerA loop is a repeating portion of codeThere are different types of loops:
while read() != 'end' do  os.queueEvent('')  coroutine.yield()endfor i=0, 5 do  os.queueEvent('')  os.pullEvent()  print iendfor k,v in pairs({'hi', ['x'] = 0})  print 'Value '..tostring(k)..' is '..v
[/namedspoiler loops]
SpoilerYou mat have noticed '==' or '!=' in my code. These are logic operators
== is =!= is not =not is opposite= is greater than/equal to< and > are normal
SpoilerAn if statment evaluates whether a boolean is true or not, and performs an action based off of that.
if boolean do--action 'true'elseif 'otr boolean'--action for otr booleanelse--False actionend
SpoilerIf an undefined variable or 'local' is used in a function or loop, then that variable is local to the loop. A workaround for this is to either use the global scope or predefine the variable as
Edited on 10 November 2016 - 03:51 PM
Lyqyd #2
Posted 10 November 2016 - 08:53 PM
This tutorial does not meet the minimum quality requirements. Please ensure that tutorial posts contain accurate information and are laid out in a manner that makes them easy to read and understand.