134 posts
Posted 23 March 2016 - 12:31 AM
Hey Pros!
so.. i got 8 MFSU's that i would like to get the total amount stored of.. but i keep getting this error..
Pastebin:
chTCBcJARaw code:
Spoiler
one = peripheral.wrap("mfsu_1")
two = peripheral.wrap("mfsu_2")
thr = peripheral.wrap("mfsu_3")
fou = peripheral.wrap("mfsu_4")
fiv = peripheral.wrap("mfsu_5")
six = peripheral.wrap("mfsu_6")
sev = peripheral.wrap("mfsu_7")
eig = peripheral.wrap("mfsu_8")
local function all()
one.getEUStored() +
two.getEUStored() +
thr.getEUStored() +
fou.getEUStored() +
fiv.getEUStored() +
six.getEUStored() +
sev.getEUStored() +
eig.getEUStored()
end
while true do
capacity = one.getEUCapacity()*8
st = all()
term.clear()
term.setCursorPos(1,1)
print("Stored: ".. st .." / ".. capacity)
sleep(2)
end
Hope you can help!
//Plazter
Edited on 23 March 2016 - 04:26 PM
3057 posts
Location
United States of America
Posted 23 March 2016 - 12:54 AM
It's complaining because you're adding without returning the value or assigning it to a variable.
Based on usage, you want all() to return the all of the stored EU added together.
local function all()
return one.getEUStored() +
two.getEUStored() +
thr.getEUStored() +
fou.getEUStored() +
fiv.getEUStored() +
six.getEUStored() +
sev.getEUStored() +
eig.getEUStored()
end
The formatting is a bit weird, but it is perfectly valid.
How I'd do it
local tMFSU = {} --#make a table for the peripherals
for i = 1, 8 do --#wrap them in a loop
tMFSU[ i ] = peripheral.wrap( "mfsu_" .. i )
end
local function all() --#make a function which loops and returns the value
local amount = 0
for i, v in ipairs( tMFSU ) do
amount = amount + v.getEUStored()
end
return amount
end
local capacity = tMFSU[ 1 ].getEUCapacity() * 8 --#Capacity doesn't change, so we get the capacity outside the loop
while true do
term.clear()
term.setCursorPos( 1, 1 )
print( "Stored: ".. all() .." / ".. capacity ) --#I don't need to assign a variable, I can use the return value directly.
sleep(2)
end
Edited on 23 March 2016 - 12:00 AM
1220 posts
Location
Earth orbit
Posted 23 March 2016 - 12:55 AM
The problem is in your all function. You aren't returning the value so it can be captured by your st variable. Try this…
local function all()
return one.getEUStored() + two.getEUStored() + thr.getEUStored() + fou.getEUStored() + fiv.getEUStored() + six.getEUStored() + sev.getEUStored() + eig.getEUStored()
end
I don't have a way to test atm, but the above *should* work.
EDIT: :ph34r:/> 'd
Edited on 22 March 2016 - 11:56 PM
134 posts
Posted 23 March 2016 - 02:15 AM
It's complaining because you're adding without returning the value or assigning it to a variable.
Based on usage, you want all() to return the all of the stored EU added together.
local function all()
return one.getEUStored() +
two.getEUStored() +
thr.getEUStored() +
fou.getEUStored() +
fiv.getEUStored() +
six.getEUStored() +
sev.getEUStored() +
eig.getEUStored()
end
The formatting is a bit weird, but it is perfectly valid.
How I'd do it
local tMFSU = {} --#make a table for the peripherals
for i = 1, 8 do --#wrap them in a loop
tMFSU[ i ] = peripheral.wrap( "mfsu_" .. i )
end
local function all() --#make a function which loops and returns the value
local amount = 0
for i, v in ipairs( tMFSU ) do
amount = amount + v.getEUStored()
end
return amount
end
local capacity = tMFSU[ 1 ].getEUCapacity() * 8 --#Capacity doesn't change, so we get the capacity outside the loop
while true do
term.clear()
term.setCursorPos( 1, 1 )
print( "Stored: ".. all() .." / ".. capacity ) --#I don't need to assign a variable, I can use the return value directly.
sleep(2)
end
didnt think of doing that wrap trick, nice one :P/>, i forgot most of those tables and things :P/> just returned to CC :D/>
Thank for the reply!
The problem is in your all function. You aren't returning the value so it can be captured by your st variable. Try this…
local function all()
return one.getEUStored() + two.getEUStored() + thr.getEUStored() + fou.getEUStored() + fiv.getEUStored() + six.getEUStored() + sev.getEUStored() + eig.getEUStored()
end
I don't have a way to test atm, but the above *should* work.
EDIT: :ph34r:/> 'd
Be my guest!
134 posts
Posted 23 March 2016 - 02:22 AM
for i, v in ipairs ( tMFSU) do
local amount = 0
amount = amount + v.getEUStored()
end
return amount
end
where and what is that V from?
1080 posts
Location
In the Matrix
Posted 23 March 2016 - 02:41 AM
A for loop with ipairs (and pairs) loops through a table and returns two things, index and value. I in this case is index, v is value. If you can't figure it out, the index is the position of the element in the array, with string indexes being returned as the actual string, and the value being returned as whatever data type it is. In the case of tMFSU it's the wrapped peripheral at the index i.
for index, value in ipairs( tMFSU) do
print(value)
print(tMFSU[index])
end
Those will print out the exact same things.
Edit: I tried to edit my starting lowercase a to A, and somehow put F. What?
Edited on 23 March 2016 - 01:56 AM
134 posts
Posted 23 March 2016 - 02:51 AM
F for loop with ipairs (and pairs) loops through a table and returns two things, index and value. I in this case is index, v is value. If you can't figure it out, the index is the position of the element in the array, with string indexes being returned as the actual string, and the value being returned as whatever data type it is. In the case of tMFSU it's the wrapped peripheral at the index i.
for index, value in ipairs( tMFSU) do
print(value)
print(tMFSU[index])
end
Those will print out the exact same things.
oh thanks!