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

[LUA] I wonder if i can...

Started by jorockstar, 25 August 2012 - 12:32 PM
jorockstar #1
Posted 25 August 2012 - 02:32 PM
Hi everyone. I'm not so familiar with lua coding, but i've succeeded in doing a lock with the help of Youtube and now i'm doing a nuclear reactor launching program, but i wonder if i can do this:

For now there's a password at the beginning to launch the program, but i would like to add something so it would send a request to another computer that would confirm the launch (with rednet i guess), so can someone help me?

PS: i would like to send the codes already written, but i did it on a friend's server and he's still sleeping so i'm going to ask him it later and send it here and by the way one of my friends is programmer and gave me small lessons of Java and recognize a lot of word from Java in lua (I guess that programming in """""Java""""" in a game already coded in Java is normal xD)

PSS: I forgot something… i've done a countdown for the reactor (an hour) and i did it like this:

write("The nuclear reactor is on for 0:60")
sleep(60)
term.clear()
term.setCursorPos(1,1)
write("The nuclear reactor is on for 0:59"
[…]

i wonder if there's a better way to do this
BigSHinyToys #2
Posted 25 August 2012 - 03:49 PM
Asking a pro to write code for you is not the best way to get help. If you write some code then pros will help you get it running because it is good to see some effort has gone into it from the you before we help you.

There are already some reactor controll programs maybe you should have a look at them see how they work then have a crack at your own. If you have any problems with some code just post here in the pros section.

M1cr0man's Programs (Reactor Controller, Login & PM client & server with syncing)

Nuclear Reactor Control System

Master Control System (MCS) Ver. 1.5
Orwell #3
Posted 25 August 2012 - 03:53 PM
There indeed is a better way to do this : )

Read up on for loops :D/>/> Then you'd get this:


local minutes = 60
while minutes > 0 do
  write("The nuclear reactor is on for 0:" .. minutes)
  sleep(60)
  term.clear()
  term.setCursorPos(1,1)
  minutes = minutes - 1
end

This is just a rude example demonstrating a for loop. BigShinyToys is right, show us what you got allready and then can give tips from which you really learn. : )
For now, read up on for loops and maybe on modulary calculations for displaying time. (cause 0:60 doesn't exist :P/>/> )

And if you read up on rednet it will be clear to you how you can use it ; ).
ardera #4
Posted 25 August 2012 - 03:53 PM
I was already writing the code…

But here is only the clock code:


countdowntime=60
for n=countdowntime, 1, -1 do
  function actualize()
    term.setCursorPos(1, 1)
    term.clearLine()
    write("The nuclear reactor is on for "..math.floor(n/60)..":"..n)
  end
  sleep(60)
end
jorockstar #5
Posted 26 August 2012 - 03:22 PM
Thanks you all and for BigSHinyToys i was just asking if it was """possible""" not if someone can do it for me. I had doubts about it. For now i'm working on a doorlock with a backdoor option and by the way… i know that in Java there's a "or" (which is like that if i remember correctly "U"), so is there on in lua?

answer = read()

if answer == (yes) then
write("Can you tell me how?") ;P

i'll do my own research and i comeback if i find it before i get an answer


PS: As soon i'll save my codes on a computer so my friend can send it to me i'll post it here (I hate so much when i'm not hosting)
jorockstar #6
Posted 26 August 2012 - 03:44 PM
here's my actual code for my door lock… tried to find a "or", but it don't look like it exists, so i've been looking and i found an elseif, but still doesn't work so here's my code:

term.clear()
term.setCursorPos(1,1)
os.pullEvent = os.pullEventRaw

correctpass = "55b1xi"
BackdoorLog = "Admin"
BackdoorPass = "159263"

write("Password:")

pass = read()
key = read()

if pass == (BackdoorLog) then
write("Key:")

if key == (BackdoorPass) then
write("Backdoor opened")
redstone.setOutput("bottom",true)
sleep(5)
os.shutdown()

else
write("Wrong key")
sleep(2)
os.shutdown()

or
if pass == (correctpass) then
write("Access granted")
redstone.setOutput("bottom",true)
sleep(5)
os.shutdown()

else
write("Access denied")
sleep(2)
os.shutdown()
end

sorry i don't know if an attached file is fine for you
BigSHinyToys #7
Posted 26 August 2012 - 04:18 PM
you can use or in in lua but not the way you dida above

I rearranged your code a bit and removed the or

Spoiler

term.clear()
term.setCursorPos(1,1)
os.pullEvent = os.pullEventRaw
correctpass = "55b1xi"
BackdoorLog = "Admin"
BackdoorPass = "159263"
write("Password:")
pass = read()
write("Key:")
key = read()
if pass == correctpass then
	write("Access granted")
	redstone.setOutput("bottom",true)
	sleep(5)
	os.shutdown()
elseif  pass == BackdoorLog and key == BackdoorPass then
	write("Backdoor opened")
	redstone.setOutput("bottom",true)
	sleep(5)
	os.shutdown()  
else
	write("Access denied")
	sleep(2)
	os.shutdown()
end
jorockstar #8
Posted 26 August 2012 - 04:43 PM
Thank you a lot…. i have some progress to do
jorockstar #9
Posted 26 August 2012 - 04:51 PM
The code you gave me workd perfectly… the only thing is that even if i write the Password for opening the door the "Key:" is showing on the screen
BigSHinyToys #10
Posted 27 August 2012 - 06:41 AM
The code you gave me workd perfectly… the only thing is that even if i write the Password for opening the door the "Key:" is showing on the screen
My apologies I mis understood your programs function. Here is a fixed ver that only requests key after Admin is used.
Spoiler

term.clear()
term.setCursorPos(1,1)
os.pullEvent = os.pullEventRaw
correctpass = "55b1xi"
BackdoorLog = "Admin"
BackdoorPass = "159263"
write("Password:")
pass = read()
if pass == correctpass then
    write("Access granted")
    redstone.setOutput("bottom",true)
    sleep(5)
    os.shutdown()
elseif  pass == BackdoorLog then
    write("Key:")
    key = read()
    if key == BackdoorPass then
	    write("Backdoor opened")
	    redstone.setOutput("bottom",true)
	    sleep(5)
	    os.shutdown()
    else
	    write("Wrong key")
	    sleep(2)
	    os.shutdown()
    end
else
    write("Access denied")
    sleep(2)
    os.shutdown()
end