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

Computercraft turorial series

Started by Hackingroelz, 11 March 2012 - 01:16 PM
Hackingroelz #1
Posted 11 March 2012 - 02:16 PM
I started a little series of Computercraft tutorials.

Tutotrial 1:
SpoilerThe first thing we are going to program is a simple door lock program. The most people that create this program haven’t programmed it themselves or they don’t understand it, so I will try to let you understand the code.

The first thing you should know is the commands for the terminal. You can see these commands by typing “help term”. This gives you list of commands you can use. In the list you will see “term.write()”. This will write some text on the screen. Something you should know is that you can’t type “term.write(Hello World!)”, you must type “term.write(“Hello World!”)” in order for it to work. Now look at some other functions in the list. Try to create a program with it. Here is an example of what you could make

term.clear()
term.write("Hello")
term.clear()
term.write("Who are you?")
term.clear()

When running this program you will notice that you don’t see any text. This is because the text disappears just after it appeared. To fix this you need the function “sleep(time)”.

term.clear()
term.write("Hello")
sleep(2)
term.clear()
term.write("Who are you?")
sleep(2)
term.clear()

Now you will notice something else. The text “Who are you?” appears at the wrong place! You can fix this by using the function “term.setCursorPos(x, y)”.

term.clear()
term.setCursorPos(1, 1)
term.write("Hello")
sleep(2)
term.clear()
term.setCursorPos(1, 1)
term.write("Who are you?")
sleep(2)
term.clear()
term.setCursorPos(1, 1)

Now you can create something cooler, by getting the input of the user. You can do this with the function “reatt()”. You can use this function by typing something like “input = read()” this puts what read returns into a variable called “input”. We’ll go over variables in another tutorial. Something

you should know about this function is that it will sleep until the user has entered some text. This is an example of how you can use “read()”

term.clear()term.setCursorPos(1, 1)
term.write("hello")
sleep(2)
term.clear()
term.setCursorPos(1, 1)
term.write("Who are you?")
sleep(2)
term.clear()
term.setCursorPos(1, 1)
input = read()
term.clear()
term.setCursorPos(1, 1)
term.write("Hello ")
term.write(input)
term.write("!")
sleep(5)
term.clear()
term.setCursorPos(1, 1)

Now type “help redstone”. You will see a list of functions. To make the computer/turtle output Redstone you need to use “rs.setOutput(“direction”, boolean)”. Now we can make a program that opens a door if you say your name:

term.clear()term.setCursorPos(1, 1)
term.write("hello")
sleep(2)
term.clear()
term.setCursorPos(1, 1)
term.write("Who are you?")
sleep(2)
term.clear()
term.setCursorPos(1, 1)
input = read()
term.clear()
term.setCursorPos(1, 1)
term.write("Hello ")
term.write(input)
term.write("!")
rs.setOutput("back", true)
sleep(5)
rs.setOutput("back", false)
term.clear()
term.setCursorPos(1, 1)
Advert #2
Posted 11 March 2012 - 04:01 PM
I really don't see why you can't post them here.
Polyryph #3
Posted 20 March 2012 - 05:30 AM
Thanks for the tutorial, I found it very helpful..

Though i'm new to this mod, i've spent a lot of time coding Lua before, but of course not in this format and i'm unsure if the format is the same.

I do have a question, as this 'lock would allow anyone in, even if they just press enter immediately, is there a way to make it somewhat more like a lock? In the sense that, if they get their name wrong they can't gain access.
For eg; pass = "*name*" or pass = "*name* , *name* , *name* .. etc "
Somewhere within the code?

I guess what i'm asking ultimately is, is that possible and how would I write in multiple 'passwords' for multiple users.

Essentially, i'm not really locking anyone out.. But it would be nice for the users to be forced to actually type their name as opposed to not and still gaining access to the location.

Once again, thanks for the better understanding of the computercraft lua format and your time =)

Kind regards.
Advert #4
Posted 20 March 2012 - 11:02 AM
Thanks for the tutorial, I found it very helpful..

Though i'm new to this mod, i've spent a lot of time coding Lua before, but of course not in this format and i'm unsure if the format is the same.

