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

Question about variables if/else

Started by DjTranceFire, 29 October 2014 - 09:14 AM
DjTranceFire #1
Posted 29 October 2014 - 10:14 AM
Hey there.
I'm completly new to all this scripting in lua, but i want to learn it.
Currently i'm just trying to create everything possible to learn everything.. :P/>
In my script i want to check some variables if they are true or false.
But the code is realy messy if i create like 10 or 20 times almost the same.
Is there any easy way to create something like a table and just use the same function for every variable in this table?

Something to check "test1,test2,test3" and print something without creating the same shit again and again?


if test1 == true then
	print("test1 - OK")
else
	print("test1 - ERROR")
end

if test2 == true then
	print("test2 - OK")
else
	print("test2 - ERROR")
end

if test3 == true then
	print("test3 - OK")
else
	print("test3 - ERROR")
end


Sorry for a stupid question like this and sorry for my bad english.
I hope its not to hard to understand.
Lyqyd #2
Posted 29 October 2014 - 03:11 PM
If that is your actual code, there isn't a better way to write it without also changing something else (the final text it would output, or putting the variables in a table, etc.). If that's not the actual code you're working with, please post the actual code.
KingofGamesYami #3
Posted 29 October 2014 - 03:18 PM
Actually,

if test1 == true then
        print("test1 - OK")
else
        print("test1 - ERROR")
end
can be rewritten as

print( "test1 - " .. (test1 and "OK" or "ERROR") )
DjTranceFire #4
Posted 29 October 2014 - 03:33 PM
If that's not the actual code you're working with, please post the actual code.

No its not the aktual code, but it is almost the same then it was when i wrote this thread..^^
Basically i'm trying to show informations from 10 reactors and 10 turbines from BigReactors on one monitor.
Its working, but i think the code i'm working with is realy bad and extremly messy.
Its ok for me that the code is bad. I just started yesterday with lua so i dont expect a brilliant piece of code.. :D/>
But i dont like it if my code is messy. I hate it if some pieces of a code are used again and again if i could use it only one time with another way.
Because of that i would like to use something like tables/arrays to use most of the script only one time and just run it for every turbine/reactor.

My actual code is this:
Spoiler

local turb01 = peripheral.wrap("BigReactors-Turbine_1")
local turb02 = peripheral.wrap("BigReactors-Turbine_2")
local turb03 = peripheral.wrap("BigReactors-Turbine_3")
local turb04 = peripheral.wrap("BigReactors-Turbine_4")
local turb05 = peripheral.wrap("BigReactors-Turbine_5")
local turb06 = peripheral.wrap("BigReactors-Turbine_6")
local turb07 = peripheral.wrap("BigReactors-Turbine_7")
local turb08 = peripheral.wrap("BigReactors-Turbine_8")
local turb09 = peripheral.wrap("BigReactors-Turbine_9")
local turb10 = peripheral.wrap("BigReactors-Turbine_10")
local reac01 = peripheral.wrap("BigReactors-Reactor_1")
local reac02 = peripheral.wrap("BigReactors-Reactor_2")
local reac03 = peripheral.wrap("BigReactors-Reactor_3")
local reac04 = peripheral.wrap("BigReactors-Reactor_4")
local reac05 = peripheral.wrap("BigReactors-Reactor_5")
local reac06 = peripheral.wrap("BigReactors-Reactor_6")
local reac07 = peripheral.wrap("BigReactors-Reactor_7")
local reac08 = peripheral.wrap("BigReactors-Reactor_8")
local reac09 = peripheral.wrap("BigReactors-Reactor_9")
local reac10 = peripheral.wrap("BigReactors-Reactor_10")
local mon = peripheral.wrap("bottom")
mon.clear()
mon.setTextScale(1)
mon.setBackgroundColour(colors.black)

