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

Program works only when it's in the mood

Started by Lancerlove, 21 December 2012 - 03:16 PM
Lancerlove #1
Posted 21 December 2012 - 04:16 PM
Hey guys,

I am new to this community( my first post) so I don't know if this problem was discussed before or not.

The problem is REALLY wierd, at least in my opinion. I have experience with several programming languages, but not with LUA. However, I do know one thing - outputting a text could not brake a program. Lua tries to contradict me.

Here is the story.

I tried for several hours to create a program to control my farm. My purpose was to make an interface asking for "char" events. But the snag here was to add start and stop functionality to the same button and the program interface to react accordingly. For example, when I pressed 1 the first time, the program should have sent a redstone signal and the message in the interface should have gone from Start to Stop the respective engine, but when I pressed 1 again, it should have stopped that and came back to the original text information.
I managed to get that to work using tables and I really thought I overcame the toughest problem of the code. From there on, it was just a matter of time to add more functionality in the if statements.

Now, I have a monitor connected to my computer.
When I pressed 1, I wanted my monitor to display - "Part of the factory - ON".

Without thinking that something could go wrong in adding some text output, I went on and added all of my monitor text in the file. I am 100% sure my code is correct regarding the monitor matter, but something wierd happens.

When I test my code now and I try to press the 1 button, the text in the interface doesn't change anymore, it only outputs the information on the monitor, but it never goes to the ON state, although it did before. I tried to remove all the monitor text I added, but the problem persists. I don't understand WHY. The code is the same as it was before the monitor addition and it worked then….

Here is the pastebin:

monitor = peripheral.wrap("left")
monitor.setTextScale(1)
fermenter = 0
squeezer = 0
melon = 0
tree = 0
entire = 0
color1 = colors.white
color2 = colors.lightBlue
color3 = colors.orange
color4 = colors.green
term.clear()
term.setCursorPos(1,1)
print("**************** Farm Software *******************")
print("**************************************************")
print("												  ")
print("**		   Choose what you want to do		 **")
print("**		   1. Start the fermenter			 **")
print("**		   2. Start the squeezer			  **")
print("**		   3. Start the melon farm			**")
print("**		   4. Start the tree farm			 **")
print("**		   5. Start the entire farm		   **")
print("**		   6. Exit							**")
print("												  ")
print("**************************************************")
monitor.clear()
monitor.setCursorPos(1,1)
monitor.write("														 ")
monitor.write("														 ")
monitor.write("														 ")
monitor.write("														 ")
monitor.write("|-------------------- Farm Software --------------------|")
monitor.write("|													   |")
monitor.write("|		 Fermenter		  |			OFF		   |")
monitor.write("|-------------------------------------------------------|")
monitor.write("|		 Squeezer		   |			OFF		   |")
monitor.write("|---------------------------------- --------------------|")
monitor.write("|		 Melon Farm		 |			OFF		   |")
monitor.write("|-------------------------------------------------------|")
monitor.write("|		 Tree Farm		  |			OFF		   |")
monitor.write("|-------------------------------------------------------|")
keys = {"1", "2", "3", "4", "5", "6"}
function responsiveInterface()
while true do
  event, key = os.pullEvent("char")

  for i = 1, #keys do
   if key == keys[1] then
	if fermenter == 0 then
	 fermenter = 1
	 rs.setBundledOutput("back", color1)
	 term.setCursorPos(1,5)
	 term.clearLine()
	 print("**		   1. Stop the fermenter			  **")
	 monitor.setCursorPos(1,7)
	 monitor.clearLine()
	 monitor.write("|		 Fermenter		  |			ON			|")
	else
	 fermenter = 0
	 rs.setBundledOutput("back", color1-color1)
	 term.setCursorPos(1,5)
	 term.clearLine()
	 print("**		   1. Start the fermenter			 **")
	 monitor.setCursorPos(1,7)
	 monitor.clearLine()
	 monitor.write("|		 Fermenter		  |			OFF		   |")
	end
   elseif key == keys[2] then
	if squeezer == 0 then
	 squeezer = 1
	 term.setCursorPos(1,6)
	 term.clearLine()
	 print("**		   2. Stop the squeezer			   **")
	 monitor.setCursorPos(1,9)
	 monitor.clearLine()
	 monitor.write("|		 Squeezer		   |			ON			|")
	else
	 squeezer = 0
	 term.setCursorPos(1,6)
	 term.clearLine()
	 print("**		   2. Start the squeezer			  **")
	 monitor.setCursorPos(1,9)
	 monitor.clearLine()
	 monitor.write("|		 Squeezer		   |			OFF		   |")
	end
   elseif key == keys[3] then
	if melon == 0 then
	 melon = 1
	 term.setCursorPos(1,7)
	 term.clearLine()
	 print("**		   3. Stop the melon farm			 **")
	 monitor.setCursorPos(1,11)
	 monitor.clearLine()
	 monitor.write("|		 Melon Farm		 |			ON			|")
	else
	 melon = 0
	 term.setCursorPos(1,7)
	 term.clearLine()
	 print("**		   3. Start the melon farm			**")
	 monitor.setCursorPos(1,11)
	 monitor.clearLine()
	 monitor.write("|		 Melon Farm		 |			OFF		   |")
	end
   elseif key == keys[4] then
	if tree == 0 then
	 tree = 1
	 term.setCursorPos(1,8)
	 term.clearLine()
	 print("**		   4. Stop the tree farm			  **")
	 monitor.setCursorPos(1,13)
	 monitor.clearLine()
	 monitor.write("|		 Tree Farm		  |			ON			|")
	else
	 tree = 0
	 term.setCursorPos(1,8)
	 term.clearLine()
	 print("**		   4. Start the tree farm			 **")
	 monitor.setCursorPos(1,13)
	 monitor.clearLine()
	 monitor.write("|		 Tree Farm		  |			OFF		   |")
	end
   elseif key == keys[5] then
	if entire == 0 then
	 entire = 1
	 fermenter = 1
	 squeezer = 1
	 melon = 1
	 tree = 1
	 term.setCursorPos(1,5)
	 term.clearLine()
	 print("**		   1. Stop the fermenter			  **")
	 term.setCursorPos(1,6)
	 term.clearLine()
	 print("**		   2. Stop the squeezer			   **")
	 term.setCursorPos(1,7)
	 term.clearLine()
	 print("**		   3. Stop the melon farm			 **")
	 term.setCursorPos(1,8)
	 term.clearLine()
	 print("**		   4. Stop the tree farm			  **")
	 term.setCursorPos(1,9)
	 term.clearLine()
	 print("**		   5. Stop the entire farm			**")
	else
	 entire = 0
	 fermenter = 0
	 squeezer = 0
	 melon = 0
	 tree = 0
	 term.setCursorPos(1,5)
	 term.clearLine()
	 print("**		   1. Start the fermenter			 **")
	 term.setCursorPos(1,6)
	 term.clearLine()
	 print("**		   2. Start the squeezer			  **")
	 term.setCursorPos(1,7)
	 term.clearLine()
	 print("**		   3. Start the melon farm			**")
	 term.setCursorPos(1,8)
	 term.clearLine()
	 print("**		   4. Start the tree farm			 **")
	 term.setCursorPos(1,9)
	 term.clearLine()
	 print("**		   5. Start the entire farm		   **")
	end
   elseif key == keys[6] then
	term.setCursorPos(1,1)
	term.clear()
	return
   end
  end
