Deltanic Posted July 22, 2010 Share Posted July 22, 2010 I always fail when I try something new, but normally I can fix the problem. Not now, I hope there's anyone who does know how to do the following: I've made a table, but before adding something to the table, I want to know if it's already in there. So there will be no double things in the table. But how do I do that? This is what I have now, unworking. Still adds multiple values to the table: (Clientscript) function someFunctionWithLoop ( ) local someTable = {} --The table for _,v in ipairs ( something ) do --Loop local name = getSomeString ( someElement ) -- Get some string if name ~= table.find(someTable, 'name', name) then --If "name" isn't in the table someTable[v] = name --Add it end end end function table.find(t, ...) -- I got this function from the freeroam resource local args = { ... } if #args == 0 then for k,v in pairs(t) do if v then return k end end return false end local value = table.remove(args) if value == '[nil]' then value = nil end for k,v in pairs(t) do for i,index in ipairs(args) do if type(index) == 'function' then v = index(v) else if index == '[last]' then index = #v end v = v[index] end end if v == value then return k end end return false end Link to comment
50p Posted July 22, 2010 Share Posted July 22, 2010 You can do: if not table.find( someTable, "name", name ) then Link to comment
Deltanic Posted July 22, 2010 Author Share Posted July 22, 2010 I've tried that before, but it that isn't working either. It still has multiple values. Well, maybe that example script wasn't wrong, so here's the script I'm using. Its a gridlist for every tuning part indeed. I know that this has been scripted for the freeroam mode already, but the one of the freeroam has a lot of code I don't need, or cod which is very difficult. So, I'm creating my own, with my own style. function populateTuneGridlist ( ) --Function is triggered when pressing a GUI button. Only when you're in a vehicle. guiGridListClear ( GUIPopupGrid_Grid ) -- Clear the gridlist, because this gridlist contains other lists too. local theVehicle = getPedOccupiedVehicle ( getLocalPlayer ( ) ) -- Get the players vehicle local upgradeType = {} -- The table for _,upgrade in ipairs ( getVehicleCompatibleUpgrades ( theVehicle ) ) do -- The loop name = getVehicleUpgradeSlotName ( upgrade ) if not name == table.find ( upgradeType, "name", name ) then --Should be: If it cannot be find in the table, then do local row = guiGridListAddRow ( GUIPopupGrid_Grid ) --Wheeeeeee, a lot of double upgradetypes in my gridlist. guiGridListSetItemText ( GUIPopupGrid_Grid, row, 1, name, true, false ) upgradeType[upgrade] = name -- Add it to the table end end showGridlistGUI ( ) --Show the gridlist, placed in another function setElementData ( thisPlayer, "PopupType", "tune" ) --Gridlist contains the tune list now. thisPlayer = getLocalPlayer ( ) end Link to comment
50p Posted July 22, 2010 Share Posted July 22, 2010 Why did you use: if not name == table.find ( upgradeType, "name", name ) then if I asked you to do: if not table.find ( upgradeType, "name", name ) then Link to comment
Deltanic Posted July 22, 2010 Author Share Posted July 22, 2010 Though I didn't do that indeed (Misreading), it still doesn't work when using yours. Oh, and by the way, the console gives no errors. Link to comment
50p Posted July 22, 2010 Share Posted July 22, 2010 You don't have any values with "name" inside the upgradeType table, so table.find will always return false. I just now understood what you're trying to do... You want this: if not table.find( upgradeType, name ) then Link to comment
Deltanic Posted July 22, 2010 Author Share Posted July 22, 2010 Thnx I didn't thought that could be the problem. 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