mon.setCursorPos(1,1)
mon.write("   Global Turbine Overview")
mon.setCursorPos(1,2)
mon.write("   -----------------------")
mon.setCursorPos(1,3)
mon.write("Turbine #01")
mon.setCursorPos(1,4)
mon.write("Turbine #02")
mon.setCursorPos(1,5)
mon.write("Turbine #03")
mon.setCursorPos(1,6)
mon.write("Turbine #04")
mon.setCursorPos(1,7)
mon.write("Turbine #05")
mon.setCursorPos(1,8)
mon.write("Turbine #06")
mon.setCursorPos(1,9)
mon.write("Turbine #07")
mon.setCursorPos(1,10)
mon.write("Turbine #08")
mon.setCursorPos(1,11)
mon.write("Turbine #09")
mon.setCursorPos(1,12)
mon.write("Turbine #10")

if turb01.getActive() == true and reac01.getActive() == true then
	mon.setCursorPos(14,3)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,3)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb01.getActive() == false and reac01.getActive() == false then
	mon.setCursorPos(14,3)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,3)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,3)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
if turb02.getActive() and reac02.getActive() == true then
	mon.setCursorPos(14,4)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,4)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb02.getActive() == false and reac02.getActive() == false then
	mon.setCursorPos(14,4)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,4)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,4)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
if turb03.getActive() == true and reac03.getActive() == true then
	mon.setCursorPos(14,5)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,5)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb03.getActive() == false and reac03.getActive() == false then
	mon.setCursorPos(14,5)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,5)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,5)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
if turb04.getActive() == true and reac04.getActive() == true then
	mon.setCursorPos(14,6)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,6)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb04.getActive() == false and reac04.getActive() == false then
	mon.setCursorPos(14,6)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,6)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,6)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
if turb05.getActive() == true and reac05.getActive() == true then
	mon.setCursorPos(14,7)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,7)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb05.getActive() == false and reac05.getActive() == false then
	mon.setCursorPos(14,7)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,7)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,7)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
if turb06.getActive() == true and reac06.getActive() == true then
	mon.setCursorPos(14,8)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,8)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb06.getActive() == false and reac06.getActive() == false then
	mon.setCursorPos(14,8)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,8)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,8)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
if turb07.getActive() == true and reac07.getActive() == true then
	mon.setCursorPos(14,9)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,9)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb07.getActive() == false and reac07.getActive() == false then
	mon.setCursorPos(14,9)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,9)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,9)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
if turb08.getActive() == true and reac08.getActive() == true then
	mon.setCursorPos(14,10)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,10)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb08.getActive() == false and reac08.getActive() == false then
	mon.setCursorPos(14,10)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,10)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,10)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
if turb09.getActive() == true and reac09.getActive() == true then
	mon.setCursorPos(14,11)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,11)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb09.getActive() == false and reac09.getActive() == false then
	mon.setCursorPos(14,11)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,11)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,11)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
if turb10.getActive() == true and reac10.getActive() == true then
	mon.setCursorPos(14,12)
	mon.setBackgroundColour(colors.green)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,12)
	mon.setBackgroundColour(colors.gray)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
elseif turb10.getActive() == false and reac10.getActive() == false then
	mon.setCursorPos(14,12)
	mon.setBackgroundColour(colors.gray)
	mon.write(" ONLINE ")
	mon.setCursorPos(22,12)
	mon.setBackgroundColour(colors.red)
	mon.write(" OFFLINE ")
	mon.setBackgroundColour(colors.black)
else
	mon.setCursorPos(14,12)
	mon.setBackgroundColour(colors.yellow)
	mon.write("	  IDLING	  ")
	mon.setBackgroundColour(colors.black)
end
InputUsername #5
Posted 29 October 2014 - 04:56 PM
Liquid is right, there isn't a shorter way to do what you want to do.

SpoilerAs this is programming, there actually is a way, but I wouldn't recommend using it.
Here it is (untested).

for i=1,3 do
	pcall( loadstring("print('test" .. i .. " - ' .. (test" .. i .." and 'OK' or 'ERROR'))"))
