Jump to content




counting peripherals


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

#1 Zelman89

  • Members
  • 20 posts

Posted 15 June 2013 - 05:05 PM

Title: counting peripherals

I am working on part of a program that can detect a specific peripheral an create 3 variables to use in a sperate program. Here is what I have.

for k,v in pairs(redstone.getSides()) do
  if peripheral.getType(v) == "monitor" then
  print(v)
  print(#v)
  end
end

It prints the sides i have monitors on (ex. top) and i get ture or false following them that I don't need. I am stumped on how to have this create a variable for each side so I can wrap them sperately because i want diferent text on two different monitors and need two different varables for .write. would something like x, y = v work? (still new at this). I also need to make a third varable that i can use for how many monitors are connect in case i want to be crazy with a third monitor.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 15 June 2013 - 07:36 PM

Split into new topic.

#3 Bomb Bloke

    Hobbyist Coder

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

Posted 15 June 2013 - 08:07 PM

I guess something like this?

monCount = 0
mon = {}  -- Create a table to put our wrapped monitors into.

for k,v in pairs(redstone.getSides()) do
  if peripheral.getType(v) == "monitor" then
    monCount = monCount + 1
    mon[v] = peripheral.wrap(v)   -- Wrap the table key named after the current side to the monitor on that side.
  end
end

So say you had a monitor to the right, after this code executes, you could write to it with:

mon["right"].write("Hello")


#4 Zelman89

  • Members
  • 20 posts

Posted 15 June 2013 - 10:20 PM

Bomb, this is very helpful in my learning but I have further refined my program and decided that I will always have two monitors but don't know on what sides. This is the code, I don't get an error but I don't get left and right as an answer.
for k,v in pairs(redstone.getSides()) do
  if peripheral.getType(v) == "monitor" then
  return v
  end
  reader1, reader2 = tostring(v)
  print(reader1.."   "..reader2)
end

I get nil after i run the program. I am trying to use rs.getside() and peripheral.getType() to generate a table that has sides and what peripheral is attached to them. I can't figure out how to break it down into two different variables. If I have a monitor on both the left and the right, I want reader1 = left and reader2 = right. Any ideas?

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 15 June 2013 - 10:26 PM

something like this would work

local function findMonitors()
  local s = {}
  for _,v in pairs(rs.getSides()) do
    if #s == 2 then break end
    if peripheral.getType(v) == "monitor" then
      table.insert(s, v)
    end
  end
  return unpack( s )
end

local reader1, reader2 = findMonitors()

if there are 2 monitors it will return their sides into reader1 and reader2.
if there is 1 monitor, reader1 will contain its side and reader2 will be nil
if there are more than 2 monitors reader1 will contain the side that appears first, reader2 will contain the side that appears second.

this is the order
"top", "bottom", "front", "back", "left", "right"

#6 Zelman89

  • Members
  • 20 posts

Posted 15 June 2013 - 11:14 PM

Awesome! I will have to try this out, thanks

#7 Zelman89

  • Members
  • 20 posts

Posted 16 June 2013 - 11:34 AM

theoriginalbit, you code is awesome but it doesn't work if I try to put the first part into a custom api. Am I missing something?
I also had a custom error message which see it cause some issues too...

API "zDrive"
--Set up two peripherals
function findTperipherals(p)
s = {}
  for _,v in pairs(rs.getSides()) do
	if #s == 2 then break end
	if peripheral.getType(v) == p then
	  table.insert(s, v)
	end
  end
  return unpack( s )
end
function wrapTperipherals(j, l, k, m)
if k ~= nil then
  MO = 2
  l = peripheral.wrap(j)
  advMon(l, j)
  m = peripheral.wrap(k)
  advMon(m, k)
elseif j ~= nil then
  MO = 1
  l = peripheral.wrap(j)
  advMon(l, j)
  notes("IMPORTANT", "With a second advanced monitor","you will see more information.")
  sleep(3)
else
  niceError("No advanced monitors attached to the computer","")
  return
end
end

--Header function
function header(te)
  term.clear()
  term.setCursorPos(1, 1)
  term.setTextColor(colors.cyan)
  cprint("---------========= "..te.." =========---------")
  term.setTextColor(colors.white)
end

--Error Messages
function niceError(Ea, Eb)
  header("Error!")
  term.setCursorPos(1, 3)
  cprint(Ea)
  cprint(Eb)
  local x, y = term.getCursorPos()
	if Eb == "" then
	y2 = y
	elseif Eb ~= "" then
	y2 = y + 1
	end
  term.setCursorPos(1, y2)
  term.setTextColor(colors.cyan)
  cprint("---------========= Error! =========---------")
  term.setTextColor(colors.white)
  error()
end

--Center text print
function cprint (t)
  local x2, y2 = term.getCursorPos()
  local x, y = term.getSize()
  term.setCursorPos(math.ceil((x/2) - (t:len()/2)), y2)
  print(t)
end

--Notes
function notes(te, ta, tb)
  term.clear()
  term.setCursorPos(1, 1)
  term.setTextColor(colors.cyan)
  cprint("---------========= "..te.." =========---------")
  term.setCursorPos(1, 3)
  term.setTextColor(colors.white)
  cprint(ta)
  cprint(tb)
  term.setCursorPos(1, 6)
  term.setTextColor(colors.cyan)
  cprint("---------========= "..te.." =========---------")
  term.setCursorPos(1, 8)
  term.setTextColor(colors.white)
end

My program:
--Set up two peripherals
MO = 0

Moni1, Moni2 = zDrive.findTperipherals("monitors")

--Moni1 holds side info and mon1 is variable to write with
zDrive.wrapTperipherals(Moni1, mon1, Moni2, mon2)

mon1.write("This is Monitor 1!")
mon2.write("This is Monitor 2!")
print("There is "..MO.."/2 monitors connected.")


#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 June 2013 - 11:42 AM

this
Moni1, Moni2 = zDrive.findTperipherals("monitors")

should be
Moni1, Moni2 = zDrive.findTperipherals("monitor")

other than that there should be no problems.

EDIT: oh wait. the `wrapTperipherals` function won't work how you want it to. make it so it works like this
mon1, mon2 = zDrive.wrapTperipherals(Moni1, Moni2)

alternatively, just as a side note you could also do this
mon1, mon2 = zDrive.wrapTperipherals(zDrive.findTperipherals("monitors"))

if you didn't need to use the Moni1 and Moni2 variables ever again.

Edited by theoriginalbit, 16 June 2013 - 11:44 AM.


#9 Zelman89

  • Members
  • 20 posts

Posted 16 June 2013 - 12:06 PM

made those changes and getting "attempt to index ? (a nil vaule)" on line 4
Moni1, Moni2 = zDrive.findTperipherals("monitor")

seems solid code to me, is a variable not able to pass to the API for some reason?
here is the fuction again for ref:
function findTperipherals(p)
s = {}
  for _,v in pairs(rs.getSides()) do
	    if #s == 2 then break end
	    if peripheral.getType(v) == p then
		  table.insert(s, v)
	    end
  end
  return unpack( s )
end

Edited by Zelman89, 16 June 2013 - 05:41 PM.


#10 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 June 2013 - 11:51 PM

you haven't loaded the other file as an API, so the `zDrive` table doesn't exist, hence you cannot call the `findTperipherals` function on it.

add this to the top of you main code
os.loadAPI("zDrive")


#11 Zelman89

  • Members
  • 20 posts

Posted 17 June 2013 - 01:35 AM

Sorry left that out but I do have it loaded. This is part of a much bigger code and posted just a section.

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 June 2013 - 01:44 AM

is it possible that you can upload all the script via pastebin? and post the exact error message. you may be overriding it somewhere.

#13 Zelman89

  • Members
  • 20 posts

Posted 17 June 2013 - 07:24 PM

Okay so I spent a little more time on this issue and tried to rewrite it. I hoped in rewriting it I could find my flaw. Here the info you will need. My guess is I am not geting the values returned out of my second function.

Error: test3:12: attempt to index ? (a nil value)

Program: http://pastebin.com/EM8Xw47y

API: http://pastebin.com/LUDBC9i1

#14 Bomb Bloke

    Hobbyist Coder

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

Posted 17 June 2013 - 08:23 PM

You're trying to put the results of "tDrive.wrapTperipherals(Moni1, Moni2)" into a pair of variables, but that function doesn't return any results (unlike the "findTperipherals" function, which does). Hence mon1 & mon2 get set to nil, leading to the error when you try to do something with them.

In short, you need to re-write the wrapping function if you want to use it that way.

I still recommend putting the wrapped peripherals into a table. It's a lot easier then coding functions to accept and return a specific amount of separate variables. Here's an adoption of my first code snippet that might make the concept easier for you.

local function getMonitors()
  local tempmon = {}  -- Create a table to put our wrapped monitors into.

  for k,v in pairs(redstone.getSides()) do
    if peripheral.getType(v) == "monitor" then
      tempmon[#tempmon+1] = peripheral.wrap(v)   -- Wrap a numbered table key to the monitor on the currently checked side.
    end
  end

  return tempmon
end

local mon = getMonitors()
mon[1].write("This is monitor 1!")
mon[2].write("This is monitor 2!")
print("I have "..(#mon).." displays attached.")


#15 Zelman89

  • Members
  • 20 posts

Posted 17 June 2013 - 09:48 PM

Shame I couldn't pass things the way I wanted.. I mixed and matched to something I like. Thank you all for the help. When I am done, I will share.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users