Accessing table within table to call functions

Ive been looking at this for too long and just cannot figure out whats is wrong. I have a table which stores some values and another table within that table which stores some functions. I want to select one of the funcs and perform all the functions stored in that table.


-- test4

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    
    card = {
        {fnamn = "jug",
        lnamn = "ste",
        funcs = {
            myFunc,
            my,
            nisse,
            lasse}
        },
        {fnamn = "cec",
        lnamn = "ste",
        funcs = {
            nisse,
            lasse}
        },
        {fnamn = "jud",
        lnamn = "top",
        funcs = {
            my,
            lasse,
            nisse}
        }
    }
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    for i, j in pairs(card) do
        print("i = "..i)
        print("card.fnamn = "..j.fnamn)
        if j.fnamn == "cec" then
            print("inne")
            for a, b in pairs(card.funcs) do
                b()
            end
        end
    end
end

function myFunc()
    text("hej", WIDTH/2.5, HEIGHT/2.5)
    end
function my()
    text("hopp", WIDTH/3.5, HEIGHT/3.5)
    end
function nisse()
    text("nisse", WIDTH/2, HEIGHT/2)
    end
function lasse()
    text("lasse", WIDTH/3, HEIGHT/3)
    end

I believe the problem is here somewhere
for a, b in pairs(card.funcs) do

So i like to access card.funcs and then just run all functions in that table.

What am i missing?

Here’s one way.


-- Use this function to perform your initial setup
function setup()
    print("Hello World!")

    card = {
        {fnamn = "jug",
        lnamn = "ste",
        funcs = {
            myFunc,
            my,
            nisse,
            lasse}
        },
        {fnamn = "cec",
        lnamn = "ste",
        funcs = {
            nisse,
            lasse}
        },
        {fnamn = "jud",
        lnamn = "top",
        funcs = {
            my,
            lasse,
            nisse}
        }
    }
    
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    for a,b in pairs(card) do
        for c,d in pairs(b) do
            if type(d)=="table" then
                for e,f in pairs(d) do
                  f()
                end  
            end
        end
    end
end

function myFunc()
    text("hej", WIDTH/2.5, HEIGHT/2.5)
    end
function my()
    text("hopp", WIDTH/3.5, HEIGHT/3.5)
    end
function nisse()
    text("nisse", WIDTH/2, HEIGHT/2)
    end
function lasse()
    text("lasse", WIDTH/3, HEIGHT/3)
    end

Here’s another.

EDIT:Added a line of code to select fnamn cec that was in the original code.


-- Use this function to perform your initial setup
function setup()
    print("Hello World!")

    card = {
        {fnamn = "jug",
         lnamn = "ste",
         funcs = {
            myFunc,
            my,
            nisse,
            lasse}
        },
        {fnamn = "cec",
        lnamn = "ste",
        funcs = {
            nisse,
            lasse}
        },
        {fnamn = "jud",
        lnamn = "top",
        funcs = {
            my,
            lasse,
            nisse}
        }
    }    
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    for a,b in pairs(card) do
        if b.fnamn=="cec" then
            for c,d in pairs(b.funcs) do
                d()
            end
        end
    end

end

function myFunc()
    text("hej", WIDTH/2.5, HEIGHT/2.5)
    end
function my()
    text("hopp", WIDTH/3.5, HEIGHT/3.5)
    end
function nisse()
    text("nisse", WIDTH/2, HEIGHT/2)
    end
function lasse()
    text("lasse", WIDTH/3, HEIGHT/3)
    end

Ok, the code works like a charm in the program. However, when i translate the above to my real program it doesnt work. Just cant figure out what im misding.

function performCard()
    playerStamina = playerStamina - exploreStaminaReq
    for k, v in pairs(card) do
        if cardTitle == v.title then
            for e, f in pairs(v.event) do
                print("ttt")
                f()
                state = 9
            end
        end
    end
end

The tables i use have the same format as in the previous code


card={
{
    1,
    title = "Forest",
    description = "info about forest",
    event = {
        infested,
        loot
    }
},
{
    2,
    title = "Field",
    description = "info about field",
    event = {
        infested,
        loot
    }
},
{
    3,
    title = "City",
    description = "info about city",
    event = {
        infested,
        loot
    }
},
{
    4,
    title = "House",
    description = "info about house",
    event = {
        infested,
        loot
    }
},
{
    5,
    title = "Random",
    description = "Something random happens",
    event = {
        infested,
        loot
    }
}
}

[withdrawn]


function setup()
    cardTitle="House"
    performCard()    
end

function performCard()
    --playerStamina = playerStamina - exploreStaminaReq
    for k, v in pairs(card) do
        if cardTitle == v.title then
            for e, f in pairs(v.event) do
                f()
                --state = 9
            end
        end
    end
end

function infested()
    print("function infested called")
end

function loot()
    print("function loot called")
end

card={
{
    1,
    title = "Forest",
    description = "info about forest",
    event = {
        infested,
        loot
    }
},
{
    2,
    title = "Field",
    description = "info about field",
    event = {
        infested,
        loot
    }
},
{
    3,
    title = "City",
    description = "info about city",
    event = {
        infested,
        loot
    }
},
{
    4,
    title = "House",
    description = "info about house",
    event = {
        infested,
        loot
    }
},
{
    5,
    title = "Random",
    description = "Something random happens",
    event = {
        infested,
        loot
    }
}
}

Ok, what fixd this was putting the card {} in my main file. Which i dont understand. Before i had card{} on another tab. This worked fine. Even the searching for the right card. But this line of code never worked:

for e, f in pairs(v.event) do

What am i missing here?

@fikabord I’m not sure what you’re question is. I moved card to another tab and deleted card from main and the code worked fine. It shouldn’t matter where card is defined. As for the “for” loops, The first “for” loop iterates thru the table card, and the second “for” loop iterates thru the table event for each entry of card. I’m not sure why you should have a problem with the “for e,v” loop.

@dave1707 thx dave. Not sure why it didnt work as it should have worked have the card {} on a different tab. Oh well, it works now so i can proceed. Once again super many thanks.