Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal

Archived

This topic is now archived and is closed to further replies.

  • 0
SusiKette

How to separate multiple components of the same name in code?

Question

I have two Flux Gates (Draconic Evolution) in a setup, but the problem is that I don't know how to separate the two in code. If there was only one I could do something like "local gate = component.flux_gate". It would be easier to write the code if I could give them names separately such as "gate_in" and "gate_out". How do I do this?

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

To start, components dont really have 'names'. Names aren't unique and we end up with the issue you've mentioned. Components have 'types' and 'addresses'. When using the component library fields like component.flux_gate what is actually happening is the component library is looking up the 'primary' component, really this is just the first component of that type to be found.

print( component.flux_gate == component.getPrimary('flux_gate') ) -- # this should print 'true'

To find individual components of a certain type you can call component.list

local flux_gates = {}

for address in component.list('flux_gate') do
  local gate = component.proxy(address)
  table.insert(flux_gates, gate)
end

-- or

local gate_iter = component.list('flux_gate')
local fg1, fg2 = component.proxy(gate_iter()), component.proxy(gate_iter())

 

Share this post


Link to post
Share on other sites

I assume the Flux Points are connected using an adapter, so you can use the Analyzer on said adapters while holding Ctrl to copy each address into the clipboard and then paste it into the script using the following code to get each instance:

local component = require("component")

local fluxPoint1Address = "<ADDRESS 1 GOES HERE>"
local fluxPoint2Address = "<ADDRESS 2 GOES HERE>"

local fluxPoint1 = component.get(fluxPoint1Address)
local fluxPoint2 = component.get(fluxPoint2Address)

-- Do whatever with the flux points

You don't need the address variables, I just place them with other settings / addresses / etc at the top of the file so they are easy to edit if needed (and I don't have to search for them throughout the file).

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.