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

Should be easy: iterate through components in table and do something with them

Question

Hey, Some background first. Minecraft=not newbie; Programming=not newbie; OC = newbie; Lua=newbie

I'm trying to do something that should be relatively simple: turn on a bunch of redstone i/o blocks. 48 in total. I don't want to define these individually, so I thought I could use a table. I tried Molinko's answer here and successfully inserted the ID's in a table. Now, I want to iterate through the table and setOutput for each one.

I am banging my head on a wall. I have tried many different things, but am unable to figure out how to do this without defining a variable for each one.

I have something like:

local component = require("component")
local sides = require("sides")

local rst = {}

for address in component.list('redstone') do
  local r = component.proxy(address)
  table.insert(rst,r)
end

for k,v in pairs(rst) do
  //what do I put here to turn on output for all redstone i/o? assume side is up.
end

I have confirmed that rst has...something?...in it by printing the k/v values, but I also notice that the values don't match the IDs of the blocks. Am I even doing that part right?

Thank you.

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

What you seem to be collecting in your 'rst' table is component proxies not addresses.. which is ok but not really necessary. Try this..

local component = require("component")
local sides = require("sides")

local rst = {}

for address in component.list('redstone') do
  table.insert(rst, address)
end

for i, address in ipairs(rst) do
  -- same as calling from a proxy but you dont have to store a table..
  -- i.e redstone.setOutput(sides.up, 15)
  component.invoke(address, 'setOutput', sides.up, 15)
end

 

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.