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

Help: text not changing colors.

Started by sidewinder5675, 21 June 2015 - 07:31 PM
sidewinder5675 #1
Posted 21 June 2015 - 09:31 PM
First off, I know next to nothing about LUA or coding in general. I know what I wrote is probably laughable, but it works (mostly) However, I am interested in learning more to make redstone bend to my will and have more general coding knowledge!


I tried to write a simple Advanced computer monitor code to control the output of liquids to a Tinker's Construct Tank, it works great!! however, There is a spot where I tried to change the color when the "if" statement is fulfilled and it does not change color! I tried to write it in the same if statement and it didn't work. I tried to originally write it so that if it was pressed again, it would go to sleep(1) to restart the program from the true statement, it did;t work that way, but it did put the text color green for a tick and shut the program off.
Here is a pastebin to what I wrote:

pastebin.com/eZDwu5ck

Appreciate any assistance and even ways to make this simple code better, or things that I should research in my coding quest. Thanks guys!
Edited on 22 June 2015 - 06:08 PM
Creator #2
Posted 21 June 2015 - 11:59 PM
if you are working on the terminal


term.clear()
term.setTextColor(colors.blue)
term.setBackroungColor(colors.gray)

On a monitor

mon = peripheral.wrap(side)
mon.clear()
same functions with mon instead of term
TheOddByte #3
Posted 22 June 2015 - 12:06 AM
First of all, to make sure there's actually a monitor attached to the computer, and also making it easier for you I'd suggest you'd check out peripheral.find
Example

local mon = peripheral.find( "monitor" )
if not monitor then --# Check if the mon variable is nil or not
	error( "No monitor attached!", 0 ) --# If it was, then we display an error
end
I know this may not be the case for you, but it may still be handy and save you trouble in the future.

Another suggestion I'd recommend would be to use tables, where it would be easier for you to store all your buttons. So if your basic "drawing" and "handling" function is working as it should, all you have todo is look at the table and fix any problems there.

Here's an example of how you'd create a table of buttons and draw them, it doesn't just make it easier for you, but it also helps shorten your code a lot.
Example: Drawing

local buttons = {
	{
		x	 = 1,
		y	 = 1,
		background = colors.lime;
		color = colors.white;
		text = "Test Button";
	}
}

mon.setBackgroundColor( colors.black )
mon.clear()

for i = 1, #buttons do
	mon.setCursorPos( buttons[i].x, buttons[i].y ) --# Set the x and y position to the buttons position
	mon.setBackgroundColor( buttons[i].background ) --# Set the background color to the buttons background color
	mon.setTextColor( buttons[i].color ) --# Same here, but change the text color
	mon.write( buttons[i].text ) --# And ofcourse display the text of the button
end

So how would you check if any of these buttons have been clicked/touched? Well that's pretty easy, we'll do the same when drawing the buttons, we'll loop through all of them.
Example: Handling

local event, side, x, y = os.pullEvent( "monitor_touch" )
for i = 1, #buttons do
	if x >= buttons[i].x and x <= buttons[i].x + #buttons[i].text - 1 and y == buttons[i].y then --# Check all positions
		-- one of the buttons was clicked
		-- code here
		break -- I'd suggest that you break out of the loop here, since one of the buttons was clicked
	end
end

Oh, and another suggestion, use code tags when posting code here on the forums
[code] code here [/code]
While this has nothing to do with actual coding, it helps people here to help you faster, as it makes it easier to read.

I've also seen this in your code

if x > 1 and x < 10 and y == 6 then
	mon.setCursorPos( 1, 6 )
	...
end
I believe you want to be able to touch at the x position 1 and 10, but the way you're checking the coordinates it checks if it's any number between them( 2-9 ), to solve this you'd want to use >= and <=


I hope this was helpful, and if you have any questions about any of the code then please feel free to ask.
Edited on 21 June 2015 - 10:08 PM
sidewinder5675 #4
Posted 22 June 2015 - 12:19 AM
Thank you for the help so quickly! And I will try to learn more about tables and how to take advantage of them un future code I work with.

This program works as of now, for exactly what I want it for, it's just that in this line here, nothing seems to happen.
Even when I add a command like mon.clear() it does not clear the monitor. Any suggestions why this may not be working? Am I just asking LUA to do something it doesn't know how to do?


  if x > 1 and x < 10 and y == 2 then
		mon.setCursorPos(1,2)
		mon.setTextColor(colors.lime)
		mon.write("Manyullyn-")
	  end
Edited on 22 June 2015 - 01:08 AM
KingofGamesYami #5
Posted 22 June 2015 - 04:55 AM
That should work perfectly - if you touch the monitor anywhere from (2,2) to (9,2). I'd guess you have it in a loop, and something happening after this redraws something different before you pull another event. Something like this:


while true do
  term.setCursorPos( 1, 1 )
  term.write( "Hello World" )
  local event = {os.pullEvent()}
  if event[ 1 ] == "key" and event[ 2 ] == keys.space then
    term.clear()
  end
end

Notice that even if you press spacebar, nothing appears to happen. In reality, the terminal is being cleared, but the text is redrawn before you can see the effects of clear.
sidewinder5675 #6
Posted 22 June 2015 - 07:16 AM
That should work perfectly - if you touch the monitor anywhere from (2,2) to (9,2). I'd guess you have it in a loop, and something happening after this redraws something different before you pull another event. Something like this:


while true do
  term.setCursorPos( 1, 1 )
  term.write( "Hello World" )
  local event = {os.pullEvent()}
  if event[ 1 ] == "key" and event[ 2 ] == keys.space then
	term.clear()
  end
end

Notice that even if you press spacebar, nothing appears to happen. In reality, the terminal is being cleared, but the text is redrawn before you can see the effects of clear.

OH! That makes sense, so it probably is turning it the lime green, but when it hits the "end" it starts the program again?

Is there a simple way to halt the program so that it just waits for another button to be pushed instead of re-writting out the terms to the monitor?
KingofGamesYami #7
Posted 22 June 2015 - 06:35 PM
Easy: just don't redraw anything.


term.setCursorPos( 1, 1 )
term.write( "Hello World" )
while true do
  local event = {os.pullEvent()}
  if event[ 1 ] == "key" and event[ 2 ] == keys.space then
    term.clear()
  elseif event[ 1 ] == "key" then
    term.setCursorPos( 1, 1 )
    term.write( "Hello World" )
  end
end

This'll display "Hello World" until you press space, where it will clear the screen. If you press any other key, it'll start displaying "Hello World" again.