I do have a question, as this 'lock would allow anyone in, even if they just press enter immediately, is there a way to make it somewhat more like a lock? In the sense that, if they get their name wrong they can't gain access.
For eg; pass = "*name*" or pass = "*name* , *name* , *name* .. etc "
Somewhere within the code?

I guess what i'm asking ultimately is, is that possible and how would I write in multiple 'passwords' for multiple users.

Essentially, i'm not really locking anyone out.. But it would be nice for the users to be forced to actually type their name as opposed to not and still gaining access to the location.

Once again, thanks for the better understanding of the computercraft lua format and your time =)

Kind regards.

Hi Polyryph, you should post your questions in the Ask A Pro forum, as more people will see your post, and you won't derail other people's threads.
Polyryph #5
Posted 21 March 2012 - 03:19 AM
Thanks for the tutorial, I found it very helpful..

Though i'm new to this mod, i've spent a lot of time coding Lua before, but of course not in this format and i'm unsure if the format is the same.

I do have a question, as this 'lock would allow anyone in, even if they just press enter immediately, is there a way to make it somewhat more like a lock? In the sense that, if they get their name wrong they can't gain access.
For eg; pass = "*name*" or pass = "*name* , *name* , *name* .. etc "
Somewhere within the code?

I guess what i'm asking ultimately is, is that possible and how would I write in multiple 'passwords' for multiple users.

Essentially, i'm not really locking anyone out.. But it would be nice for the users to be forced to actually type their name as opposed to not and still gaining access to the location.

Once again, thanks for the better understanding of the computercraft lua format and your time =)

Kind regards.

Hi Polyryph, you should post your questions in the Ask A Pro forum, as more people will see your post, and you won't derail other people's threads.

Duly noted. Thank you =)
iZINC #6
Posted 03 April 2012 - 07:28 PM
would it possible for a video series at some point?
Noodle #7
Posted 03 April 2012 - 10:39 PM
Videos would be nice.
Could make an in-game tutorial.
Then again, people can read the one already made.
EmTeaKay #8
Posted 04 April 2012 - 07:28 PM
Don't for get to add this to the top and bottom. Makes it so you can't Ctrl+T out.
local oldPull = os.pullEvent;
os.pullEvent = os.pullEventRaw;
[Code Here]
os.pullEvent = oldPull;
Aerik #9
Posted 06 April 2012 - 07:39 PM
Very nice and helpful! I would definitely love to see further parts being posted in this thread :)/>/>
Teraminer #10
Posted 19 April 2012 - 06:36 PM
Don't for get to add this to the top and bottom. Makes it so you can't Ctrl+T out.
local oldPull = os.pullEvent;
os.pullEvent = os.pullEventRaw;
[Code Here]
os.pullEvent = oldPull;
If you put at the top

os.pullEvent = os.pullEvent
Would it be wrong?
Wolvan #11
Posted 19 April 2012 - 08:49 PM
Don't for get to add this to the top and bottom. Makes it so you can't Ctrl+T out.
local oldPull = os.pullEvent;
os.pullEvent = os.pullEventRaw;
[Code Here]
os.pullEvent = oldPull;
If you put at the top

os.pullEvent = os.pullEvent
Would it be wrong?
Nope it is just that you would overwrite a global variable that will stay even if the program ends. That means that it will be in all other programs. You wouldn't be possible to CTRL+T them. oldPull just resets the os.pullEvent again and makes it possible to terminate.


@Polyryph
yep just add
if input == "yourPass" then

else
print("You are not allowed to enter! Leave!")
sleep(3)
os.shutdown()
end
Teraminer #12
Posted 19 April 2012 - 08:53 PM
and what is that ; in the end of os.pullEvent/Raw
Wolvan #13
Posted 20 April 2012 - 11:49 AM
Ehh yeah. Don't really know why it is there. It doesn't really belong there
djblocksaway #14
Posted 20 April 2012 - 02:36 PM
this tutorial is good for noobs and people that are new to computercraft

good job :)/>/>
cant_delete_account #15
Posted 20 April 2012 - 04:55 PM
and what is that ; in the end of os.pullEvent/Raw
I think some people get it confused with Java. xD
EmTeaKay #16
Posted 20 April 2012 - 06:55 PM
and what is that ; in the end of os.pullEvent/Raw
That's how I found it. I assumed it was the only way to do it.