ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: FurryJacklyn on Dec 16, 2020, 02:21 AM

Title: wrapping peripherals with arrays
Post by: FurryJacklyn on Dec 16, 2020, 02:21 AM
Is it possible to bulk wrap things by saying
local monitors = {
    "monitor_1"
    "monitor_2"
    "monitor_3"
    "monitor_4"
    "monitor_5"
    "monitor_6"
}
if a = 1, 6 do
    local monitor = peripheral.wrap(monitors(a))
end

I want several monitors to display all the same information, they are all the same size and same everything, and I don't want 6 wrap statements and repeating lines 6 times for each monitor, so I want to know if I can just use a for loop and an array to wrap all of the monitors on a network under 1 variable and use "monitor.[function]" and send it to as many monitors I want using 1 line of dot notation
Title: wrapping peripherals with arrays
Post by: Lupus590 on Dec 16, 2020, 04:16 AM
What you want is peripheral.find("monitor"), it returns every monitor as separate variables. putting this into a table.pack makes them a single table of periperals.

local monitors = table.pack(peripheral.find("monitor"))peripheral.find takes a second argument which is a filter function, you could use that to endure that the connected monitors are the right sizes.
https://tweaked.cc/module/peripheral.html#v:find
Title: wrapping peripherals with arrays
Post by: FurryJacklyn on Dec 16, 2020, 12:06 PM
so I'm assuming I have to wrap all the monitors before, then use that to group them all together under a single variable correct? because I read the document you sent and it said it only returns a table of peripherals that are wrapped
Title: wrapping peripherals with arrays
Post by: FurryJacklyn on Dec 16, 2020, 01:29 PM
I tried this method trying to use monitors.write to write to the screen, but all I get is an error saying that write is a nil value, though I know write is valid because I've used it before.
local monitors = table.pack(peripheral.find("monitor"))
monitors.write("hello")
is what I used, it didn't work
Title: wrapping peripherals with arrays
Post by: Lupus590 on Dec 16, 2020, 02:36 PM
This code writes to one monitor. To write to all monitors like that, you will need to make a fake monitor which repeats the function calls on all the monitors in the table.
local monitors = table.pack(peripheral.find("monitor"))
monitors[1].write("hello")
Title: wrapping peripherals with arrays
Post by: FurryJacklyn on Dec 16, 2020, 03:20 PM
I have no idea how to write something like that, I assume a for loop for everytime I want to write to all of them and replace [1] with whatever the variable is in the for loop like;
for i = 1, 6 do
monitors.write("hello")
end
would this technically work? assuming that I'm using the same table.pack method
Title: wrapping peripherals with arrays
Post by: Lupus590 on Dec 16, 2020, 03:45 PM
slight ajustment, you forgot to index the monitors table.
for i = 1, 6 do
  monitors[i].write("hello")
end

You might also want to replace the 6 in the for loop with the count of monitors in the table.
for i = 1, monitors.n or #monitors do
  monitors[i].write("hello")
end
We do monitors.n as table.pack should set that, however just in case we also use #monitors which gets the length too. We prefer monitors.n as it's a quick lookup compared with #monitors which loops through all the values of the table until it finds a nil value (which can take some time).

Another way of doing the immediate above is this.
for _, monitor in ipairs(monitors) do
  monitor.write("hello")
end
So ipairs makes an iterator of the monitors table which the in keyword then uses to generate key value pairs. We discard the key into _ and take the value as monitor (note the missing s). Since the values in monitors are the wrapped peripherals we can use the value as the peripheral.
Title: wrapping peripherals with arrays
Post by: FurryJacklyn on Dec 16, 2020, 03:51 PM
so from what I understand, which isn't much of how the last one works, is that monitor in line 1 will become what I call whenever I want to write something to all the monitors. will I be able to use this outside of the loop by chance or is monitor a local variable meant to stay inside the loop and only in the loop
Title: wrapping peripherals with arrays
Post by: Lupus590 on Dec 16, 2020, 05:52 PM
In the last example, monitor is a variable local to the for loop and will not work outside of it. Monitor as a variable also only writes to one monitor, the for loop reassigns the variable as it loops though, like how i gets reassigned in the first two examples.
Title: wrapping peripherals with arrays
Post by: QuickMuffin8782 on Dec 17, 2020, 12:31 AM
Quote from: Lupus590 on Dec 16, 2020, 05:52 PMIn the last example, monitor is a variable local to the for loop and will not work outside of it. Monitor as a variable also only writes to one monitor, the for loop reassigns the variable as it loops though, like how i gets reassigned in the first two examples.

Like @Lupus590 is saying, this is more like getting all the monitors you need.

The "_" symbol is to make sure that variable isn't defined, and you're not using it, which means the left side of the pair function for the "for" loop, will be part of the table you will not be using.. Here's a demo with the pairs() function.

--< Corresponding table with a and b to indicate each side the variable uses. >--table = {
    ["a1"] = "b1",
    ["a2"] = "b2"
}

for a, b in pairs(table) do
    print(a .. " = " ..  b) -- Print all sides with "a" being the left and "b" being the right.
end
Title: wrapping peripherals with arrays
Post by: FurryJacklyn on Dec 17, 2020, 02:48 AM
I got everything now, thanks guys. doing some testing with it now