Jump to content




Wrapping a table (Peripherals)


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

#1 ShadowDisruptor

  • Members
  • 74 posts

Posted 17 September 2014 - 12: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[i])
end

(Note - goodReactors is a table including available connections. Example: BigReactors-Reactors_x) Is there any way to do this?

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 September 2014 - 01: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 by KingofGamesYami, 17 September 2014 - 01:54 AM.


#3 Bomb Bloke

    Hobbyist Coder

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

Posted 17 September 2014 - 02: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.

View PostShadowDisruptor, on 17 September 2014 - 12:28 AM, said:

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.

#4 ShadowDisruptor

  • Members
  • 74 posts

Posted 17 September 2014 - 02:20 AM

View PostBomb Bloke, on 17 September 2014 - 02:02 AM, said:

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.

View PostShadowDisruptor, on 17 September 2014 - 12:28 AM, said:

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 by ShadowDisruptor, 17 September 2014 - 02:33 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users