wrapping peripherals with arrays

Started by FurryJacklyn, Dec 16, 2020, 02:21 AM

Previous topic - Next topic

FurryJacklyn

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

Lupus590

#1
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

FurryJacklyn

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

FurryJacklyn

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

Lupus590

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")

FurryJacklyn

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

Lupus590

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.

FurryJacklyn

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

Lupus590

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.

QuickMuffin8782

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
GOTTA GO FAST!!!! - Sunrise Studios now calling testers! Check the post!
Gitlab - Join my official discord! - SS Discord

FurryJacklyn

I got everything now, thanks guys. doing some testing with it now