end

end
responsiveInterface()

If anyone has any idea about this bug, please tell me. Or , it might something wrong in the code that I don't know…


Thanks a bunch!
ChunLing #2
Posted 21 December 2012 - 05:09 PM
I guess you don't have a backup of the version that worked?

What's with the for loop? You end up switching the selected entry on and off a bunch of times. I don't know what you're trying to accomplish with that, but it seems to be the source of your problem.
cmurtheepic #3
Posted 21 December 2012 - 06:52 PM
I guess you don't have a backup of the version that worked?

What's with the for loop? You end up switching the selected entry on and off a bunch of times. I don't know what you're trying to accomplish with that, but it seems to be the source of your problem.
I AGREE!!! 100%
with chunling oh and love your title chunling :)/>
remiX #4
Posted 21 December 2012 - 11:23 PM
– Scrap that…

You shouldn't need to use a loop, I'll fix it up and show you

EDIT: It's better to use true and false then numbers.
Spoiler

monitor = peripheral.wrap("left")
monitor.setTextScale(1)
fermenter = false
squeezer = false
melon = false
tree = false
entire = false
color1 = colors.white
color2 = colors.lightBlue
color3 = colors.orange
color4 = colors.green
term.clear()
term.setCursorPos(1,1)
print("**************** Farm Software *******************")
print("**************************************************")
print("																						   ")
print("**				  Choose what you want to do		    **")
print("**				  1. Start the fermenter					    **")
print("**				  2. Start the squeezer						  **")
print("**				  3. Start the melon farm					  **")
print("**				  4. Start the tree farm					    **")
print("**				  5. Start the entire farm			    **")
print("**				  6. Exit													  **")
print("																						   ")
print("**************************************************")
monitor.clear()
monitor.setCursorPos(1,1)
monitor.write("																										  ")
monitor.write("																										  ")
monitor.write("																										  ")
monitor.write("																										  ")
monitor.write("|-------------------- Farm Software --------------------|")
monitor.write("|																										   |")
monitor.write("|				 Fermenter			    |					 OFF			    |")
monitor.write("|-------------------------------------------------------|")
monitor.write("|				 Squeezer				  |				    OFF			    |")
monitor.write("|---------------------------------- --------------------|")
monitor.write("|				 Melon Farm			  |					  OFF			    |")
monitor.write("|-------------------------------------------------------|")
monitor.write("|				 Tree Farm			    |					 OFF			    |")
monitor.write("|-------------------------------------------------------|")

