21 posts
Posted 04 October 2012 - 01:09 AM
I'm building a vault door that requires the user to enter the correct password to open the door, but I've run into a bit of a snag.
I have it set up such that if the password is correct, the terminal sends a signal to the server inside the vault which in turn opens the door.
Entrance Terminal –> Server
Interior Terminal –>
The server is coded so it will only accept a message from a particular computer ID. (The terminal outside the door)
local allowedComputerID = 36
The program runs perfectly, but the problem is that I can't get the server to accept two different ids; one for the interior and one for the exterior terminal, leading to the user's journey into the vault becoming a one-way trip.
How can I fix it?
print("Computer ID " , os.computerID())
redstone.setOutput("back" , true)
rednet.open("top")
repeat
local allowedComputerID = 33
local sEvent, param1, param2 = os.pullEvent()
if sEvent == "rednet_message" and param1 == allowedComputerID then
print (param2)
redstone.setOutput("back" , false)
sleep(4)
redstone.setOutput("back" , true)
end
until event == "char" and p1== "x"
1688 posts
Location
'MURICA
Posted 04 October 2012 - 01:14 AM
Just make another variable and check that one too.
print("Computer ID " , os.computerID())
redstone.setOutput("back" , true)
rednet.open("top")
repeat
local allowedComputerID = 33
local allowedComputerID2 = 34
local sEvent, param1, param2 = os.pullEvent()
if sEvent == "rednet_message" and (param1 == allowedComputerID or param1 == allowedComputerID2) then
print (param2)
redstone.setOutput("back" , false)
sleep(4)
redstone.setOutput("back" , true)
end
until event == "char" and p1== "x"
Of course while changing allowedComputerID2 to whatever your other computer ID is.
I'd also recommend shortening your variable names. :/
21 posts
Posted 04 October 2012 - 01:26 AM
Nice! It works perfectly now.
What I tried earlier was " local computer = 33 or 34 ". Is it possible to have a variable store two different numbers?
1688 posts
Location
'MURICA
Posted 04 October 2012 - 01:30 AM
Technically, yes. Tables.
-- defined around curly brackets with commas separating them
local varTable = {33,34}
-- accessed by the number they're in the table.
print(varTable[1])
--> 33
print(varTable[2])
--> 34
But if you mean for one variable to return true when compared to two different values, I'll have to think about that.
64 posts
Posted 04 October 2012 - 01:36 AM
local varTable = {33,34}
for i=1,# varTable do
if sEvent == "rednet_message" and (param1 = varTable
Try something like this. I cant test :(/>/>
21 posts
Posted 04 October 2012 - 01:39 AM
Technically, yes. Tables.
-- defined around curly brackets with commas separating them
local varTable = {33,34}
-- accessed by the number they're in the table.
print(varTable[1])
--> 33
print(varTable[2])
--> 34
But if you mean for one variable to return true when compared to two different values, I'll have to think about that.
I was originally trying to apply it to an if statement.
if computer == 33 or 34 then
-- do this code
64 posts
Posted 04 October 2012 - 01:47 AM
This is pretty much your code, but attempts to loop through all variables in your table, however many. but I cant test and never done so no idea if implemented right..
EDIT - slight amendment to spacing
print("Computer ID " , os.computerID())
redstone.setOutput("back" , true)
rednet.open("top")
repeat
local allowedComputerIDs = {33,34}
local sEvent, param1, param2 = os.pullEvent()
for i=1,# varTable do
if sEvent == "rednet_message" and param1 == allowedComputerIDs {i} then
print (param2)
redstone.setOutput("back" , false)
sleep(4)
redstone.setOutput("back" , true)
end
end
until event == "char" and p1== "x"
1688 posts
Location
'MURICA
Posted 04 October 2012 - 02:03 AM
If you're looking for efficiency, I think I know of a good way you can do this. You can set table indexes and see if those indexes exist without having to loop through it.
print("Computer ID " , os.computerID())
redstone.setOutput("back" , true)
rednet.open("top")
repeat
local allowedIDs = { [33] = true, [34] = true }
local sEvent, param1, param2 = os.pullEvent()
if sEvent == "rednet_message" and allowedIDs[param1] then
print (param2)
redstone.setOutput("back" , false)
sleep(4)
redstone.setOutput("back" , true)
end
until event == "char" and p1== "x"
64 posts
Posted 04 October 2012 - 02:05 AM
Nice one! I like the idea
21 posts
Posted 04 October 2012 - 02:06 AM
This is pretty much your code, but attempts to loop through all variables in your table, however many. but I cant test and never done so no idea if implemented right..
EDIT - slight amendment to spacing
print("Computer ID " , os.computerID())
redstone.setOutput("back" , true)
rednet.open("top")
repeat
local allowedComputerIDs = {33,34}
local sEvent, param1, param2 = os.pullEvent()
for i=1,# varTable do
if sEvent == "rednet_message" and param1 == allowedComputerIDs {i} then
print (param2)
redstone.setOutput("back" , false)
sleep(4)
redstone.setOutput("back" , true)
end
end
until event == "char" and p1== "x"
I put your code into the server and it runs but when i enter the password I get a "startup:10: attempt to call table" error. The error occurs when the signal is received.
1688 posts
Location
'MURICA
Posted 04 October 2012 - 02:09 AM
That's because in this line:
if sEvent == "rednet_message" and param1 == allowedComputerIDs ###{i}### then
I've put hash symbols around the error. These should be square brackets, not curly ones.
21 posts
Posted 04 October 2012 - 02:13 AM
Ah, that's fixed it. So both types of brackets are similar to the = , == operator because they do two different things?
64 posts
Posted 04 October 2012 - 02:17 AM
In case it dont work, might need to change varTable to allowedComputerIDs in the loop
21 posts
Posted 04 October 2012 - 02:24 AM
My last question is, what line of code do i need to add to mask the password while the user is entering it?
Password : test
Password : ****
1688 posts
Location
'MURICA
Posted 04 October 2012 - 02:36 AM
Change read() to read("*")
read '*' works too.
21 posts
Posted 04 October 2012 - 02:48 AM
Thanks King and Lucas. Your help is much appreciated.