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

Open multiple modems + assign variable

Started by lifewcody, 14 February 2015 - 11:35 PM
lifewcody #1
Posted 15 February 2015 - 12:35 AM
This is sort of hard to clarify, so bear with me.

I have a random amount of modems on a computer, I get them using this:

local lSides = redstone.getSides()
local lCSides = {}
for i=1, #lSides do
  if peripheral.isPresent(lSides[i]) and peripheral.getType(lSides[i]) == "modem" then
	table.insert(lCSides, lSides[i])
  end
end

This puts all of the sides modems are connected to in lCSides (ListConnectedSides)

I then try to open 1 - 128 channels on each modem. Problem I have is, I need to assign variables to the modems to transmit.

So if lCSides[1] == "left" then I need to be able to do table[2].transmit(1, 2, "Test)

This is the code I am having difficulty on:


for s=1, #lCSides do
  for c=1, 128 do
	lCSides[s].peripheral.wrap(lCSides[s])
	lCSides[s].open(c)
  end
end
Lignum #2
Posted 15 February 2015 - 12:41 AM
You can use peripheral.find for this:

local modems = { peripheral.find("modem") }
for _,v in pairs(modems) do
   for i=1,128 do
      v.open(i)
   end
end
Bomb Bloke #3
Posted 15 February 2015 - 12:42 AM
Looks like you meant to do:

for s=1, #lCSides do
  lCSides[s] = peripheral.wrap(lCSides[s])

  for c=1, 128 do
        lCSides[s].open(c)
  end
end

peripheral.find() is indeed easier, if you're on CC 1.6 or later.

I do find myself thinking that whatever you're trying to achieve by opening that many channels, there's a way to do it with just one or two.
Edited on 14 February 2015 - 11:43 PM
lifewcody #4
Posted 15 February 2015 - 12:44 AM
Looks like you meant to do:

for s=1, #lCSides do
  lCSides[s] = peripheral.wrap(lCSides[s])

  for c=1, 128 do
		lCSides[s].open(c)
  end
end

peripheral.find() is indeed easier, if you're on CC 1.6 or later.

I do find myself thinking that whatever you're trying to achieve by opening that many channels, there's a way to do it with just one or two.

I'm redesigning my InZernet protocol and it will work with sending / receiving channels

Edit: Your reply worked! Thank you
Edited on 14 February 2015 - 11:46 PM