function responsiveInterface()
    while true do
        e = {os.pullEvent("char")}
        if e[2] == "1" then
            if not fermenter then
                fermenter = true
                rs.setBundledOutput("back", color1)
                term.setCursorPos(1,5)
                term.clearLine()
                print("**				 1. Stop the fermenter						  **")
                monitor.setCursorPos(1,7)
                monitor.clearLine()
                monitor.write("|			    Fermenter			    |					 ON					  |")
            else
                fermenter = false
                rs.setBundledOutput("back", color1-color1)
                term.setCursorPos(1,5)
                term.clearLine()
                print("**				 1. Start the fermenter					    **")
                monitor.setCursorPos(1,7)
                monitor.clearLine()
                monitor.write("|			    Fermenter			    |					 OFF			    |")
            end
        elseif e[2] == "2" then
            if not squeezer then
                squeezer = true
                term.setCursorPos(1,6)
                term.clearLine()
                print("**				 2. Stop the squeezer				    **")
                monitor.setCursorPos(1,9)
                monitor.clearLine()
                monitor.write("|			    Squeezer				  |				    ON					  |")
            else
                squeezer = false
                term.setCursorPos(1,6)
                term.clearLine()
                print("**				 2. Start the squeezer						  **")
                monitor.setCursorPos(1,9)
                monitor.clearLine()
                monitor.write("|			    Squeezer				  |				    OFF			    |")
            end
       elseif e[2] == "3" then
            if not melon then
             melon = true
             term.setCursorPos(1,7)
             term.clearLine()
             print("**				 3. Stop the melon farm					    **")
             monitor.setCursorPos(1,11)
             monitor.clearLine()
             monitor.write("|			    Melon Farm			  |					  ON					  |")
            else
             melon = false
             term.setCursorPos(1,7)
             term.clearLine()
             print("**				 3. Start the melon farm					  **")
             monitor.setCursorPos(1,11)
             monitor.clearLine()
             monitor.write("|			    Melon Farm			  |					  OFF			    |")
            end
       elseif e[2] == "4" then
            if not tree then
             tree = true
             term.setCursorPos(1,8)
             term.clearLine()
             print("**				 4. Stop the tree farm						  **")
             monitor.setCursorPos(1,13)
             monitor.clearLine()
             monitor.write("|			    Tree Farm			    |					 ON					  |")
            else
             tree = false
             term.setCursorPos(1,8)
             term.clearLine()
             print("**				 4. Start the tree farm					    **")
             monitor.setCursorPos(1,13)
             monitor.clearLine()
             monitor.write("|			    Tree Farm			    |					 OFF			    |")
            end
       elseif e[2] == "5" then
        if not entire then
            entire = true
            fermenter = true
            squeezer = true
            melon = true
            tree = true
            term.setCursorPos(1,5)
            term.clearLine()
            print("**				 1. Stop the fermenter						  **")
            term.setCursorPos(1,6)
            term.clearLine()
            print("**				 2. Stop the squeezer				    **")
            term.setCursorPos(1,7)
            term.clearLine()
            print("**				 3. Stop the melon farm					    **")
            term.setCursorPos(1,8)
            term.clearLine()
            print("**				 4. Stop the tree farm						  **")
            term.setCursorPos(1,9)
            term.clearLine()
            print("**				 5. Stop the entire farm					  **")
        else
            entire = false
            fermenter = false
            squeezer = false
            melon = false
            tree = false
            term.setCursorPos(1,5)
            term.clearLine()
            print("**				 1. Start the fermenter					    **")
            term.setCursorPos(1,6)
            term.clearLine()
            print("**				 2. Start the squeezer						  **")
            term.setCursorPos(1,7)
            term.clearLine()
            print("**				 3. Start the melon farm					  **")
            term.setCursorPos(1,8)
            term.clearLine()
            print("**				 4. Start the tree farm					    **")
            term.setCursorPos(1,9)
            term.clearLine()
            print("**				 5. Start the entire farm			    **")
            end
       elseif e[2] == "6" then
            term.setCursorPos(1,1)
            term.clear()
            return
        end
    end
