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

[Help] Terminal uploading piston state

Started by Macapple, 03 June 2012 - 06:30 AM
Macapple #1
Posted 03 June 2012 - 08:30 AM
Hi everyone,i'm not that good at coding,so,i need help to create a thing.
I've created a Terminal to lock a door,the problem is…
I want the terminal to print: "CURRENT DOOR STATUS: LOCKED " when it's closed
else if it's open.
Can anybody help me?
Lolgast #2
Posted 03 June 2012 - 09:45 AM
You could use a variable doorstatus, which changes when you open/close the door. Then print it. For example:
local doorstatus =  "locked"
print("Current door status: " .. doorstatus)
Now it will print "Current door status: locked"
If you change the variable doorstatus to "open" when you open the door, then print again, it will print "Current door status: open".
my_hat_stinks #3
Posted 03 June 2012 - 03:11 PM
local doorstatus =  "locked"
print("Current door status: " .. doorstatus)

No, not like that at all

There's only 2 states, it should be a boolean
local DoorOpen = true
print( "Door Status: "..  (DoorOpen and "Open" or "Locked") )
Macapple #4
Posted 03 June 2012 - 08:00 PM
local doorstatus =  "locked"
print("Current door status: " .. doorstatus)

No, not like that at all

There's only 2 states, it should be a boolean
local DoorOpen = true
print( "Door Status: "..  (DoorOpen and "Open" or "Locked") )

It worked,thanks bro :)/>/>
Lolgast #5
Posted 04 June 2012 - 08:20 AM
local doorstatus =  "locked"
print("Current door status: " .. doorstatus)

No, not like that at all

There's only 2 states, it should be a boolean
local DoorOpen = true
print( "Door Status: "..  (DoorOpen and "Open" or "Locked") )
In my opinion my solution is easier to understand for people who are not familiar with coding. Personally I'd think "(DoorOpen and "Open" or "Locked")" would be a bit hard to understand, if you're new to coding. Plus this will work when working with multiple statuses.
my_hat_stinks #6
Posted 04 June 2012 - 03:07 PM
In my opinion my solution is easier to understand for people who are not familiar with coding. Personally I'd think "(DoorOpen and "Open" or "Locked")" would be a bit hard to understand, if you're new to coding. Plus this will work when working with multiple statuses.

There is two states, so it is a boolean. If, in future, it needs changed, then is the time to change it.
Even then, the only edit I can think of is
print( "Door Status: "..  (Locked and "Locked " or "") .. (DoorOpen and "Open" or "Closed") )
To print "Locked Open", "Locked Closed", "Open", or "Closed"

People should learn best practices as soon as possible, and little tricks like
local Variable = Boolean and Value1 or Value2
Are a lot easier than
local Variable
if Boolean then
  Variable = Value1
else
  Variable = Value2
end
And will no doubt be useful in future


I get particularly irritated when people teach bad practices simply for "convenience". It'll be harder to become a good coder if you get into bad habits.
Lolgast #7
Posted 04 June 2012 - 07:49 PM
In my opinion my solution is easier to understand for people who are not familiar with coding. Personally I'd think "(DoorOpen and "Open" or "Locked")" would be a bit hard to understand, if you're new to coding. Plus this will work when working with multiple statuses.

There is two states, so it is a boolean. If, in future, it needs changed, then is the time to change it.
Even then, the only edit I can think of is
print( "Door Status: "..  (Locked and "Locked " or "") .. (DoorOpen and "Open" or "Closed") )
To print "Locked Open", "Locked Closed", "Open", or "Closed"
And how would you want to print something like, when using a login program, and registering who logged in, the last person who logged in? For example, there are 5 users who use the computer, and you want the last person who logged in to the computer. In my opinion

print("Last user who logged in: ".. lastLoggedIn)
would be MUCH easier then trying to work with booleans.
my_hat_stinks #8
Posted 04 June 2012 - 09:01 PM
And how would you want to print something like, when using a login program, and registering who logged in, the last person who logged in? For example, there are 5 users who use the computer, and you want the last person who logged in to the computer. In my opinion

print("Last user who logged in: ".. lastLoggedIn)
would be MUCH easier then trying to work with booleans.

I'm going to assume you're completely ignoring me, now, so I can call you the fool you're acting and you probably won't notice.
That isn't two states. That is a completely different situation.


That's like comparing a function with a loop. They have completely different uses, and are used in different situations.


Whether a door is open is a boolean. What user logged in last is a string (or a number, depending on how you're tracking it. Could occasionally be a table depending on what you're using it for, but most likely it's a string)