Gtagasje Posted December 31, 2013 Share Posted December 31, 2013 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
Castillo Posted December 31, 2013 Share Posted December 31, 2013 What does "tempWepName" return? Link to comment
Gtagasje Posted December 31, 2013 Author Share Posted December 31, 2013 It returns the ID of the weapon I clicked. The gridlist has the columns "Weapon", "Price" and "ID", so if I click "Colt 45" it returns "22", and if I click "Silenced" it returns "23". Link to comment
Castillo Posted December 31, 2013 Share Posted December 31, 2013 I'm sure that the problem is that it's returning a string, try doing this: if wepTable[i]["ID"] == tonumber ( tempWepName ) then Link to comment
Gtagasje Posted December 31, 2013 Author Share Posted December 31, 2013 I tried it, but it the labels still get the text "nil" when I click a row, and if I remove the tostring() at the guiSetText I get errors that a string was expected but a nil was given. Link to comment
Castillo Posted December 31, 2013 Share Posted December 31, 2013 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
Gtagasje Posted December 31, 2013 Author Share Posted December 31, 2013 Thanks a lot man, you helped me again! And yeah, if I encounter into more problems related to this I'll first try that before asking here. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now