Jump to content




Showing each word of a variable with different colors


  • You cannot reply to this topic
4 replies to this topic

#1 gustavowizard

  • Members
  • 94 posts

Posted 06 June 2014 - 09:40 AM

hey guys, i could use some help again, i got this program here, its ready and show the Amount of 10 Tanks (using CCsensors), all i want to do now is this:

- As you guys can see on the code below, it prints "Tank of:" then print the variable of the 'Name' atribute of each tank, so if i put Milk on a tank hes gonna print 'Tank of Milk', and them the amount. What i want is to make each word of a color when the variable changes, like when its milk, the word 'Milk' is printed white, if its water it prints blue, etc so this way if i replace the liquid of a tank it will change the word and also the color.

os.loadAPI("ocs/apis/sensor")
os.loadAPI("ocs/apis/graph")
monitor = peripheral.wrap("top")
prox = sensor.wrap("left")
monitor.setBackgroundColor(colors.black)
monitor.clear()
monitor.setTextColor(colors.white)
term.clear()
term.setCursorPos(1,1)
monitor.setTextScale(1.5)
monitor.setCursorPos(6,1)
monitor.write("Liquid Storage Control")
monitor.setCursorPos(1,2)
monitor.write("-----------------------------------------------------------")

while true do
moreDetails = prox.getTargetDetails("-5,0,-3")
monitor.setCursorPos(1,3)
monitor.write("Tank of:")
monitor.setCursorPos(10,3)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,3)
monitor.write(moreDetails.Tanks[1].Amount.."mB")
moreDetails = prox.getTargetDetails("-5,0,-4")
monitor.setCursorPos(1,4)
monitor.write("Tank of:")
monitor.setCursorPos(10,4)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,4)
monitor.write(moreDetails.Tanks[1].Amount.."mB")
moreDetails = prox.getTargetDetails("-5,0,-5")
monitor.setCursorPos(1,5)
monitor.write("Tank of:")
monitor.setCursorPos(10,5)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,5)
monitor.write(moreDetails.Tanks[1].Amount.."mB")
moreDetails = prox.getTargetDetails("-5,0,-6")
monitor.setCursorPos(1,6)
monitor.write("Tank of:")
monitor.setCursorPos(10,6)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,6)
monitor.write(moreDetails.Tanks[1].Amount.."mB")
moreDetails = prox.getTargetDetails("-5,0,-7")
monitor.setCursorPos(1,7)
monitor.write("Tank of:")
monitor.setCursorPos(10,7)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,7)
monitor.write(moreDetails.Tanks[1].Amount.."mB")
moreDetails = prox.getTargetDetails("5,0,-3")
monitor.setCursorPos(1,8)
monitor.write("Tank of:")
monitor.setCursorPos(10,8)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,8)
monitor.write(moreDetails.Tanks[1].Amount.."mB")
moreDetails = prox.getTargetDetails("5,0,-4")
monitor.setCursorPos(1,9)
monitor.write("Tank of:")
monitor.setCursorPos(10,9)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,9)
monitor.write(moreDetails.Tanks[1].Amount.."mB")
moreDetails = prox.getTargetDetails("5,0,-5")
monitor.setCursorPos(1,10)
monitor.write("Tank of:")
monitor.setCursorPos(10,10)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,10)
monitor.write(moreDetails.Tanks[1].Amount.."mB")
moreDetails = prox.getTargetDetails("5,0,-6")
monitor.setCursorPos(1,11)
monitor.write("Tank of:")
monitor.setCursorPos(10,11)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,11)
monitor.write(moreDetails.Tanks[1].Amount.."mB")
moreDetails = prox.getTargetDetails("5,0,-7")
monitor.setCursorPos(1,12)
monitor.write("Tank of:")
monitor.setCursorPos(10,12)
monitor.write(moreDetails.Tanks[1].Name)
monitor.setCursorPos(22,12)
monitor.write(moreDetails.Tanks[1].Amount.."mB")

end


pastebin code:
http://pastebin.com/QVKrBXtD

also i notice something odd, when i take all the liquid from a tank, he does not detect its Amount anymore (it do not detect as '0') so it can send back the Amount data hehe is there a way to bypass this or only by fixing the CCsensor mod?

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 06 June 2014 - 09:44 AM

Define a table up the top along these lines:

local liquidColour = {["water"] = colours.blue, ["lava"] = colours.orange, ["milk"] = colours.white}

Make sure that the names you use in the table (water/lava/milk/etc) match the names your tank peripherals give you exactly (including capitalisation).

Then you can do stuff like this:

monitor.setCursorPos(10,10)
monitor.setTextColor( liquidColour[ moreDetails.Tanks[1].Name ] )
monitor.write(moreDetails.Tanks[1].Name)

Edit: By the way, are you familiar with loops? If so, you might be able to see how joining a for loop together with another table would allow you to dramatically shorten your script.

Edited by Bomb Bloke, 06 June 2014 - 09:47 AM.


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 06 June 2014 - 09:56 AM

View PostBomb Bloke, on 06 June 2014 - 09:44 AM, said:

Edit: By the way, are you familiar with loops? If so, you might be able to see how joining a for loop together with another table would allow you to dramatically shorten your script.
Even just a function would massively shorten the code.

For example this function:

local function outputTankInfo(tankLoc, yPos)
  local tankInfo = prox.getTargetDetails(tankLoc)
  monitor.setCursorPos(1, yPos)
  monitor.write("Tank of:")
  monitor.setCursorPos(10, yPos)
  monitor.write(tankInfo.Tanks[1].Name)
  monitor.setCursorPos(22, yPos)
  monitor.write(tankInfo.Tanks[1].Amount.."mB")
end

would reduce the while loop to
while true do
  outputTankInfo("-5,0,-3", 3)
  outputTankInfo("-5,0,-4", 4)
  outputTankInfo("-5,0,-5", 5)
  outputTankInfo("-5,0,-6", 6)
  outputTankInfo("-5,0,-7", 7)
  outputTankInfo("5,0,-3", 8)
  outputTankInfo("5,0,-4", 9)
  outputTankInfo("5,0,-5", 10)
  outputTankInfo("5,0,-6", 11)
  outputTankInfo("5,0,-7", 12)
  sleep(1)
end

Edited by theoriginalbit, 06 June 2014 - 10:04 AM.


#4 gustavowizard

  • Members
  • 94 posts

Posted 06 June 2014 - 11:37 PM

lol nice i need to learn a lot i started to learn this 2 weeks ago :D, thanks for that info originalbit, and bomb, i tried that code, this line:

monitor.setTextColor( liquidColour[ moreDetails.Tanks[1].Name ] )

gives the error


tank2:33:Expected Number, what im doing wrong?

ty

#5 gustavowizard

  • Members
  • 94 posts

Posted 06 June 2014 - 11:52 PM

oh nvm i fix it, thanks Bomb it works!, im gonna study your code later to learn more original, thank you both





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users