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

Question regarding component.proxy()

Question

I wasn't sure this belonged in the normal question areas being this was more of a general curiosity than it was useful info, so I came here.
But enough beating around the bush. My question is:
Why, when using

Quote

component.proxy(component.list("redstone") () ).getInput(sides.north)

do you need to add in the orange (colored for convenience) parentheses?
I'm sure there's a bit of obviousness I'm probably missing, with my patchwork amount of programming knowledge. :P

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 1
  • Solution

component.list returns an iterator. It's a function that returns a new value when it's called. The iteration stops when the iterator returns nil. Example:

local component = require("component")

local iter = component.list("screen")

local addr1 = iter()
local addr2 = iter()

If only one screen is connected, the value of addr1 will be the address of that screen, and the value of addr2 will be nil. If you connect another screen, addr1 will contain the address of one of two screens, and addr2 will contain the address of another screen.

 

Iterators are really useful when you're using the for ... in loop. Here's an example of such loop.

for key, value in pairs(table) do
  print(key, value)
end

pairs() returns an interator. When the iterator is called the first time, it will choose some key/value pair and return two values: the key and the value. Next time you call it, the iterator returns another pair. And so on.

The key, value part that comes before in is the list of variables the iterator return values are assigned to. It's like running the following line inside each loop iteration:

local key, value = iterator()

(iterator is the function returned by pairs)

Here's how you can rewrite the for ... in loop:

local iterator = pairs(table)
while true do
  local key, value = iterator()
  if key == nil then
    break
  end
  print(key, value)
end

 

I said the iterator returned by component.list is useful when using the for ... in loop. Hopefully you've guessed why.

It makes it easier to iterate over the addresses of connected components. In the following example I turn off every screen connected to the computer running that code.

local component = require("component")

for address in component.list("screen") do
  component.invoke(address, "turnOff")
end

 

Now I'm going to answer the question you asked. Let's find out what the code you gave does step-by-step.

  1. component.proxy(component.list("redstone")()).getInput(sides.north)
    The selected code will return an iterator that returns the address of one of the connected redstone components when called.
  2. component.proxy(component.list("redstone")()).getInput(sides.north)
    Then we call the iterator. It will return the address of one of the connected redstone components.
  3. component.proxy(component.list("redstone")()).getInput(sides.north)
    Now we use that address as an argument to the component.proxy function. It returns the component proxy, the table of all methods and values provided by component.
  4. component.proxy(component.list("redstone")()).getInput(sides.north)
    And then we call the getInput method of the component with the argument sides.north.

By the way, if you're coding under OpenOS, component.proxy(component.list("redstone")()) is the same as component.redstone. That makes your code way more readable.

Link to post
Share on other sites
  • 0
Quote

2. component.proxy(component.list("redstone")()).getInput(sides.north)
    Then we call the iterator. It will return the address of one of the connected redstone components.

I see! I didn't really understand that you were calling the iterator at first, but now it makes sense. Thanks!

Also thank you for the advice, I knew already. :)
I kinda just grabbed the example from the component API page on the wiki. :P

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

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