ComputerCraft and Monitoring (and some RedNet stuff)

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
Hey guys, I asked a question on here the other day and got a really good answer very quickly, so I thought I'd ask another question about some other stuff!

I've seen some let's plays and some other Youtube videos that people have ComputerCraft setups that allow them to monitor they're bases' power systems and I think it's awesome. I've currently got a couple of MFSUs and nuclear reactors monitored with Nuclear Control's advanced panels but they're limited and have no controlling mechanisms (as far as I'm aware).

What I'd really love to do is to monitor my entire MFSU room - in each base - and be able to control my Nuclear Reactors based on what amounts of power they have. I'd also like to integrate into that, my BC power systems which includes a number of redstone energy cells.

I have limited programming knowledge (I'm studying a degree in Computing and only one module is programming) so I don't really understand all the overly complicated systems that people have, but I seem to be able to learn programming pretty quickly.

Anyways, all the tutorials and such that I've found online show you how to use the basic peripherals that come with the game and I can't find any tutorials that show me all these things.

So, my questions are:
How do I access the information on these different blocks?
How do I use ComputerCraft to control the RedNet cables and thus switch on/off the reactors?
How do I make a display showing total EUs and MJs stored and possibly even a GUI that allows me to manually override controls.

Thanks for help in advance!

(I should probably have posted this on the CC forum, but since it's part of FTB and controlling non-CC things, I thought that here suited best)

EDIT: I'm also curious about the turtles and how to set them up to mine a huge amount of materials in a group together.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Hi,

unfortunately I don't know what modpack you are running but I am fairly confident that if it is anything that FTB put out since 1.5 you have open peripherals.

There are a few APIs that you will need to take a look at from Vanilla Computercraft to start doing what you want.They are:

peripheral - the peripheral.wrap method is needed for your computer to be able to talk to most peripherals, including monitors and MFSUs
redstone - you can control rednet cable using the methods with "bundled" in the name
term - this has a few methods which will enable you to write stuff onto the monitor.*

*When writing to the console you can just code

Code:
print()
or
write()
or 
term.write()

When writing to a monitor (or doing other things to a monitor) you need to remember the name of the variable to which you wrapped the monitor. For the sake of argument I am going to assume it is monitor. For most of the methods in the term api replace the word term with monitor and then you can manipulate the monitor.

If you want to use color on your monitor make sure to use advanced versions.

You can find a list of all the basic Computercraft apis here - http://www.computercraft.info/wiki/Category:APIs
Don't be confused by the rednet one. In case you weren't already aware it is referring to computercrafts modem communication system.

The mod Openperipherals allows your computers to interact at a closer level with many different blocks including modded and vanilla.

http://www.openperipheral.info/openperipheral/documentation/ic2

That link will take you to the ic2 peripheral documentation where you should find methods to get information on the eu storage. You need to wrap the MFSU as a peripheral before you can do any of this. If you can't, or just don't want to, attach your peripherals directly to your computer then you can do it with wired modems. Simply place a wired modem onto your peripheral and right click it. It should turn red and there will be a chat message on your screen saying that peripheral x is connected. The name of the peripheral is important because you will use that string in lieu of a side for your peripheral.wrap() command.

You also need a wired modem on your computer and it all needs to be connected by the cable (I forget what its name is but there is only one in computercraft so pick that one).

Open peripherals also works with Thermal Expansion and Buildcraft so check the documentation out for those too. They are all on the same website as the IC2 ones.
 

rhn

Too Much Free Time
Nov 11, 2013
5,706
4,420
333
Ehm using CC for this is way to complicated if you have access to the Nuclear Control mod. It adds Industrial information screens and energy sensor kits to let you monitor both EU storage and Redstone Energy cells. It also adds the Range Trigger that can output redstone signals depending on the energy level in in the monitored storage device.

Examples:
Monitoring:
6fjgaoG.png


Regulation of engines with Range Triggers:
1WtQKmV.png


Regulation of MFSU for Geothermals to only run when solars isn't enough:
XKdxsdm.png
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
Hi,

unfortunately I don't know what modpack you are running but I am fairly confident that if it is anything that FTB put out since 1.5 you have open peripherals.

There are a few APIs that you will need to take a look at from Vanilla Computercraft to start doing what you want.They are:

peripheral - the peripheral.wrap method is needed for your computer to be able to talk to most peripherals, including monitors and MFSUs
redstone - you can control rednet cable using the methods with "bundled" in the name
term - this has a few methods which will enable you to write stuff onto the monitor.*

*When writing to the console you can just code

Code:
print()
or
write()
or
term.write()

When writing to a monitor (or doing other things to a monitor) you need to remember the name of the variable to which you wrapped the monitor. For the sake of argument I am going to assume it is monitor. For most of the methods in the term api replace the word term with monitor and then you can manipulate the monitor.

If you want to use color on your monitor make sure to use advanced versions.

You can find a list of all the basic Computercraft apis here - http://www.computercraft.info/wiki/Category:APIs
Don't be confused by the rednet one. In case you weren't already aware it is referring to computercrafts modem communication system.

The mod Openperipherals allows your computers to interact at a closer level with many different blocks including modded and vanilla.

http://www.openperipheral.info/openperipheral/documentation/ic2

That link will take you to the ic2 peripheral documentation where you should find methods to get information on the eu storage. You need to wrap the MFSU as a peripheral before you can do any of this. If you can't, or just don't want to, attach your peripherals directly to your computer then you can do it with wired modems. Simply place a wired modem onto your peripheral and right click it. It should turn red and there will be a chat message on your screen saying that peripheral x is connected. The name of the peripheral is important because you will use that string in lieu of a side for your peripheral.wrap() command.

You also need a wired modem on your computer and it all needs to be connected by the cable (I forget what its name is but there is only one in computercraft so pick that one).

Open peripherals also works with Thermal Expansion and Buildcraft so check the documentation out for those too. They are all on the same website as the IC2 ones.


Thanks!

I should've said that I was using Unleashed but I forgot. So to monitor, let's say 10 MFSUs, I'll need 11 wired modems, a bunch of CC cable and a computer, the modems attached to each of the MFSUs and one on the computer, connected by said cables and then from that computer, I can access the storage numbers of all of the MFSUs and use them as variables to control a Rednet signal?


Ehm using CC for this is way to complicated if you have access to the Nuclear Control mod. It adds Industrial information screens and energy sensor kits to let you monitor both EU storage and Redstone Energy cells. It also adds the Range Trigger that can output redstone signals depending on the energy level in in the monitored storage device.

Examples:
Monitoring:
-snip-

Regulation of engines with Range Triggers:
-snip-

Regulation of MFSU for Geothermals to only run when solars isn't enough:
-snip-

I was unaware that you could do that with Nuclear Control as I didn't know about the Range Trigger. It's still limited to only 6 MFSUs per energy array card though and I'd like access to my entire storage room (something like 200 MFSUs) in a single number, not separated into many screens each with only 3 cards. I'd also enjoy using CC to learn a little more programming as well and is one of the main reasons I want to use it.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
Thanks!

I should've said that I was using Unleashed but I forgot. So to monitor, let's say 10 MFSUs, I'll need 11 wired modems, a bunch of CC cable and a computer, the modems attached to each of the MFSUs and one on the computer, connected by said cables and then from that computer, I can access the storage numbers of all of the MFSUs and use them as variables to control a Rednet signal?

You pretty much have it. Just be sure to right click the wired modem on every peripheral and you should be fine. I don't know if you have to but I always right click the modem on the computer as well just to be sure. Unless if you are planning on having the monitor directly touching the computer you will need a wired modem for that too. Good luck with your coding.
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
You pretty much have it. Just be sure to right click the wired modem on every peripheral and you should be fine. I don't know if you have to but I always right click the modem on the computer as well just to be sure. Unless if you are planning on having the monitor directly touching the computer you will need a wired modem for that too. Good luck with your coding.

Haha! Thanks, yeah. I doing some testing and got it to work with the computer touching but I couldn't figure out how to connect to the MFSU throught he wired modems (aww, why don't they make the old fashioned screechy noise)? Turns out, that all I had to do was to right click the modem on the MFSU. I also automatically right clicked the one on the computer. Well... I shall attempt to have some fun with this coding now and may have to redesign my MFSU room slightly. I should show my server to someone who doesn't know about FTB (and some other mods) and show them that in Minecraft, you can build power stations, computers - to monitor and control things and finally all of that on a space station thanks to Galacticraft. Oh I imagine I'd be bored to death of MC by now if mods didn't exist.... Haha!


EDIT: I can't work Lua.... I've had the most experience in Java which is nothing like Lua. I've done a bit of bash and batch scripting which seems much more like Lua, but still not the same and I can't even figure out how to do a simple for loop to create the peripheral.wrap names for each of them.

I'm trying to put in something that will do the equivalent of:
Code:
for (i = 0; i < 10; i++){
   "mfsu" + i = peripheral.wrap("batbox_" + i)
}
Or whatever would actually work. But that's not the way that Lua works and it's making nothing work. Haha. Any help?
 
Last edited:

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
The mods keep me interested too, and I love controlling all the things with computercraft. Shameless self promotion incoming. I started a thread on community showcase that exhibits some cool computercraft control. It focuses on railcraft but in the attached imgur album there is a small mention of a tank monitoring program I wrote and also I had a bank of dartcraft engines that was computer controlled based on the storage in a Redstone energy cell. The less energy stored in the cell the more engines are made to run. The railcraft stuff is quite interesting too (at least imho) and I reckon you might like it.

If you are interested there is a link below.

http://forum.feed-the-beast.com/threads/railcraft-item-depot-with-some-computercraft-stuff.34348/
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
The mods keep me interested too, and I love controlling all the things with computercraft. Shameless self promotion incoming. I started a thread on community showcase that exhibits some cool computercraft control. It focuses on railcraft but in the attached imgur album there is a small mention of a tank monitoring program I wrote and also I had a bank of dartcraft engines that was computer controlled based on the storage in a Redstone energy cell. The less energy stored in the cell the more engines are made to run. The railcraft stuff is quite interesting too (at least imho) and I reckon you might like it.

If you are interested there is a link below.

http://forum.feed-the-beast.com/threads/railcraft-item-depot-with-some-computercraft-stuff.34348/

Hmm, sounds awesome, I'll just check that out. Currently I have everything just running 24/7 so it's quite possibly wasting power - having said that, I think I've got enough machines running and not enough engines, so it's probably OK just now. With computer control, it'll be much better and I can add more and more machines etc to make things do what I want.

Note: did you see my edit above as you posted around the same time I edited?
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
There are two points of divergence between Lua and Java for what you are trying to do.

To do a for loop you just do this

Code:
x = your starting value
y = ending value
z = increment, can be negative. Defaults to +1 if you leave it blank
for i = x,y,z do
   --some code
end

You concatenate strings differently in Lua too. Instead of using a plus sign use a double period instead, like this

Code:
"hello".." world"

The computercraft wiki has a few helpful pages that explain basic lua concepts.
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
There are two points of divergence between Lua and Java for what you are trying to do.

To do a for loop you just do this

Code:
x = your starting value
y = ending value
z = increment, can be negative. Defaults to +1 if you leave it blank
for i = x,y,z do
   --some code
end

You concatenate strings differently in Lua too. Instead of using a plus sign use a double period instead, like this

Code:
"hello".." world"

The computercraft wiki has a few helpful pages that explain basic lua concepts.


OK, that's exactly what I want to do, but how do I concatenate a variable onto a variable name without it naming the variable incorrectly or just causing an error (if that even makes any sense). I'll try and say that a different way... I have a
Code:
for i = 0,9 do
    mfsu..i = peripheral.wrap("batbox_0"..i)
end

Now that doesn't work because of the "mfsu..i" part. How would I generate 10 variables in that way by concatenating the value of "i" onto the end of it?

I hope you understand that. I did some Googling and checked through the wiki but I can't find anything on that.
 

Bomb Bloke

New Member
Jul 29, 2019
612
0
0
You want a table. Quite similar to arrays:

Code:
local mfsu = {}  -- "mfsu" is now an empty table.

for i = 0,9 do
   mfsu[i+1] = peripheral.wrap("batbox_"..i)
end
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
What you are doing, at least in the way you are trying, doesn't work I am afraid but there is a better solution. I don't know how familiar you are with tables (like arrays in other languages) but you can use them to do what you are doing. I am going to assume that you don't know what a table is and how it works, just so my explanation makes sense. If you do know, feel free to only take what you need.

A table is like a mega variable which can store multiple variables. In Lua, variables within a table can be of different types. Variables within a table have a key which you can use to access them. A key can be an interger or a string. To access the variable in a table you need to know the name of the table (which is easy because often you will have named it yourself) and the name of the key. By default variable keys start at 1 and climb but you can specify them if that is appropriate.

To make a table you just give it a name like any other variable and then you say it equals {}

for example

Code:
local t = {} -- initiates an empty table. Lets put some variables in it
t["hola"] = "hello"
t["mundo"] = "world"

--now we have two variables inside t. the keys are what is inside the square brackets. 
--alternatively we can put variables into the table at the same time as we initiate it using a comma separated list like so

local twilight = {"bella", "+", "edward", 4, "eva"} -- notice that not all the variables are of the same type. Because I did not specify the key names they will have been given integers starting at 1
print(twilight[1])
--prints "bella" to the console
print(twilight[4])
--prints 4 to the console

You can also iterate through the table using a generic for loop which is slightly different from the one you are already familiar with (this is a wee bit more advanced but it will probably help you to know this with the program you are writing right now). You can iterate through key value pairs in a table (key being the key that a mentioned earlier and value being the data stored inside that key) using the generic for loop. Just like you can use i as a variable in the normal for loop you can also capture the key and the value of each part of the table in turn. This is what you need to do

Code:
local data = {["Energy storage"] = 1000000, ["Current Energy Stored"] = 500000, ["Percentage Filled"] = 50
for i,v in pairs(data) do -- i = key and v = value. in pairs() is just what you need to write in Lua so you can iterate through tables. You put the name of the table you want to iterate through in the brackets in in pairs
   print(i..": "..v)
end

--[[
You end up with this printed to the console
Energy storage: 1000000
Current Energy Stored: 500000
Percentage Filled = 50
]]

If you want a for loop to wrap all your MFSUs (which is a good idea and I encourage it, it is more expandable) you can make a table called mfsu and have each key refer to a particular mfsu.

I am putting how I would do it inside a spoiler because it already seems like you can work it out on your own now you know to use tables. But you can always look inside if you want. (btw, you can put tables inside of tables which can come in handy)

Code:
local mfsu = {}
for i = 1,10 do
  mfsu[i] = {[1] = peripheral.wrap("batbox_"..i-1), [2] = "MFSU:"..i}
end

later on to use the methods available in open peripherals such as getStored() you can just do this

local energyStored = mfsu[1][1].getStored() -- The first number in square brackets refers to the key that determines which mfsu you are looking at, the second key will always be 1 because that is where in the sub table I wrapped the peripheral. I hope that makes sense. The reason I made a subtable in the first place was so that I could store the MFSUs name so I can do something like this.

Code:
local mfsuID = 3 -- in this example I am looking at the third mfsu in your array
local energyStored = mfsu[mfsuID][1].getStored()
local mfsuName = mfsu[mfsuID[2]
print("Energy Stored")
print(mfsuName..": "..energyStored)

I could also use a generic for loop to iterate through each mfsu in the array.
In all honesty, if the only extra data you wanted to store was a name you could do it without making a subtable because you can easily generate the name when you are reading the main data. If however you wanted to get more data than from just one method then a subtable can come in handy. Sorry if that makes no sense. However, in that other thread I made that I mentioned earlier I talk a bit about a program I wrote to get details about a group of tanks where I maybe explain myself a bit better, and with better code examples.
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
What you are doing, at least in the way you are trying, doesn't work I am afraid but there is a better solution. I don't know how familiar you are with tables (like arrays in other languages) but you can use them to do what you are doing. I am going to assume that you don't know what a table is and how it works, just so my explanation makes sense. If you do know, feel free to only take what you need.

A table is like a mega variable which can store multiple variables. In Lua, variables within a table can be of different types. Variables within a table have a key which you can use to access them. A key can be an interger or a string. To access the variable in a table you need to know the name of the table (which is easy because often you will have named it yourself) and the name of the key. By default variable keys start at 1 and climb but you can specify them if that is appropriate.

To make a table you just give it a name like any other variable and then you say it equals {}

for example

Code:
local t = {} -- initiates an empty table. Lets put some variables in it
t["hola"] = "hello"
t["mundo"] = "world"

--now we have two variables inside t. the keys are what is inside the square brackets.
--alternatively we can put variables into the table at the same time as we initiate it using a comma separated list like so

local twilight = {"bella", "+", "edward", 4, "eva"} -- notice that not all the variables are of the same type. Because I did not specify the key names they will have been given integers starting at 1
print(twilight[1])
--prints "bella" to the console
print(twilight[4])
--prints 4 to the console

You can also iterate through the table using a generic for loop which is slightly different from the one you are already familiar with (this is a wee bit more advanced but it will probably help you to know this with the program you are writing right now). You can iterate through key value pairs in a table (key being the key that a mentioned earlier and value being the data stored inside that key) using the generic for loop. Just like you can use i as a variable in the normal for loop you can also capture the key and the value of each part of the table in turn. This is what you need to do

Code:
local data = {["Energy storage"] = 1000000, ["Current Energy Stored"] = 500000, ["Percentage Filled"] = 50
for i,v in pairs(data) do -- i = key and v = value. in pairs() is just what you need to write in Lua so you can iterate through tables. You put the name of the table you want to iterate through in the brackets in in pairs
   print(i..": "..v)
end

--[[
You end up with this printed to the console
Energy storage: 1000000
Current Energy Stored: 500000
Percentage Filled = 50
]]

If you want a for loop to wrap all your MFSUs (which is a good idea and I encourage it, it is more expandable) you can make a table called mfsu and have each key refer to a particular mfsu.

I am putting how I would do it inside a spoiler because it already seems like you can work it out on your own now you know to use tables. But you can always look inside if you want. (btw, you can put tables inside of tables which can come in handy)

Code:
local mfsu = {}
for i = 1,10 do
  mfsu[i] = {[1] = peripheral.wrap("batbox_"..i-1), [2] = "MFSU:"..i}
end

later on to use the methods available in open peripherals such as getStored() you can just do this

local energyStored = mfsu[1][1].getStored() -- The first number in square brackets refers to the key that determines which mfsu you are looking at, the second key will always be 1 because that is where in the sub table I wrapped the peripheral. I hope that makes sense. The reason I made a subtable in the first place was so that I could store the MFSUs name so I can do something like this.

Code:
local mfsuID = 3 -- in this example I am looking at the third mfsu in your array
local energyStored = mfsu[mfsuID][1].getStored()
local mfsuName = mfsu[mfsuID[2]
print("Energy Stored")
print(mfsuName..": "..energyStored)

I could also use a generic for loop to iterate through each mfsu in the array.
In all honesty, if the only extra data you wanted to store was a name you could do it without making a subtable because you can easily generate the name when you are reading the main data. If however you wanted to get more data than from just one method then a subtable can come in handy. Sorry if that makes no sense. However, in that other thread I made that I mentioned earlier I talk a bit about a program I wrote to get details about a group of tanks where I maybe explain myself a bit better, and with better code examples.

You want a table. Quite similar to arrays:

Code:
local mfsu = {}  -- "mfsu" is now an empty table.

for i = 0,9 do
   mfsu[i+1] = peripheral.wrap("batbox_"..i)
end


Both of you are telling me exactly what I should've thought of! It's been a while since I've done much programming so I'd basically forgotten about tables and I don't think any of the tutorials I watched spoke about them at all.

I'm a little confused as to exactly what everything's doing in your example, casilleroatr, but it certainly will allow me to get this working and hopefully figure it out from there. What I'm going to want to do is have a program running that monitors the overall storage and if i goes below a certain amount, the reactors will kick in and they'll cut off when it hits a certain percentage filled and such.

Now to do some more testing with my extra knowledge and this as reference, and then on to converting my bases to work with this as I want it to when I've figured it out. I'll post here with any more issues I have or if I manage to succeed!

Thanks for the help so far!
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
Well I think I'm getting somewhere... I had a bit of a brainfart when trying to work out percentages but I just thought about it for a minute and got it. Haha!

Anyways, I now have a system which asks you to enter the number of MFSUs, from that it works out the current, max and percentage filled (sort of) of the totals and then displays it to the terminal. It doesn't have a loop so won't keep updated, though. Would the best and most efficient way to do that to make it sleep for 1 or 2 seconds and then repeat the for loop or would some other way work better? Finally, how do you multiply a number without it breaking? "x" and "*" don't work. It errors out my code before it even runs and says "multiple points".

EDIT: Finally found on the wiki the operators, but it seems that "*" should work.
EDIT2: Well it seems that putting it into a variable first works. Next issue: how do you knock off the digits after the decimal?
 
Last edited:

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
If you don't mind me pointing you to that thread again about half way down the first post I have a program inside a spoiler which contains a rounding function. You put the number that you want to round into the first argument and you put the number of decimal places into the second argument (0 would return a whole number). Its not my idea btw, I got it from a Lua website but I forget which one.

You can also use the following commands

math.ceil(x)
math.floor(x)

where x is the number that you want to floor or ceiling.

I don't quite understand why you are a having a multiplication problem it might be that the error is talking about something else. If you could show me your code then I might be able to tell you what the problem is, unless you already solved it that is.
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
If you don't mind me pointing you to that thread again about half way down the first post I have a program inside a spoiler which contains a rounding function. You put the number that you want to round into the first argument and you put the number of decimal places into the second argument (0 would return a whole number). Its not my idea btw, I got it from a Lua website but I forget which one.

You can also use the following commands

math.ceil(x)
math.floor(x)

where x is the number that you want to floor or ceiling.

I don't quite understand why you are a having a multiplication problem it might be that the error is talking about something else. If you could show me your code then I might be able to tell you what the problem is, unless you already solved it that is.

Of course, math.ceil or floor. I should've known that. It's been too long since I did any programming! Haha.

Here's the current version of my little program. You simply tell it how many MFSUs are in the system and it will (in theory) work out the numbers and display it to you. On my little test world, it seems to work perfectly - except for the lack of rounding.

Code:
local mfsu = {}
local storedEUs = 0
local maxEUs = 0
local numMFSUs = 0
local storePercent = 0

print("Please enter the number of MFSUs in the system --> ")
numMFSUs = read()-1

while true do
  storedEUs = 0
  maxEUs = 0

  for i = 0,numMFSUs do
  mfsu[i+1] = peripheral.wrap("batbox_"..i)
  storedEUs = storedEUs + mfsu[i+1].getStored()
  maxEUs = maxEUs + mfsu[i+1].getCapacity()
  end

  storePercent = storedEUs/maxEUs --This works like this. As: print((storedEUs/maxEUs)*100) or print(storedEUs/maxEUs*100)
  storePercent = storePercent*100 --It comes up with the "multiple points" error.

  term.clear()
  term.setCursorPos(1, 1)
  print(storedEUs.."/"..maxEUs)
  print(storePercent.."%")
  sleep(2)
end

Some time when I've not got too much actual work, I'll sit and play around more with it and make it look fancy etc. I'll be looking up how to make a "progress bar" style meter and such.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
I am having a bit of trouble testing as my version of open peripherals does not seem to work with IC2-exp (I am on 1.6.4). I will look into it and maybe start up a 1.5 instance soon if necessary).
 

Bomb Bloke

New Member
Jul 29, 2019
612
0
0
For your MFSU-wrapping loop, you might consider something like this:

Code:
for a,b in pairs(peripheral.getNames()) do  -- Loop through a table that contains names of ALL connected peripherals.
   if peripheral.getType(b) == "batbox" then  -- If a detected peripheral is a batbox, then...
     mfsu[#mfsu+1] = peripheral.wrap(b)  -- Sticking a # in front of a given variable generally returns the length of that variable. Indexes in the table, in this case.
     storedEUs = storedEUs + mfsu[#mfsu].getStored()
     maxEUs = maxEUs + mfsu[#mfsu].getCapacity()
   end
end

Basically it just grabs all energy storage units hooked up, rather then a user-specified amount.

It may or may not be worth noting that when printing numbers, Lua may or may not print a decimal point even for integers depending on the exact data type. "tostring(numericvariable)" generally avoids this.
 

xBlizzDevious

New Member
Jul 29, 2019
110
0
0
For your MFSU-wrapping loop, you might consider something like this:

Code:
for a,b in pairs(peripheral.getNames()) do  -- Loop through a table that contains names of ALL connected peripherals.
   if peripheral.getType(b) == "batbox" then  -- If a detected peripheral is a batbox, then...
     mfsu[#mfsu+1] = peripheral.wrap(b)  -- Sticking a # in front of a given variable generally returns the length of that variable. Indexes in the table, in this case.
     storedEUs = storedEUs + mfsu[#mfsu].getStored()
     maxEUs = maxEUs + mfsu[#mfsu].getCapacity()
   end
end

Basically it just grabs all energy storage units hooked up, rather then a user-specified amount.

It may or may not be worth noting that when printing numbers, Lua may or may not print a decimal point even for integers depending on the exact data type. "tostring(numericvariable)" generally avoids this.

Hmm... Casilleroatr tried to explain the in pairs thing earlier and I didn't understand it so I just went with the "for i =" way. Could you explain exactly how the for a,b in pairs stuff works? I can understand stuff a lot more if I know how it works.
 

casilleroatr

New Member
Jul 29, 2019
1,360
0
0
The in pairs loop is just what you can use to iterate through a table. It is good because you don't have to know how long the table is, or what the keys are to to get through the whole table. Each point in a table has a stored value which is referred to by a key - together they form a pair. So when you say <for i,v in pairs(t) do> you are saying, "for each pair in table t, return the key to variable i and the value to variable v".

In Bomb Bloke's example (kudos by the way I hadn't though of that) he uses the function peripheral.getNames which returns the table so that you can iterate through it.