end
Again, don't use this, it's just a proof of concept, or something.
Edited on 29 October 2014 - 03:56 PM
KingofGamesYami #6
Posted 29 October 2014 - 05:28 PM
This should work, any questions are appreciated (haven't actually tested with reactors/turbines). It also does not do colors, but I'm sure you can add that in yourself.

local mon = peripheral.wrap( "bottom" )
local tPower = {}
for i = 1, 10 do
  tPower[ i ] = {
    reactor = peripheral.wrap( "BigReactors-Reactor-" .. i ),
    turbine = peripheral.wrap( "BigReactors-Turnine-" .. i ),
  }
  mon.setCursorPos( 1, i )
  mon.write( "Turbine " .. i .. ": " )
  tPower[ i ].x, tPower[ i ].y = mon.getCursorPos()
end
local function update()
  for k, v in ipairs( tPower ) do
    local rOnline, tOnline = v.reactor.getActive(), v.turbine.getActive()
    mon.setCursorPos( v.x, v.y )
    mon.write( rOnline and tOnline and " Online " or not rOnline and not tOnline and " Offline " or " Idling " )
  end
end
Edited on 29 October 2014 - 04:30 PM
Dragon53535 #7
Posted 29 October 2014 - 05:48 PM
There is actually a LOT better way to do this with loops and the already available modem.getNamesRemote makes it so you can allow for an unlimited amount of reator/turbines.

Lets start with finding your wired modem that you're connecting to the turbines and reactors with, as well as finding the monitor.

Spoiler

local function findPeriph(arg1) --#Declare the function findPeriph with the parameter arg1
  for a,v in pairs({"top","bottom","left","right","back","front"}) do --#Look through the specified table of sides. a is the numerical index/string index of the item
  --#so in this case top is 1 bottom is 2 and so on.
  --#v is the word itself, top bottom left right and so on. And it will go through them one at a time, very useful :P/>/>
	if peripheral.getType(v) == arg1 then --#If the side is what we're looking for
	  return v --#Return the side
	end
  end
  error("Cannot find " .. arg1 .. " connected to computer",0) --# If the device we're looking for cannot be found, tell them.
end
local modem = peripheral.wrap(findPeriph("modem")) --#Look for and wrap the modem
local mon = peripheral.wrap(findPeriph("monitor")) --#Look for and wrap the monitor
So this code is a simple one, and i use it quite a bit as i need to sometimes find certain peripherals connected to a computer, and rather than force myself to hard code one side, i make it check them all.

Next you want to setup the monitor and start printing out the states (I leave the turbine names until last so that i can know how many there are)

Spoiler

mon.clear()
mon.setTextScale(1)
mon.setBackgroundColour(colors.black)
local function printConnected(arg1,arg2,count)
  local s = 3
  --#in this case arg1 is what we're looking for, and arg2 is the x position we're printing to
  for a,v in pairs(modem.getNamesRemote()) do --#It grabs the names of all remotely attached peripherals.
	--#Just like you did at the top of your code manually.
	if peripheral.getType(v) == arg1 then --#Peripheral.getType gets what it is from the name.
	  --#So a monitor is monitor modem is modem and so on.
	  if count then --#If we should keep count. (I don't use this for getting the reactors)
		maxturbines += 1 --#Add one to the max
	  end
	  mon.setCursorPos(arg2,s)--#Set it in the correct spot
	  local active = peripheral.call(v,"getActive") --#This is local z = peripheral.wrap("peripheral") and then z.getActive()
	  if active then --#If it's active.
		mon.setBackgroundColor(colors.gray)
		mon.write(" ONLINE ")
	  elseif not active then --#If it's not active
		mon.setBackgroundColor(colors.red)
		mon.write(" OFFLINE ")
	  end
	  mon.setBackgroundColor(colors.black)
	  s += 1 --Add one to the line so we go to the next
	end
  end
end
printConnected("BigReactors-Turbine",14,true) --#Call printConnected and tell it we're looking for BigReactors-Turbine and put it at mon.setCursorPos(14,y) and we're going to count.
printConnected("BigReactors-Reactor",22) --#Call printConnected and tell it we're looking for BigReactors-Reactor and put it at mon.setCursorPos(22,y)

And finally we need to print out the numbers of our reactors.

Spoiler

mon.setCursorPos(1,1)
mon.write("   Global Turbine Overview")
mon.setCursorPos(1,2)
mon.write("   -----------------------")
for num = 1, maxturbines do --#Starting at one and ending at the amount connected, count up one
  mon.setCursorPos(1,num+2) --#Set the cursor to the correct spot
  mon.write("Turbine #"..string.format("%02d",num)) --# %02d says add a 0 if a number is less than 2 digits
  --#Print it out, I'm using string.format to make sure it has a digit in the tens place
end

And now the finished product :D/>

Spoiler

local function findPeriph(arg1)
  for a,v in pairs({"top","bottom","left","right","back","front"}) do
	if peripheral.getType(v) == arg1 then
	  return v
	end
  end
  error("Cannot find " .. arg1 .. " connected to computer")
end
local modem = peripheral.wrap(findPeriph("modem"))
local mon = peripheral.wrap(findPeriph("monitor"))
local maxturbines = 0
mon.clear()
mon.setTextScale(1)
mon.setBackgroundColour(colors.black)
local function printConnectted(arg1,arg2,count)
  local s = 3
  --#in this case arg1 is what we're looking for, and arg2 is the x position we're printing to
  for a,v in pairs(modem.getNamesRemote()) do --#It grabs the names of all remotely attached peripherals.
	--#Just like you did at the top of your code manually.
	if peripheral.getType(v) == arg1 then --#Peripheral.getType gets what it is from the name.
	  --#So a monitor is monitor modem is modem and so on.
	  if count then --#If we should keep count. (I don't use this for getting the reactors)
		maxturbines += 1 --#Add one to the max
	  end
	  mon.setCursorPos(arg2,s)--#Set it in the correct spot
	  local active = peripheral.call(v,"getActive")
	  if active then --#If it's active.
		mon.setBackgroundColor(colors.gray)
		mon.write(" ONLINE ")
	  elseif not active then --#If it's not active
		mon.setBackgroundColor(colors.red)
		mon.write(" OFFLINE ")
	  end
	  mon.setBackgroundColor(colors.black)
	  s += 1 --Add one to the line so we go to the next
	end
  end
end
printConnected("BigReactors-Turbine",14,true)
printConnected("BigReactors-Reactor",22)
mon.setCursorPos(1,1)
mon.write("   Global Turbine Overview")
mon.setCursorPos(1,2)
mon.write("   -----------------------")
for num = 1, maxturbines do --#Starting at one and ending at the amount connected, count up one
  mon.setCursorPos(1,num+2) --#Set the cursor to the correct spot
  mon.write("Turbine #"..string.format("%02d",num)) --# %02d says add a 0 if a number is less than 2 digits
  --#Print it out, I'm using string.format to make sure it has a digit in the tens place
end

This should work for wherever you try to put it, however it doesn't print out idle for you.
Edited on 29 October 2014 - 04:55 PM
Bomb Bloke #8
Posted 29 October 2014 - 11:13 PM
Let's muddy the waters further.

Spoiler
local turb, reac, mon = {}, {}

local function monPrint(text,x,y,col)
	local curX, curY = mon.getCursorPos()
	x, y = x or curX, y or curY  -- If x or y weren't specified, set them to current screen co-ords.
	if x ~= curX or y ~= curY then mon.setCursorPos(x,y) end  -- Move the cursor if it isn't where specified.
	if col then mon.setBackgroundColour(col) end  -- Change the background colour, if specified.
	mon.write(text)
	mon.setCursorPos(1,y+1)  -- Move down a line.
end

for key, value in pairs(peripheral.getNames()) do
	if peripheral.getType(value) == "BigReactors-Turbine" then     turb[#turb+1] = peripheral.wrap(value).getActive
	elseif peripheral.getType(value) == "BigReactors-Reactor" then reac[#reac+1] = peripheral.wrap(value).getActive
	elseif peripheral.getType(value) == "monitor" then             mon = peripheral.wrap(value) end
end

local xSize, ySize = mon.getSize()

mon.setBackgroundColour(colors.black)
mon.clear()
mon.setTextScale(1)

monPrint("Global Turbine Overview",3,1)
monPrint("-----------------------",3)
for i=1,9 do monPrint("Turbine #0"..tostring(i)) end
monPrint("Turbine #10")

while true do  -- A loop that repeats indefinitely (bearing in mind Ctrl + T terminates scripts).
	for i = 1,10 do
		local tOn, rOn = turb[i](), reac[i]()  -- Slow, so do it once then record the result.
		monPrint(string.rep(" ", xSize - 13), 14, 2+i, colors.black)  -- Partial term.clearLine() effect.		

		if tOn and rOn then  -- If both are true, ...
			monPrint(" ONLINE ",  14, 2+i, colors.green)
			monPrint(" OFFLINE ", 22, 2+i, colors.gray)
		elseif not (tOn or rOn) then  -- If both are false, ...
			monPrint(" ONLINE ",  14, 2+i, colors.gray)
			monPrint(" OFFLINE ", 22, 2+i, colors.red)
		else monPrint("        IDLING         ", 14, 2+i, colors.yellow) end
	end
	
	sleep(5)
end

Things get a little trickier if the turbine names don't sync up with the reactor names.
Edited on 30 October 2014 - 03:06 AM
Dragon53535 #9
Posted 29 October 2014 - 11:26 PM
Bomb Bloke, you're never wrapping the monitor.
Bomb Bloke #10
Posted 30 October 2014 - 01:00 AM
Or the reactors/turbines, for that matter. Fixed. :)/>
Dragon53535 #11
Posted 30 October 2014 - 02:00 AM
I guess you learn something new every day, even while helping people.


if not(true or false) then

It never occurred to me that this works as an if both false check. And until i actually thought about it, i thought it was if (not )or (not ) instead of if not both. Because for the only way that expression can be true, is if the inside is false, and an or always tries to find the true. So if any are true, it's going to choose the true and then it will become false. However if neither are true, then it becomes true. Very neat little trick.

Thanks for teaching me a new way to logic BB :D/>
Edited on 30 October 2014 - 01:01 AM
Bomb Bloke #12
Posted 30 October 2014 - 04:18 AM
Since you mention it, that block could actually be simplified further:

                if tOn == rOn then
                	monPrint(" ONLINE ",  14, 2+i, tOn and colors.green or colors.gray)
                        monPrint(" OFFLINE ", 22, 2+i, rOn and colors.gray or colors.red)
                else monPrint("        IDLING         ", 14, 2+i, colors.yellow) end
Dragon53535 #13
Posted 30 October 2014 - 04:43 AM
That is actually simpler. Jeez Bomb… way to notice the little things. Great job simplifying your own code, however i'm curious if we actually helped him :P/> All joking aside you are quite a ways better than I due to experience, however i'm catching up :D/>

Anyways, I do hope we helped answer your question GjTranceFire2 in all the ways that we have showed you different ways to look at it and different things you could do. All work, and all are good.
DjTranceFire #14
Posted 30 October 2014 - 01:57 PM
Hey guys.
First i want to say thank you for all your help! :)/>
Its nice to see an active community that is realy trying to help! :)/>
I think i have to take more then one look at your codes to realy understand everything.
All the comments in that code are nice and realy helpfull. I like it!

