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

Programming Assistance Needed For C.C. Noob-In-Training

Started by Desperado92, 01 September 2012 - 09:49 PM
Desperado92 #1
Posted 01 September 2012 - 11:49 PM
Hi guys.

I've been playing around with ComputerCraft and trying my best to learn lua, but I've encountered a problem with a program I have been trying to write.

Basically, me and my friend were building a train line. At either end of the platform there is a detector track hooked up to a computer, which has a monitor on top of it. I'm trying to make a program that will display a message on the monitor when one detector is triggered and a different message when the second is triggered.

Here is what I've come up with:

[attachment=432:TrainDetector.txt]

The issue i'm having is that the computer expects an "=" on line 2. Any additional issues I am currently unaware of, however I came to the forums to ask all of you pros:

a) what i've done wrong and
:)/>/> if there is a better way of doing it than the way I am…

I know i'm not amazing, but I won't learn anything if I don't ask!

Thank you all for reading my shout out for help!
Jan #2
Posted 02 September 2012 - 12:06 AM
original code:

if redstone.getInput("right") == "true" then
	    monitor top print("The Train has entered the Station!")
if redstone.getInput("left") == "true" then
	    monitor top print("The Train has left the Station!")
else
sleep(1)
end
Hello developer, I found (i am sorry to say) lots of mistakes:

1. It is true not "true" . Othewise Lua thinks you are talking about a string
2. When using if for the second time on the same level, use 'elseif'
3. The monitor isnt initialised, and used completely wrong. I would recommend for beginners to FIRST print it on the terminal screen, and if the code works, THEN print it on the monitor.
4. I put 'while true do' and 'end' around your code. This makes the program repeat, and not just check the redstone once.
5. I put the sleep after the program. You also need to sleep if the program detects a cart, not only when it doesnt detect.
6. I made the sleep last 0.1 seconds. I dont know exactly how long a detector pulses, but this way you are sure it doesnt miss the cart.


Changed code

while true do
if redstone.getInput("right") == true then
  print("The Train has entered the Station!")
elseif redstone.getInput("left") == true then
  print("The Train has left the Station!")
end
sleep(0.1)
end

Please note the adapted version is a little spammy :)/>/> but I tested it and it works.
Tomorrow I add the monitor version if you like.
Matrixmage #3
Posted 02 September 2012 - 12:16 AM
there were some structuring error that I fixed but I can't get the monitors to work, here is what i got anyway, sorry :)/>/>


while true do
	if redstone.getInput("right") then
		monitor top print("The Train has entered the Station!")

	elseif redstone.getInput("left") then
		monitor top print("The Train has left the Station!")

end
end
Desperado92 #4
Posted 02 September 2012 - 12:23 AM
original code:

if redstone.getInput("right") == "true" then
		monitor top print("The Train has entered the Station!")
if redstone.getInput("left") == "true" then
		monitor top print("The Train has left the Station!")
else
sleep(1)
end
Hello developer, I found (i am sorry to say) lots of mistakes:

1. It is true not "true" . Othewise Lua thinks you are talking about a string
2. When using if for the second time on the same level, use 'elseif'
3. The monitor isnt initialised, and used completely wrong. I would recommend for beginners to FIRST print it on the terminal screen, and if the code works, THEN print it on the monitor.
4. I put 'while true do' and 'end' around your code. This makes the program repeat, and not just check the redstone once.
5. I put the sleep after the program. You also need to sleep if the program detects a cart, not only when it doesnt detect.
6. I made the sleep last 0.1 seconds. I dont know exactly how long a detector pulses, but this way you are sure it doesnt miss the cart.


Changed code

while true do
if redstone.getInput("right") == true then
  print("The Train has entered the Station!")
elseif redstone.getInput("left") == true then
  print("The Train has left the Station!")
end
sleep(0.1)
end

Please note the adapted version is a little spammy :)/>/> but I tested it and it works.
Tomorrow I add the monitor version if you like.

Thank you for your help!!

I really appreciate all your crit. Thats why the forums are here after all lol,

Adding a monitor would be a nice learning curve. Me and my friend are both noob with cc, but i'm making an effort to learn lua.
Glad to see I didn't get a "you suck at lua" response though lol. Being a noob sucks lol

Cheers mates!
Magus #5
Posted 02 September 2012 - 02:39 AM
Its better to use os.pullEvent instead of a busy wait


  while true do
    os.pullEvent("redstone")
    if rs.getInput("left") then
       print("The train has left the station")
    else
       print("The train has entered the station")
    end
end
Jan #6
Posted 02 September 2012 - 11:27 AM
Ok I'll show you how to implement monitor


monitor = peripheral.wrap("top")
This will make a 'monitor' table, with functions


function say(text)
monitor.clear()
monitor.setCursorPos(1,1)
monitor.write(text)
end

This defines a simple function. The name of the function is 'say' and the argument will be saved as 'text'
It calls a function to clear the screen, then one to reset the cursor position, and finally 'writes' the gives text


while true do
os.pullEvent("redstone")
if redstone.getInput("right") == true then
  say("The Train has entered the Station!")
  sleep(3)
  say("...")
elseif redstone.getInput("left") == true then
  say("The Train has left the Station!")
  sleep(3)
  say("...")
end
end
In the main loop I replaced the print functions with the new say function
also I let the text stay on the screen for 3 seconds, and then clear the screen
Also I added Magus suggestion:
the os.pullEvent function waits until it 'something happens' (such as a key press, or redstone change) which is called an event.
Using os.pullEvent("redstone") waits for redstone events

Good luck programming

Full code:


monitor = peripheral.wrap("top")
function say(text)
monitor.clear()
monitor.setCursorPos(1,1)
monitor.write(text)
end
while true do
os.pullEvent("redstone")
if redstone.getInput("right") == true then
  say("The Train has entered the Station!")
  sleep(3)
  say("...")
elseif redstone.getInput("left") == true then
  say("The Train has left the Station!")
  sleep(3)
  say("...")
end
end