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

need help understanding code

Started by Karaktar, 08 October 2012 - 11:41 PM
Karaktar #1
Posted 09 October 2012 - 01:41 AM
ok so heres the thing i have this awesome code mostly done by cranium and he was a huge help except i really want to better understnad what each thing does and how i could implement it in other code operations i have in mind also since i am the only guy atm on the server who can code at all i am of course getting requests and well im still learning and quite often find my self hitting my head againt a wall lol

so heres what i would like from whoever would care to take it on in the code below if anyone could please add comments explaining what each line does and how it is referenced i mean there are somethings which i of course already understnad like rs.set and rs.get and what not but what i need to understand most like the tables and the function calls those i really need help understanding

also if anyone knows of away aside from pastebin (which requires http get and has been decided is too much risk for the server) to copy paste direct to the in game editor(which sucks) that would be awesome thanks in advance


local sides = {"left", "right", "back", "top"}
local function lights(delay)
if not delay then
for i = 1,#sides do
rs.setOutput(sides[i], true)
end
else
local rand = math.random(1,4)
rs.setOutput(sides[rand],true)
sleep(delay)
rs.setOutput(sides[rand],false)
sleep(delay)
end
end
local function display(side,delay1,delay2)
rs.setOutput(side,true)
sleep(delay1)
rs.setOutput(side,false)
sleep(delay2)
end
lights()
sleep(2)
local event, p1 = os.pullEvent()
if event == "redstone" then
while rs.getInput("front") do changed to continue loop while there is a redstone signal on the front side
local x = math.random(1,4)
display(sides[x],.5,.5)
end
end
end
i appoligize when i pasted it came out like this and then i tried to add it to bbcode that went unreadable so sorry

oh and please note this code doesnt not work as is i edited it after so dont worry about that it works now just not like this i just didnt have a pastble copy of the new code to add here
ChaddJackson12 #2
Posted 09 October 2012 - 02:12 AM
Well for one you can copy the code world/computer/computer#
In the server folder with a file name of whatever you want. Other than that you are either to type it in or use the pastebin way
Karaktar #3
Posted 09 October 2012 - 02:15 AM
yeah im talking about smp which unless you have access to the server files is not possible but thanks anyway that was a minor thing the main thing i need is to understand the code so i can get better at this
ChaddJackson12 #4
Posted 09 October 2012 - 02:25 AM
yeah im talking about smp which unless you have access to the server files is not possible but thanks anyway that was a minor thing the main thing i need is to understand the code so i can get better at this
I would help on understanding but what do you need help understanding? Everything? Or something specific?
ChaddJackson12 #5
Posted 09 October 2012 - 02:43 AM
Here is a couple things that should help you:
A "function" is a section of code called in another section of code. Here is an example:

function myfunction() -- can be any name. With () after it. 
   print("Hello!")
end

term.clear() -- Clears the screen
term.setCursorPos(1,1) -- Sets the blinking cursor to the top left of the screen. 
print("I will say hello!")
os.sleep(1) -- pause for 1 second. 
myfunction() -- Call the function. 

To call a function, just put its name with the ()s after it. But to prevent errors I recommend putting the actual function above the part that calls it as it seems that Lua does not look below the part that calls it. (if that makes any sense :D/>/>)

Also. You can make things equal to other things. This is useful for shortening your work and can save you for complications.

How you do this is put this

local greeting = "Hello, my name is Bob and I like Cheese!"

print(greeting) - prints the above code that is in quotes. 
Now that only calls the code once. So it isn't very useful, but let's say you have to call it many times. It could cause complications and would be a pain to change the same text many times. So define a variable (the greeting is a variable) and only change it once.

Now to call a variable; don't put quotes around it. Like with print() or it will print the actual text.

I hope I helped! It wasn't much. But is a start nonetheless!
ChaddJackson12 #6
Posted 09 October 2012 - 02:46 AM
Oh. I'd also recommend going and checking out the CC Wiki's API's as they are probably what you are looking for the most. Good luck.
Luanub #7
Posted 09 October 2012 - 06:55 AM
@ChaddJackson12 the edit post feature on the forums works well..
ChaddJackson12 #8
Posted 09 October 2012 - 02:12 PM
@ChaddJackson12 the edit post feature on the forums works well..
:D/>/> gotcha..