Jump to content

Table


Gtagasje

Recommended Posts

Hi there,

Im having a little problem with tables.

I have a table storing certain informating about weapons, and I want to try match those values with values in a gridlist row when it's clicked.

But, I just can't get it working. It first gave me the error 'attempt to index ?(a nil value' but that is gone after I changed something, and now it just returns nil (I found out by using tostring on the returned value)

This is my code:

  
wepTable = { 
    {["ID"] = 22, ["Name"] = "Colt 45", ["Price"] = 3, ["Dual"] = "Yes", ["Clip"] = "17(34)", ["Type"] = "Handgun"}, 
        {["ID"] = 23, ["Name"] = "Silenced", ["Price"] = 1, ["Dual"] = "No", ["Clip"] = "17", ["Type"] = "Handgun"}, 
    {["ID"] = 24, ["Name"] = "Deagle", ["Price"] = 7, ["Dual"] = "No", ["Clip"] = "7", ["Type"] = "Handgun"}     
} -- These are just 3 lines from the table, because it's quite big, but there are more. 
  
--This is the malfunctioning function 
function gridUpdate(row, col) 
    local tempWepName = guiGridListGetItemText(wepGrid, row, wepGrid_ID) 
    for i, v in ipairs(wepTable) do 
        if wepTable[i]["ID"] == tempWepName then 
            local wepID = wepTable[i]["ID"] 
            local wepName = wepTable[i]["Name"] 
            local wepPrice = wepTable[i]["Price"] 
            local wepDual = wepTable[i]["Dual"] 
            local wepClip = wepTable[i]["Clip"] 
            local wepType = wepTable[i]["Type"] 
            break 
        end 
    end 
    guiSetText(nameVar, tostring(wepName)) 
    guiSetText(dualVar, tostring(wepDual)) 
    guiSetText(clipVar, tostring(wepClip)) 
    guiSetText(typeVar, tostring(wepType)) 
     
    guiGridListClear(wepGrid) 
    for i=1,#wepTable do 
        local row = guiGridListAddRow(wepGrid) 
        guiGridListSetItemText(wepGrid, row, wepGrid_name, wepTable[i]["Name"], false, false) 
        guiGridListSetItemText(wepGrid, row, wepGrid_price, wepTable[i]["Price"], false, false) 
        guiGridListSetItemText(wepGrid, row, wepGrid_ID, wepTable[i]["ID"], false, false) 
    end 
     
    guiGridListSetSelectedItem(wepGrid, row, col) 
end 
  

I hope I gave enough information, basicly the labels (nameVar, typeVar etc.) all get set to 'nil', because I did something wrong with the table but can't find out what.

Thanks in advance.

Link to comment
function gridUpdate(row, col) 
    local tempWepName = guiGridListGetItemText(wepGrid, row, wepGrid_ID) 
    for i, v in ipairs(wepTable) do 
        if v["ID"] == tonumber ( tempWepName ) then 
            local wepID = v["ID"] 
            local wepName = v["Name"] 
            local wepPrice = v["Price"] 
            local wepDual = v["Dual"] 
            local wepClip = v["Clip"] 
            local wepType = v["Type"] 
            guiSetText(nameVar, tostring(wepName)) 
            guiSetText(dualVar, tostring(wepDual)) 
            guiSetText(clipVar, tostring(wepClip)) 
            guiSetText(typeVar, tostring(wepType)) 
            break 
        end 
    end 
    
    guiGridListClear(wepGrid) 
    for i=1,#wepTable do 
        local row = guiGridListAddRow(wepGrid) 
        guiGridListSetItemText(wepGrid, row, wepGrid_name, wepTable[i]["Name"], false, false) 
        guiGridListSetItemText(wepGrid, row, wepGrid_price, wepTable[i]["Price"], false, false) 
        guiGridListSetItemText(wepGrid, row, wepGrid_ID, wepTable[i]["ID"], false, false) 
    end 
    
    guiGridListSetSelectedItem(wepGrid, row, col) 
end 

P.D: You could store the data of each weapon in the row using guiGridListSetItemData.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...