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

Changing The Value In A Table Within A Table

Started by campicus, 05 November 2013 - 08:51 PM
campicus #1
Posted 05 November 2013 - 09:51 PM
I have this table:


local spawn = {
creeper = {active = false, status = "off", color = colors.green, yPos = 3,},
witch = {active = false, status = "off", color = colors.red, yPos = 4,},
skeleton = {active = false, status = "off", color = colors.white, yPos = 5,},
blaze = {active = false, status = "off", color = colors.orange, yPos = 6,},
pigmen = {active = false, status = "off", color = colors.yellow, yPos = 7,},
slime = {active = false, status = "off", color = colors.lime, yPos = 8,},
chicken = {active = false, status = "off", color = colors.pink, yPos = 9,},
witch = {active = false, status = "off", color = colors.purple, yPos = 10,},
cow = {active = false, status = "off", color = colors.brown, yPos = 11,},
zombie = {active = false, status = "off", color = colors.lightGray, yPos = 12,},
}

How do I change the value of 'active'? I was thinking something like:

spawn[creeper][active] = true

Will this work? I'm currently at work so cannot test this, thought I would put it to the masters

Cheers in advance!
theoriginalbit #2
Posted 05 November 2013 - 10:35 PM
Close. Currently what you've got is this
spawn[nil][nil] = true
since it assumes creeper and active are variables, which are not set.

As such you would do either of the following
spawn["creeper"]["active"] = true
or
spawn.creeper.active = true

Good references
The official PIL
Lua-users
campicus #3
Posted 06 November 2013 - 01:11 AM
thanks!
campicus #4
Posted 06 November 2013 - 02:32 AM
I am probably a little beyond my abilities here and I am having issues (nothing works like it should haha).
If anyone wants to take a look for any blatant issues I would be grateful. I am not getting any errors, the program is simply not toggling the correct wires and not changing the status on screen when u click the 'off'.

thanks in advance

Spoiler

local rsSide = "back"
local sCount = 1
local mon = peripheral.wrap("right")

local spawn = {
creeper = {status = "off", color = colors.green, yPos = 3,},
witch = {status = "off", color = colors.red, yPos = 4,},
skeleton = {status = "off", color = colors.white, yPos = 5,},
blaze = {status = "off", color = colors.orange, yPos = 6,},
pigmen = {status = "off", color = colors.yellow, yPos = 7,},
slime = {status = "off", color = colors.lime, yPos = 8,},
chicken = {status = "off", color = colors.pink, yPos = 9,},
zombie = {status = "off", color = colors.purple, yPos = 10,},
cow = {status = "off", color = colors.brown, yPos = 11,},
spider = {status = "off", color = colors.lightGray, yPos = 12,},
wither = {status = "off", color = colors.black, yPos = 13,},
}

local function header()
  mon.clear()
  mon.setCursorPos(1,1)
  mon.setBackgroundColor(colors.black)
  mon.setTextColor(colors.white)
  mon.write("Camp's Spawner v0.3")
  for mob, data in pairs(spawn) do
    mon.setCursorPos(3, data.yPos)
    mon.write(" "..mob..": ")
    if data.status == "on" then
      mon.setBackgroundColor(colors.green)
      mon.setTextColor(colors.white)
    elseif data.status == "off" then
      mon.setBackgroundColor(colors.red)
      mon.setTextColor(colors.black)
    end
    mon.setCursorPos(16,data.yPos)
    mon.write(data.status)
    mon.setBackgroundColor(colors.black)
    mon.setTextColor(colors.white)
    mon.setCursorPos(24,data.yPos)
    mon.write(sCount)
  end
end

local function toggleSpawn()
  for mob,data in pairs(spawn) do
    if data.status == "on" then
      rs.setBundledOutput(rsSide, rs.getBundledOutput(rsSide)-data.color)
      data.status = "on"
    else
      rs.setBundledOutput(rsSide, rs.getBundledOutput(rsSide)+data.color)
      data.status = "off"
    end
  end
end

local function checkClick(row, xMin, xMax, mob)
  if yClick == row then
    if xClick >= xMin and xClick <= xMax then
      if mob == "off" then
        mob = "on"
      else
        mob = "off"
      end
    end
  end
end

while true do
  toggleSpawn()
  header()
  event, side, xClick, yClick = os.pullEvent("monitor_touch")
  checkClick(3,16,21,spawn.creeper.status)
  checkClick(4,16,21,spawn.witch.status)
  checkClick(5,16,21,spawn.skeleton.status)
  checkClick(6,16,21,spawn.blaze.status)
  checkClick(7,16,21,spawn.pigmen.status)
  checkClick(8,16,21,spawn.slime.status)
  checkClick(9,16,21,spawn.chicken.status)
  checkClick(10,16,21,spawn.zombie.status)
  checkClick(11,16,21,spawn.cow.status)
  checkClick(12,16,21,spawn.spider.status)
end
Bomb Bloke #5
Posted 06 November 2013 - 03:25 AM
When you call "checkClick()", the "mob" variable that gets passed the content of "spawn.whatever.status" only exists within that function. It's not a pointer to the original variable, just a temporary copy that gets discarded when that function ends - were you to pass a table it'd be a different story (as in that case a pointer to the original table WOULD get passed along), but in this case the function ends up achieving nothing.

Your "toggleSpawn()" function also has some issues. What if you try to add a colour value to the bundled output that's already IN there (eg, adding green twice means you end up with the value for red). What if you try to subtract one that ISN'T (you could easily end up with a negative value!)? Use colors.test() / bit.band() to check the current state of the cable before altering it - or check out the other color API functions.
campicus #6
Posted 06 November 2013 - 05:53 AM
were you to pass a table it'd be a different story

How do I pass the table properly then?

I will take a look at the colors.test :)/>
Bomb Bloke #7
Posted 06 November 2013 - 06:55 AM
Well, if you specifically wanted to pass a table, you could send on "spawn.blaze" instead of "spawn.blaze.status", for eg. "mob.status" would then refer to the variable you actually want the "checkClick()" function to mess with.