Jump to content




Having problems with tables


  • You cannot reply to this topic
7 replies to this topic

#1 SkyDevil_

  • Members
  • 10 posts

Posted 01 August 2015 - 08: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

#2 flaghacker

  • Members
  • 655 posts

Posted 01 August 2015 - 08: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


#3 SkyDevil_

  • Members
  • 10 posts

Posted 01 August 2015 - 09: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")


#4 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 01 August 2015 - 10: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.

#5 SkyDevil_

  • Members
  • 10 posts

Posted 01 August 2015 - 10: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 :)

#6 SkyDevil_

  • Members
  • 10 posts

Posted 02 August 2015 - 03: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 by SkyDevil_, 02 August 2015 - 03:48 AM.


#7 grand_mind1

  • Members
  • 207 posts

Posted 02 August 2015 - 04: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 by grand_mind1, 02 August 2015 - 04:29 AM.


#8 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 02 August 2015 - 04: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users