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

Wrapping a table (Peripherals)

Started by ShadowDisruptor, 16 September 2014 - 11:28 PM
ShadowDisruptor #1
Posted 17 September 2014 - 01:28 AM
In the program I am writing, I am using the peripherals.getNames to get all connected peripherals. I am using the mod BigReactors which dose not work with peripherals.find, which is why I'm not using it. After my function sorts the correct peripherals into a new table, I wish to wrap them separately. For example, reactor_x (1,2,3) in the order they are in the table. This is how I approached it, but it dosen't work:

for i=1, #goodReactors do
("reactor_"..i) = peripheral.wrap(goodReactors)
end

(Note - goodReactors is a table including available connections. Example: BigReactors-Reactors_x) Is there any way to do this?
KingofGamesYami #2
Posted 17 September 2014 - 02:52 AM
You can't declare a variable with a variable name. You'd be better off simply placing the wrapped object inside another table.

Edit: Well, actually you can if you define it globally, by using _G[ "reactor_" .. i ] or some such, however local variables are accessed faster and it is bad coding practice to pollute the global scope.
Edited on 17 September 2014 - 12:54 AM
Bomb Bloke #3
Posted 17 September 2014 - 03:02 AM
For example, I'd re-write the loop to do this:

for i=1, #goodReactors do
   goodReactors[i] = peripheral.wrap(goodReactors[i])
end

Once it's done, "goodReactors" will contain wrapped peripherals instead of just peripheral names.

I am using the mod BigReactors which dose not work with peripherals.find, which is why I'm not using it.

peripherals.find() works with any peripheral, so long as it's used correctly. Assuming it hasn't changed at some point, you should be passing it a type of "BigReactors-Reactor".

Eg:

local goodReactors = {peripherals.find("BigReactors-Reactor")}

It's even possible to take that a step further and have it make sure the reactors are fully assembled, if need be.
ShadowDisruptor #4
Posted 17 September 2014 - 03:20 AM
For example, I'd re-write the loop to do this:

for i=1, #goodReactors do
   goodReactors[i] = peripheral.wrap(goodReactors[i])
end

Once it's done, "goodReactors" will contain wrapped peripherals instead of just peripheral names.

I am using the mod BigReactors which dose not work with peripherals.find, which is why I'm not using it.

peripherals.find() works with any peripheral, so long as it's used correctly. Assuming it hasn't changed at some point, you should be passing it a type of "BigReactors-Reactor".

Eg:

local goodReactors = {peripherals.find("BigReactors-Reactor")}

It's even possible to take that a step further and have it make sure the reactors are fully assembled, if need be.

Very useful. Thanks for the help. EDIT: The answer to why peripheral.find wasn't working is in bold letters on the API.. "Requires computercraft 1.6 or later" Sadly, the tekkit pack dosen't have this yet. I think your way of wrapping the table will work better in my situation anyways.
Edited on 17 September 2014 - 01:33 AM