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

Locking my redstone singal when I log out and log in?

Started by Kingofawesome13, 03 June 2014 - 01:05 AM
Kingofawesome13 #1
Posted 03 June 2014 - 03:05 AM
Title: Locking my redstone singal when I log out and log in?

I have a door lock program on 2 computers, one on the inside of the reinforced stone door and another on the outside. They are connected by some red alloy wire to the door pistons.

The problem is when I log out of my singleplayer world, the redstone signal resets so I have to tear down the walls every time I log in to turn the redstone singal on. Is there a way to have the singal locked everytime I login/logout?

Thanks.
CCJJSax #2
Posted 03 June 2014 - 03:30 AM
You can save it's current state to a file when it changes, then always change it when the computer restarts based on what is in the file.

Spoileror you could put a redstone torch on the back if you expect to always need the rs signal to be on when you log on.
edit: the spoiler idea is stupid. Use bomb blokes. I was basing that idea off the beta CC that didn't startup when the chunk was loaded.
Edited on 03 June 2014 - 01:35 AM
Bomb Bloke #3
Posted 03 June 2014 - 03:32 AM
Well, you can certainly program the computers to set their redstone outputs to a certain state every time they boot, if that's what you're asking. Are you familiar with startup scripts?

It's possible you might need to be a bit more specific as to what you're already tried and where exactly you're getting stuck.
Whitecatblack #4
Posted 03 June 2014 - 03:43 AM
This thread my be helpful.

Whitecatblack
Kingofawesome13 #5
Posted 03 June 2014 - 09:36 PM
Well, you can certainly program the computers to set their redstone outputs to a certain state every time they boot, if that's what you're asking. Are you familiar with startup scripts?

It's possible you might need to be a bit more specific as to what you're already tried and where exactly you're getting stuck.

Unfortunatly, nto that familiar with startup scripts. I'll look into it.
Kingofawesome13 #6
Posted 03 June 2014 - 10:25 PM
Title: New system for a piston doorway?

The door way I made for my somewhat Fallout inspired Vault is stupid. Why you may ask? Because whenever I login to my world the redstone signal in the doorway contraption is reset so I have to tear down the wall to turn it on.

Imgur explanation

Is there a way I can make it easier for me? Thanks.
Whitecatblack #7
Posted 03 June 2014 - 10:33 PM
As I think was stated in the previous thread you posted, you would have to save the redstone output of the computers in files, then load those files upon startup. Please don't post multiple topics of the same thing, even if your question isn't answered in an acceptable form for you. Multiple responses were given to your problem ( I even posted a very helpful link), so there is no reason to start a new thread.

Whitecatblack
Kingofawesome13 #8
Posted 03 June 2014 - 10:52 PM
As I think was stated in the previous thread you posted, you would have to save the redstone output of the computers in files, then load those files upon startup. Please don't post multiple topics of the same thing, even if your question isn't answered in an acceptable form for you. Multiple responses were given to your problem ( I even posted a very helpful link), so there is no reason to start a new thread.

Whitecatblack

How do I delete the previous topic?

And how would I save it into the file?
Edited on 03 June 2014 - 08:54 PM
Whitecatblack #9
Posted 03 June 2014 - 11:00 PM
I posted I link that explains that very well on the last thread. Please, please read it. And you would have to contact a mod or admin (Lyqyd will probably take care of it if you ask him to nicely)

Whitecatblack
Kingofawesome13 #10
Posted 03 June 2014 - 11:47 PM
I posted I link that explains that very well on the last thread. Please, please read it. And you would have to contact a mod or admin (Lyqyd will probably take care of it if you ask him to nicely)

Whitecatblack

Ah, thank you so much.
Lyqyd #11
Posted 04 June 2014 - 12:47 AM
Threads merged. Please stick to one topic for all questions about a given problem.
Kingofawesome13 #12
Posted 04 June 2014 - 12:57 AM
Threads merged. Please stick to one topic for all questions about a given problem.

