a couple of questions on tables

Hello again everyone. I’m working on my in game shop functions, and to facilitate that, I’m creating a table, like in an external file called, waffleTableData.lua, currently, it looks like this:

--table for waffle purchasable items. local shop = require( "src.waffle\_shop") local waffleTable = { [1]= {cost = 5, func = shop:addLives}, } return waffleTable;

I’ve got two issues, first, I’m having no problem referencing the table by numerical value, such as

local cost = waffleTable[event.target.id]["cost"] 

and passing the number 1 as my event.target.id. However, I’d prefer to pass it the same id as we will for the actual cash shop, in the form of

id = "com.mybundle.myapp.1Life"

however, no matter how i format the ID in the table file (as a string only, as a string encased in [], or as just the literal com.mybundle.myapp.1Life, I always get some form of error. I suspect it’s because of the dot notation, but is there a way around it?

The second, and perhaps more pressing issue, involves the calling of a function from within the table.Right now, since I only have one item being set up, I have an if statement, but I’d like to change that a a for(event.target.id) do statement, and have it call all of the required information and function to make that happen. However, with the current setup (with those functions in yet another separate file, it doesn’t seem to work. I don’t get any errors generated, but it just doesn’t seem to take me to the function as I expect. am I better off just including all the functions in the table file?

ak,

Why not just add BundleID to the table that has the value you are trying to pass?  You must have the bundle id saved as a variable somewhere, no? 

pass it in like you pass in the index now.

event.target.BundleID to reference it.

local waffleTable = {
    [1]= {cost = 5, BundleID  = bundleId, func = shop:addLives},
    
}

or you could just index the waffleTable as your BundleID.

local waffleTable = {
    [BundleID]= {cost = 5, func = shop:addLives},
    
}

Hope this helps,

Nail

Nail;

However, if I try to access event.target.cost, it returns a nil value.

Trying ti index it as the ID is what I had been trying to do, but with the dot notation in it throws exceptions…

and now that I go to generate the exception, it doesn’t…

So, one final question, if I want to pass information to func, do I need to define the entire thing in the table?

ak,

run this one, it looks like you can pass parameters like this by just calling the function in the table and creating the function above or in scope and then passing in the params.

local BundleID = 10

local function shop(value, bundleID)
    local Value = value + 10
    print("Value == ", Value)
    print("bundleID == ", bundleID)
end

local waffleTable = {
    [BundleID]= {cost = 5, func = shop},
    
}

print("waffleTable[10].cost == ", waffleTable[10].cost)

waffleTable[BundleID].func(20, BundleID)

ak,

Why not just add BundleID to the table that has the value you are trying to pass?  You must have the bundle id saved as a variable somewhere, no? 

pass it in like you pass in the index now.

event.target.BundleID to reference it.

local waffleTable = {
    [1]= {cost = 5, BundleID  = bundleId, func = shop:addLives},
    
}

or you could just index the waffleTable as your BundleID.

local waffleTable = {
    [BundleID]= {cost = 5, func = shop:addLives},
    
}

Hope this helps,

Nail

Nail;

However, if I try to access event.target.cost, it returns a nil value.

Trying ti index it as the ID is what I had been trying to do, but with the dot notation in it throws exceptions…

and now that I go to generate the exception, it doesn’t…

So, one final question, if I want to pass information to func, do I need to define the entire thing in the table?

ak,

run this one, it looks like you can pass parameters like this by just calling the function in the table and creating the function above or in scope and then passing in the params.

local BundleID = 10

local function shop(value, bundleID)
    local Value = value + 10
    print("Value == ", Value)
    print("bundleID == ", bundleID)
end

local waffleTable = {
    [BundleID]= {cost = 5, func = shop},
    
}

print("waffleTable[10].cost == ", waffleTable[10].cost)

waffleTable[BundleID].func(20, BundleID)