10 posts
Posted 01 August 2015 - 09:08 PM
Hi, I'm trying to assign multiple turbines using tables (First time using tables by the way)
and I'm having problems Ive been searching google and forum but i cant seem to find the help i need
Could anyone shed some light on what I'm doing wrong?
http://pastebin.com/sKJ8p82nthanks,
Sky
656 posts
Posted 01 August 2015 - 09:26 PM
{peripheral.find} returns a table of already wrapped peripherals, so your for loop is unnessary/wrong.
You will have to loop though that table when you want to call any functions though. For convenience you can create functions that do the looping for you:
local reactors = {peripheral.find ("whatever")}
local function setActive (active)
for i, reactor in ipairs (t)
reactor.setActive (active)
end
end
10 posts
Posted 01 August 2015 - 10:40 PM
Ok thanks how would i set each Turbine a name IE if it was done normally it would be.
T1 = peripheral.wrap("BigReactors-Turbine_0")
1140 posts
Location
Kaunas, Lithuania
Posted 01 August 2015 - 11:10 PM
After putting the results of peripheral.find() to a table you'd have to access the separate peripherals using:
T[1].setActive(...)
T[2].anotherMethod()
Read up on tables
here.
10 posts
Posted 01 August 2015 - 11:26 PM
I read so much about tables but i can seem to grasp it fully
Guess i will keep trying lol thanks for the help all :)/>
10 posts
Posted 02 August 2015 - 04:48 AM
Hi, Another question
I am currently trying to add the total RF per a tick generated for all of the turbines im able to call the value pretty easy but i need to find a way to add the value together for all 10 turbines so i get a total value.
the only way i know to do this is:
Total = T[1].getEnergyProducedLastTick() + T[2]getEnergyProducedLastTick() + T[3].......
Is there a more efficient way?
Edit is the also a way to remove the decimal at the end of the Total?
Edited on 02 August 2015 - 02:48 AM
202 posts
Posted 02 August 2015 - 05:25 AM
I think you can loop through the turbine table and add the energy of each turbine to the total like this:
for i, turbine in pairs(turbines) do
Total = Total + T[i].getEnergyProducedLastTick()
end
To get rid of the decimal, you can round the total using either math.floor() or math.ceil().Bomb Bloke has a better loop and more info on the roudning below
Edited on 02 August 2015 - 03:29 AM
7083 posts
Location
Tasmania (AU)
Posted 02 August 2015 - 05:26 AM
When it comes to dealing with multiple entries in a table, usually the answer is "for loops".
Eg:
local Total = 0
for i = 1, #T do Total = Total + T[i].getEnergyProducedLastTick() end
If you're getting a ".0" when mon.write()'ing numbers, tostring() them. If you wish to round them down, math.floor() them.