Jump to content




Help: text not changing colors.

api peripheral help

6 replies to this topic

#1 sidewinder5675

  • Members
  • 3 posts

Posted 21 June 2015 - 08: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 by sidewinder5675, 22 June 2015 - 07:08 PM.


#2 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 21 June 2015 - 10: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


#3 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 21 June 2015 - 11:06 PM

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

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

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 by TheOddByte, 21 June 2015 - 11:08 PM.


#4 sidewinder5675

  • Members
  • 3 posts

Posted 21 June 2015 - 11:19 PM

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 by sidewinder5675, 22 June 2015 - 02:08 AM.


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 22 June 2015 - 03: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.

#6 sidewinder5675

  • Members
  • 3 posts

Posted 22 June 2015 - 06:16 AM

View PostKingofGamesYami, on 22 June 2015 - 03:55 AM, said:

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?

#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 22 June 2015 - 05: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users