Because i have a lot of work to do atm, all i could do is do a small test to let you know if it worked or not.
Sadly it seems like its not working.

The code from Dragon53535 in post #7 throws an error:
bios:366: [string "startup"]:23 '=' expected

The reworked code on post #8 seems to work but looks like its buggy.
This is how it actually looks like with your code:
http://s14.directupl...30/p4vvchz9.png

This is the real state of turbines and reactors (while running my own code (Post #4):
http://s14.directupl...30/6s34vg2u.png

If i try to use the code from KingofGamesYami from Post #6 it looks like this:
http://s14.directupl...30/hdci726u.png

Tomorrow i will take a deeper look at all the codes and hopefully understand most of them.. :D/>
Maybe i can fix the problems..^^

Again, sorry for my bad english! :x
Edited on 30 October 2014 - 01:30 PM
Dragon53535 #15
Posted 30 October 2014 - 06:17 PM
Ahh I see my problem, silly me to assume that Lua had the shorthand mathematical operations.

Spoiler

local function findPeriph(arg1)
  for a,v in pairs({"top","bottom","left","right","back","front"}) do
		if peripheral.getType(v) == arg1 then
		  return v
		end
  end
  error("Cannot find " .. arg1 .. " connected to computer")
end
local modem = peripheral.wrap(findPeriph("modem"))
local mon = peripheral.wrap(findPeriph("monitor"))
local maxturbines = 0
mon.clear()
mon.setTextScale(1)
mon.setBackgroundColour(colors.black)
local function printConnected(arg1,arg2,count)
  local s = 3
  --#in this case arg1 is what we're looking for, and arg2 is the x position we're printing to
  for a,v in pairs(modem.getNamesRemote()) do --#It grabs the names of all remotely attached peripherals.
		--#Just like you did at the top of your code manually.
		if peripheral.getType(v) == arg1 then --#Peripheral.getType gets what it is from the name.
		  --#So a monitor is monitor modem is modem and so on.
		  if count then --#If we should keep count. (I don't use this for getting the reactors)
				maxturbines = maxturbines + 1 --#Add one to the max
		  end
		  mon.setCursorPos(arg2,s)--#Set it in the correct spot
		  local active = peripheral.call(v,"getActive")
		  if active then --#If it's active.
				mon.setBackgroundColor(colors.gray)
				mon.write(" ONLINE ")
		  elseif not active then --#If it's not active
				mon.setBackgroundColor(colors.red)
				mon.write(" OFFLINE ")
		  end
		  mon.setBackgroundColor(colors.black)
		  s = s + 1 --Add one to the line so we go to the next
		end
  end
end
printConnected("BigReactors-Turbine",14,true)
printConnected("BigReactors-Reactor",22)
mon.setCursorPos(1,1)
mon.write("   Global Turbine Overview")
mon.setCursorPos(1,2)
mon.write("   -----------------------")
for num = 1, maxturbines do --#Starting at one and ending at the amount connected, count up one
  mon.setCursorPos(1,num+2) --#Set the cursor to the correct spot
  mon.write("Turbine #"..string.format("%02d",num)) --# %02d says add a 0 if a number is less than 2 digits
  --#Print it out, I'm using string.format to make sure it has a digit in the tens place
end
There i fixed it
Edited on 30 October 2014 - 06:09 PM
DjTranceFire #16
Posted 30 October 2014 - 06:28 PM
startup:39: attempt to call nil

But i cant find the problem on line 39? o.O
Does lua count on comment lines or not?
Dragon53535 #17
Posted 30 October 2014 - 07:10 PM
Spelling mistake on my part, i mispelled the function declaration so when i tried to call it, i was using a different spelling, i corrected it in the post.
Bomb Bloke #18
Posted 30 October 2014 - 07:18 PM
In the case of my code, I'd guess it's getting the peripherals in the wrong order. In which case, changing this:

for key, value in pairs(peripheral.getNames()) do
        if peripheral.getType(value) == "BigReactors-Turbine" then     turb[#turb+1] = peripheral.wrap(value).getActive
        elseif peripheral.getType(value) == "BigReactors-Reactor" then reac[#reac+1] = peripheral.wrap(value).getActive
        elseif peripheral.getType(value) == "monitor" then             mon = peripheral.wrap(value) end
end

… to this:

for i=1,10 do
	turb[i] = peripheral.wrap("BigReactors-Turbine_"..tostring(i)).getActive
	reac[i] = peripheral.wrap("BigReactors-Reactor_"..tostring(i)).getActive
end
mon = peripheral.wrap("bottom")

… may sort it.
DjTranceFire #19
Posted 30 October 2014 - 08:47 PM
Spelling mistake on my part, i mispelled the function declaration so when i tried to call it, i was using a different spelling, i corrected it in the post.

Seems like there are any other problems too.. :x

http://s14.directupl...30/mpinn2e6.png




In the case of my code, I'd guess it's getting the peripherals in the wrong order. In which case, changing this:

for key, value in pairs(peripheral.getNames()) do
		if peripheral.getType(value) == "BigReactors-Turbine" then	 turb[#turb+1] = peripheral.wrap(value).getActive
		elseif peripheral.getType(value) == "BigReactors-Reactor" then reac[#reac+1] = peripheral.wrap(value).getActive
		elseif peripheral.getType(value) == "monitor" then			 mon = peripheral.wrap(value) end
end

… to this:

for i=1,10 do
	turb[i] = peripheral.wrap("BigReactors-Turbine_"..tostring(i)).getActive
	reac[i] = peripheral.wrap("BigReactors-Reactor_"..tostring(i)).getActive
end
mon = peripheral.wrap("bottom")

… may sort it.


With this changes it actually works fine! :)/>
Spoiler

local turb, reac, mon = {}, {}
local function monPrint(text,x,y,col)
		local curX, curY = mon.getCursorPos()
		x, y = x or curX, y or curY  -- If x or y weren't specified, set them to current screen co-ords.
		if x ~= curX or y ~= curY then mon.setCursorPos(x,y) end  -- Move the cursor if it isn't where specified.
		if col then mon.setBackgroundColour(col) end  -- Change the background colour, if specified.
		mon.write(text)
		mon.setCursorPos(1,y+1)  -- Move down a line.
end
for i=1,10 do
		turb[i] = peripheral.wrap("BigReactors-Turbine_"..tostring(i)).getActive
		reac[i] = peripheral.wrap("BigReactors-Reactor_"..tostring(i)).getActive
end
mon = peripheral.wrap("bottom")
local xSize, ySize = mon.getSize()
mon.setBackgroundColour(colors.black)
mon.clear()
mon.setTextScale(1)
monPrint("Global Turbine Overview",3,1)
monPrint("-----------------------",3)
for i=1,9 do monPrint("Turbine #0"..tostring(i)) end
monPrint("Turbine #10")
while true do  -- A loop that repeats indefinitely (bearing in mind Ctrl + T terminates scripts).
		for i = 1,10 do
				local tOn, rOn = turb[i](), reac[i]()  -- Slow, so do it once then record the result.
				monPrint(string.rep(" ", xSize - 13), 14, 2+i, colors.black)  -- Partial term.clearLine() effect.			  
				if tOn == rOn then
						monPrint(" ONLINE ",  14, 2+i, tOn and colors.green or colors.gray)
						monPrint(" OFFLINE ", 22, 2+i, rOn and colors.gray or colors.red)
				else monPrint("	 IDLING		 ", 14, 2+i, colors.yellow) end
		end
	  
		sleep(5)
end
Just had to delete some spaces from the "idling" part but thats just a cosmetically part.. :D/>

EDIT:
I changed this:
Spoiler

for i=1,9 do monPrint("Turbine #0"..tostring(i)) end
monPrint("Turbine #10")

to this:
Spoiler

local maxturbines = 10
for num = 1, maxturbines do --#Starting at one and ending at the amount connected, count up one
  mon.setCursorPos(1,num+2) --#Set the cursor to the correct spot
  mon.write("Turbine #"..string.format("%02d",num)) --# %02d says add a 0 if a number is less than 2 digits
  --#Print it out, I'm using string.format to make sure it has a digit in the tens place
end

I thought it would be a nice way after i viewed the script from dragon again.
Is this correct? Atleast its working but i'm not sure about it.. :D/>
So after all, this my the script i'm currently using to expand it.
Edited on 30 October 2014 - 08:02 PM
Dragon53535 #20
Posted 30 October 2014 - 09:17 PM
Yeah that will work. I made my version as expandable as i could. However that does bring about flaws, however that is the correct way to go about doing so.