This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SkyDevil_'s profile picture

Having problems with tables

Started by SkyDevil_, 01 August 2015 - 07:08 PM
SkyDevil_ #1
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/sKJ8p82n

thanks,
Sky
flaghacker #2
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
SkyDevil_ #3
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")
MKlegoman357 #4
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.
SkyDevil_ #5
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 :)/>
SkyDevil_ #6
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
grand_mind1 #7
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
Bomb Bloke #8
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.