Thank you.
Whitecatblack #13
Posted 04 June 2014 - 12:57 AM
Whitecatblack said:
Lyqyd will probably take care of it if you ask him to nicely
Lyqyd said:
Threads merged.
And you didn't even have to ask for it, King. Such a nice admin.

Whitecatblack
Kingofawesome13 #14
Posted 04 June 2014 - 01:10 AM
As I think was stated in the previous thread you posted, you would have to save the redstone output of the computers in files, then load those files upon startup. Please don't post multiple topics of the same thing, even if your question isn't answered in an acceptable form for you. Multiple responses were given to your problem ( I even posted a very helpful link), so there is no reason to start a new thread.

Whitecatblack

I just read it and I was confused how I would save the redstone signal like I would a variable?(Sorry for acting like an idiot, I haven't done code in a really long time, finished Computer Science 1(Visual Basic) in early January.)

Whitecatblack said:
Lyqyd will probably take care of it if you ask him to nicely
Lyqyd said:
Threads merged.
And you didn't even have to ask for it, King. Such a nice admin.

Whitecatblack

I messaged him.
Whitecatblack #15
Posted 04 June 2014 - 01:36 AM
Kingofawesome13 said:
I just read it and I was confused how I would save the redstone signal like I would a variable?(Sorry for acting like an idiot, I haven't done code in a really long time, finished Computer Science 1(Visual Basic) in early January.)
Well, if you wanted to save it like a variable, it is possible, just not directly. You would have a variable like this in your main program:

randomVariable = 0
And when conditions were true for the door to open, you would set the variable equal to 1 instead. Then you would save that variable, like instructed in the thread I linked you, and you would be fine. You would have to call for that variable again, though, at the beginning of your startup code, maybe something like:

if randomVariable == 0 then
  CloseDoor()
elseif randomVariable == 1 then
  OpenDoor()
end
Or something like that.

Whitecatblack
Kingofawesome13 #16
Posted 05 June 2014 - 08:28 PM
Kingofawesome13 said:
I just read it and I was confused how I would save the redstone signal like I would a variable?(Sorry for acting like an idiot, I haven't done code in a really long time, finished Computer Science 1(Visual Basic) in early January.)
Well, if you wanted to save it like a variable, it is possible, just not directly. You would have a variable like this in your main program:

randomVariable = 0
And when conditions were true for the door to open, you would set the variable equal to 1 instead. Then you would save that variable, like instructed in the thread I linked you, and you would be fine. You would have to call for that variable again, though, at the beginning of your startup code, maybe something like:

if randomVariable == 0 then
  CloseDoor()
elseif randomVariable == 1 then
  OpenDoor()
end
Or something like that.

Whitecatblack

Here is my code, I bolded my attempted variable saving code(I know it's terrible, it's a new subject to me). Could you tell me what I did wrong?

(I did the dashes to represent spaces, I couldn't do it in a code window because that would not support bold font, and the quotes wouldn't support the spaces. Each dash represents one tab key press.)


local doorVariable = 0

term.clear()
term.setCursorPos(2,1)
print("–WELCOME TO SiliCo ™ TERMINAL VT5 INTERFACE–")
term.setCursorPos(2,3)
print("-CODE OPTIONS-")
term.setCursorPos(4,4)
print("[Open Door] 1")
term.setCursorPos(4,5)
print("[Close Door] 2")
term.setCursorPos(4,6)
print("[Shutdown Terminal] 3")
term.setCursorPos(2,8)
write("
 ")
input = read()

[b]if not fs.exists("status", "w")
-local file = io.open("status", "w")
-file:write()
-file:close()
end

local file = io.open("status", "w")
file:write(doorVariable)
file:close()

local file = io.open("status", "r")
doorVariable = file:read()
file:close()

if doorVariable == 0 then
-redstone.setOutput("back", false)
elseif doorVariable == 1 then
-redstone.setOutput("back", true)
end[/b]

if input == '1' then
-term.clear()
-redstone.setOutput("back", true)
-term.setCursorPos(2,1)
-print("[Door Opened]")
-sleep(1)
-term.clear()
-shell.run("startup")
elseif input == '2' then
-term.clear()
-redstone.setOutput("back", false)
-term.setCursorPos(2,1)
-print("[Door Closed]")
-sleep(1)
-term.clear()
-shell.run("startup")
elseif input == '3' then
-term.clear()
-term.setCursorPos(2,1)
-print("-TERMINAL SHUTDOWN REQUEST DETECTED-")
-term.setCursorPos(2,3)
-print("Are You Sure You Want To Shutdown This Terminal?")
-term.setCursorPos(4,4)
-print("[Yes] 4")
-term.setCursorPos(4,5)
-print("[No] 5")
-term.setCursorPos(2,7)
-write("[Code] ")
-input = read()
	
-if input == '4' then
--term.clear()
--term.setCursorPos(2,1)
--print("-TERMINAL SHUTTING DOWN-")
--sleep(0.5)
--term.setCursorPos(2,3)
--print("-THANK YOU FOR CHOOSING SiliCo ™-")
--sleep(1)
--os.shutdown()
--term.clear()
--shell.run("startup")
-elseif input == '5' then
--term.clear()
--term.setCursorPos(2,1)
--print("Shutdown Terminated")
--term.setCursorPos(2,3)
--print("Returning")
--sleep(2)
--term.clear()
--shell.run("startup")
-else
--term.clear()
--term.setCursorPos(2,1)
--print("Invalid Input")
--sleep(2)
--term.clear()
--shell.run("startup")
-end
else
-term.clear()
-term.setCursorPos(2,1)
- print("Invalid Input")
-sleep(2)
-term.clear()
-shell.run("startup")
end

Edited on 05 June 2014 - 06:41 PM
Bomb Bloke #17
Posted 06 June 2014 - 03:06 AM
There are a couple of issues.

First one is that every time your script starts the first line sets doorVariable to 0, and then when you hit your next bolded section you write that to your file without bothering to check what was already in the file… You perform your read after doing that write, meaning you can be sure that the file's contents will always be a "0".

The other is that file reads, much like the basic read() function, always return strings (no matter what type you saved in the first place). After reading, doorVariable with be neither numbers 0 nor 1 - it'll be a string representing "0" (or maybe "0.0", I dunno).

Change the entire lower bolded block to something like:

if fs.exists("status") then
  local file = io.open("status","r")
  doorVariable = tonumber(file:read())
  file:close()
end

redstone.setOutput("back", doorVariable == 1)

After doing that, whenever you open the door you need to re-open the file in write mode and dump a 1 into it, and whenever you close the door you need to re-open the file and write a 0.

Oh, and ditch the "shell.run("startup")" business - use a "while" loop instead:

while <condition> do
  Any code in this block will be repeated over and over as long as the condition is true.
end

For eg, if you want to repeat something pretty much forever, you'd do:

while true do
  print("Spam")
end
Kingofawesome13 #18
Posted 06 June 2014 - 09:38 PM
There are a couple of issues.

First one is that every time your script starts the first line sets doorVariable to 0, and then when you hit your next bolded section you write that to your file without bothering to check what was already in the file… You perform your read after doing that write, meaning you can be sure that the file's contents will always be a "0".

The other is that file reads, much like the basic read() function, always return strings (no matter what type you saved in the first place). After reading, doorVariable with be neither numbers 0 nor 1 - it'll be a string representing "0" (or maybe "0.0", I dunno).

Change the entire lower bolded block to something like:

if fs.exists("status") then
  local file = io.open("status","r")
  doorVariable = tonumber(file:read())
  file:close()
end

redstone.setOutput("back", doorVariable == 1)

After doing that, whenever you open the door you need to re-open the file in write mode and dump a 1 into it, and whenever you close the door you need to re-open the file and write a 0.

Oh, and ditch the "shell.run("startup")" business - use a "while" loop instead:

while <condition> do
  Any code in this block will be repeated over and over as long as the condition is true.
end

For eg, if you want to repeat something pretty much forever, you'd do:

while true do
  print("Spam")
end

I have a few questions; What's the difference between the "while —- do" and "startup? What does "true" in the last example you gave me mean? How would I work that into the script? How to I redo the numbers you were talking about? And what is meant by: "After doing that, whenever you open the door you need to re-open the file in write mode and dump a 1 into it, and whenever you close the door you need to re-open the file and write a 0."

Also is this what you meant by the part where I replace the lines of code?(I copied a snippet of the script(the beginning and end lines are 2 lines of the extra code each so it's an example of where it is) so if the see the beginning and end lines are not finished, it's because they are):


doorVariable = file:read()
file:close()

if fs.exists("status") then
  local file = io.open("status","r")
  doorVariable = tonumber(file:read())
  file:close()
end
redstone.setOutput("back", doorVariable == 1)

if input == '1' then
   term.clear()
Edited on 06 June 2014 - 07:39 PM
KingofGamesYami #19
Posted 06 June 2014 - 09:52 PM
-snip-

shell.run( "startup" ) --#runs the entire script again
while true do --# loops infinitely
 print( "hello" )
 sleep( 1 )
end
local x = 1 --#set x to a numerical value, so we do not get an error
while x < 10 do  --# emulating a for loop with while
 x = x - 1
end
generally, good coding practice is to avoid running the same file. shell interprets this like this:

shell.run( "thisprogramname" )  --#infinite stack of "thisprogramname"
--#file is still running...
The other thing he is trying to explain is this:

doorVariableA = file:read() --#doorVariableA = "1"
doorVariableN = tonumber( file:read() ) --#doorVariableN = 1
print( tostring(doorVariableA == doorVariableB).." "..tostring(doorVariableA == "1").." "..tostring(doorVariableA == 1) ) --#will print: false true false
Bomb Bloke #20
Posted 07 June 2014 - 02:13 AM
What's the difference between the "while —- do" and "startup?

Well, for starters, a "while" loop runs faster then reloading the whole script back into memory over and over.

A big catch with using shell.run() to restart the script repetitively is that the previous instance of the running script does not end when you do it. It sits there in RAM, waiting for the next instance of the script to end so it can carry on. Start enough copies of the script running, eventually you run out of RAM and the whole stack of scripts crash.

You can read up on loops here.

What does "true" in the last example you gave me mean?

Exactly that: true.

Say you've got a line like this:

if fs.exists("status") then

When Lua runs it, it checks to see what fs.exists("status") resolves to, the answer either being true or false. Based on the result of that condition, it either runs what's in the "if" block, or it doesn't.

In some situations - such as when you want to rig a "while" loop to run forever - you might want to provide a condition that will always resolve a certain way. In those instances, you can simply refer to eg "true" directly.

true / false values are called booleans.

How would I work that into the script?

Work out the exact bit of your script that needs to be repeated, stick "while true do" above it, a matching "end" below it, then go through and remove the shell.run() calls. You could wrap the loop around the entire script if you wanted, but if you think about it, you'll see that there are bits that only need to be run once for every time the script starts…

How to I redo the numbers you were talking about?

The code snippet I gave you does that by converting the strings it reads from the file to numbers, using the tonumber() function.

Also is this what you meant by the part where I replace the lines of code?

No. You've got two bolded sections - one is the very first line of your script, the other consists of four blocks of code. I'm saying nuke that entire second bolded section and replace it with the code I gave you.

And what is meant by: "After doing that, whenever you open the door you need to re-open the file in write mode and dump a 1 into it, and whenever you close the door you need to re-open the file and write a 0."

This is the later bit of your script where you open the door:

if input == '1' then
 term.clear()
 redstone.setOutput("back", true)
 term.setCursorPos(2,1)
 print("[Door Opened]")
 sleep(1)
 term.clear()
 shell.run("startup")

You need to add code in here which opens your "status" file in write mode, saves a 1 into it, then closes that file handle again.

Then take a look at the bit where you close the door, and… well, I assume you get the idea.