end

responsiveInterface()

I haven't tested it because I'm too lazy to set up a monitor ;)/> But it should work…
Also: Instead of it re-writing the text after each update, make it update just before the os.pullEvent("char") with if's etc.
Lancerlove #5
Posted 22 December 2012 - 12:35 AM
Hey guys thanks for the help. I don't know how it worked for me before, but it worked.
The idea of the for loop came from a forum when a guy asked the same thing as me… the thing is that, again, it worked before( btw, the code was exactly as this one without the monitor bits)

However, remiX's version works as a charm :)/>. Thank you master! I tried myself with if's before but it didn't update at all. Something was very wrong I guess.

Anyway, take care everyone!
ChunLing #6
Posted 22 December 2012 - 12:54 AM
Know what, I bet the for loop was originally supposed to replace all those if/elseif cases in an elegant manner with minimal rewriting of the display.

But a loop is just inherently the wrong structure. You would want to use tonumber(key) and then do maths on it to set the cursor and such. Something like:
local options = {
    {false,colors.white},
    {false,colors.lightBlue},
    {false,colors.orange},
    {false,colors.green},
}
at the top so that you have a table of exactly what needs updating for each input case, and then:

local index = tonumber(e[2])
if options[index][1] then
options[index][1] = false
rs.setBundledOutput("back", colors.combine(rs.getBundledInput("back"),options[index][2]))
-- do maths on index to figure out where to place cursor (or just have the positions stored in options[index] somewhere)
-- write only "Start" and "OFF" in the relevant locations
else
options[index][1] = true
rs.setBundledOutput("back", colors.subtract(rs.getBundledInput("back"),options[index][2]))
-- and terminal/monitor updating as above
-- write only "Stop " and "ON " in the relevant locations
end
That would be how I would do it. But meh….
Lancerlove #7
Posted 22 December 2012 - 02:56 AM
Yeah that's exactly what I was thinking of. I wanted the code to be as concise as possible, but I guess it was way over my head to do it.

Btw, do you have any idea why the monitor interface isn't showing when I start the program? It is supposed to be displayed on the monitor, even if I don't press any button.
remiX #8
Posted 22 December 2012 - 05:19 AM
Yeah that's what I thought you were getting at ;)/> I would have done it for you but I had to go
Lancerlove #9
Posted 22 December 2012 - 06:23 AM
Hey again guys,

Can anybody explain me how would I go about storing the variables even the computer shutdown or I log out?

I would like my computer to store these information somewhere and then when I run the program again to update accordingly, even from the start.

Because now, if I log off and my engines are still running, and then log in again the interface would say Start the X engine, although it is already running.

Any ideas?

Thanks!
remiX #10
Posted 22 December 2012 - 11:33 AM
Hey again guys,

Can anybody explain me how would I go about storing the variables even the computer shutdown or I log out?

I would like my computer to store these information somewhere and then when I run the program again to update accordingly, even from the start.

Because now, if I log off and my engines are still running, and then log in again the interface would say Start the X engine, although it is already running.

Any ideas?

Thanks!

You would need to save the variables to a file and then read them when you start the program. I'll write the code that you need to put at the beginning of your code which will get it to work.
Lancerlove #11
Posted 22 December 2012 - 12:18 PM
Hey again guys,

Can anybody explain me how would I go about storing the variables even the computer shutdown or I log out?

I would like my computer to store these information somewhere and then when I run the program again to update accordingly, even from the start.

Because now, if I log off and my engines are still running, and then log in again the interface would say Start the X engine, although it is already running.

Any ideas?

Thanks!

You would need to save the variables to a file and then read them when you start the program. I'll write the code that you need to put at the beginning of your code which will get it to work.

Thanks a bunch! You are great btw!
ChunLing #12
Posted 22 December 2012 - 12:38 PM
Huh. I thought remiX would have written your code by now.

I'm lazy, so I'll just point you to the fs and io API's while you're waiting.

The io API is a "wrapper" for fs, which implements the fs functions in a manner consistent with the standard Lua implementation of io. It is recommended as being more functional and better practice for coding in Lua, though most of us just use fs directly.
remiX #13
Posted 22 December 2012 - 01:22 PM
Omg lol, I forgot to post the code -_-/>

I also shortened it abit, basically redid it.

Pastebin
Lancerlove #14
Posted 22 December 2012 - 01:50 PM
Genius!! - It just has some text formatting bugs but I will fix them.
It's also a good source of inspiration for my future projects.

I hope to understand Lua as well as you do one day, but until then I still have to come here every now and then and post questions.

Thanks a lot!!