Search code examples
lualua-tablelove2d

call a function from inside a table that's inside another table in lua


I am attempting to build my first game using love2D, I have hit a problem.

The game is a bubble popping game, I want to assign a bubble to each letter on the keyboard so that when a letter is pressed, the bubble will pop.

I have an external file called "bubble.lua" which I have tried to make an object "bubble" with. to do that I have created a table "bubble" in the bubble.lua which contains functions and variables. Now, this file works when called from main.lua using just one bubble, however I am going to need 26 bubbles so I thought it would be best to store each bubble in another table. For the purpose of trying this I just stored one bubble using 1 as the key.This is where I have problems.

require "bubble"
local bubbles = {}
function love.load()
    bubbles[1] = bubble.load(100, 100)
end
function love.draw()
    for bubble in bubbles do
        bubble.draw()
    end
end

function love.keypressed(key)
    bubbles[key].bubble.pop()
end

Firstly, I know that the for loop in love.draw() does not work, and the line "bubble[key].bubble.pop" seems to return nil as well

The for loop I can probably find the solution myself online, my main problem is the "bubble[key].bubble.pop()" line, I cannot work out what's wrong or how to fix it.

Can anybody help me?

You may want to look at this as well:

bubble.lua

bubble = {}
function bubble.load(posX, posY)
    bubble.x = posX
    bubble.y = posY
    bubble.popped = false
end

function bubble.draw()

    if not bubble.popped then
        love.graphics.rectangle("line", bubble.x, bubble.y, 37, 37)
    else
        love.graphics.rectangle("line", bubble.x, bubble.y, 37, 100)
    end
end

function bubble.pop()
    bubble.popped = true
end

Edit:

Following the advice of the answer below I now have the following error when I press "a":

main.lua:14: attempt to index a nil value

the updated code is below

main.lua

require "bubble"
local bubbles = {}
function love.load()
    bubbles["a"] = bubble.load(100, 100)
end
function love.draw()
    for key, bubble in pairs(bubbles) do
       bubble.draw()
    end
end

function love.keypressed(key)
    bubbles[key].pop()
end

any thoughts?


Solution

  • There are several issues with this code. First, you index by number when you initialize bubbles (bubbles[1]), but access them using the key as the index (bubbles[key]), which is NOT a number. You need to settle on one mechanism to index the bubbles. Let's say you picked using key as the index (instead of the number).

    This loop:

    for bubble in bubbles do
        bubble.draw()
    end
    

    should be written as:

    for key, bubble in pairs(bubbles) do
        bubble.draw()
    end
    

    and instead of bubbles[key].bubble.pop() you can simply do bubbles[key].pop() as bubbles[key] already returns the bubble you can pop.

    To initialize, instead of bubbles[1] you need to do bubbles['a'] (or whatever other value is used by key in love.keypressed(key)).