I was trying to find a more clever way to solve this.

This is an excerpt from a code related to a game, it loops trough each slot of each backpack untill it finds a shovel and a rope

local continue
local foundShovel, foundRope
        for i = 0, Container.GetLast():Index() do -- looping trough backpacks
        local cont = Container(i)
            for j = 0, cont:ItemCount()-1 do -- looping trough each slot
            local id = cont:GetItemData(j).id -- Getting ID of that slot
            foundShovel, foundRope = GetToolIndex(id,0) or foundShovel,GetToolIndex(id,1) or foundRope -- confusing...
                if foundShovel and foundRope then
                    continue = true
                    break
                end
            end
            if continue then
               -- do something i need to do
            end
        end
    end
-- Switches ID to corresponding index :
function GetToolIndex(id,retrn)
    local shovel = {
    [9598] = 4 , -- whacking driller of fate
    [9599]= 4 , -- whacking driller of fate(jammed)
    [9596]= 3 , -- squeezing gear of girlpower
    [9597]= 3 , -- squeezing gear of girlpower(jammed)
    [9594]= 2 , -- sneaky stabber of elitenesss
    [9595]= 2 , -- sneaky stabber of elitenesss(jammed)
    [5710]= 1, -- light shovel
    [3457] = 0 -- shovel
    }
    local rope = {
    [646]= 1, -- elvenhair rope
    [3003] = 0, -- rope
    [9598] = 4 , -- whacking driller of fate
    [9599]= 4 , -- whacking driller of fate(jammed)
    [9596]= 3 , -- squeezing gear of girlpower
    [9597]= 3 , -- squeezing gear of girlpower(jammed)
    [9594]= 2 , -- sneaky stabber of elitenesss
    [9595]= 2  -- sneaky stabber of elitenesss(jammed)
    }
    if retrn == 0 then 
        return shovel[id] 
    elseif return == 1 then 
        retrn rope[id] 
    end
end

But its not working, and i was thinking there must be a better way for this approach, what if i need to find X values within a table instead of just 2? I hope my issue could be understood here.

share|improve this question
    
I'm surprised if return == 0 doesn't cause an error. – greatwolf Dec 13 '14 at 20:21
    
it does, my bad – Mojimi Dec 13 '14 at 20:24
    
Can you confirm the way the game indexes stuff is really 0-based and not 1-based? – greatwolf Dec 13 '14 at 20:24
    
yeah, pretty sure it starts at 0 – Mojimi Dec 13 '14 at 20:27
up vote 3 down vote accepted

Well, storing all data in tables is much more better,rather than calling a function to get containers data.

Anyhow here's a script that will get all backpack id's, store them in a table and then just call you function with each id.

Backpack = {data = {}};
function Backpack:update(refresh)
    if(#self.data==0 or refresh) then
        for i = 0, Container.GetLast():Index() do
            local cont = Container(i);
            for j = 0,cont:ItemCount()-1 do
                --table.insert(self.data,cont:GetItemData(j).id)
                self.data[#self.data+1] = cont:GetItemData(j).id; -- faster than table.insert
            end
        end
    end
end
function Backpack:refresh()
    self:update(true)
end
function Backpack:find(func,...) -- func should return a value if id is good, otherwise false, add extra args to call with the function
    if (not func) then
        return false;
    end
    self:update(); -- Incase there is no inventory data;
    for key,value in pairs(self.data) do
        local value = func(value,...); -- calls the function with the id (as first parameter) of the 'cached' user inventory
        if (value) then
            return value
        end
    end
    return false;
end

Example:

function GetToolIndex(id,return)
    local shovel = {
    [9598] = 4 , -- whacking driller of fate
    [9599]= 4 , -- whacking driller of fate(jammed)
    [9596]= 3 , -- squeezing gear of girlpower
    [9597]= 3 , -- squeezing gear of girlpower(jammed)
    [9594]= 2 , -- sneaky stabber of elitenesss
    [9595]= 2 , -- sneaky stabber of elitenesss(jammed)
    [5710]= 1, -- light shovel
    [3457] = 0 -- shovel
    }
    local rope = {
    [646]= 1, -- elvenhair rope
    [3003] = 0, -- rope
    [9598] = 4 , -- whacking driller of fate
    [9599]= 4 , -- whacking driller of fate(jammed)
    [9596]= 3 , -- squeezing gear of girlpower
    [9597]= 3 , -- squeezing gear of girlpower(jammed)
    [9594]= 2 , -- sneaky stabber of elitenesss
    [9595]= 2  -- sneaky stabber of elitenesss(jammed)
    }
    if return == 0 then 
        return shovel[id] 
    elseif return == 1 then 
        return rope[id] 
    end
end

function Test()
    local shovel,rope = Backpack:find(GetToolIndex,0),Backpack:find(GetToolIndex,1)
    if (shovel and rope) then
        print("Shovel and rope exist");
    end
end
Test();

EDIT:

After thinking a while, you seem to try to check if user has that specific id.

Here's another method, that will check all all user backpack data (deep table search), it will search based on table key or table value, it will work on nested tables too, this should set you good.

Backpack = {data = {}};
function Backpack:update(refresh) 
    if(#self.data==0 or refresh) then
        for i = 0, Container.GetLast():Index() do
            local cont = Container(i);
            for j = 0,cont:ItemCount()-1 do
                local data = cont:GetItemData(j); -- pretty sure this returns a table
                self.data[data.id] = data; -- self.data[9598] = {...}
            end
        end
    end
end
function Backpack:refresh()
    self:update(true)
end
function Backpack:MultiTableSearch(input,value,case,index_check)
    if (input and type(input) == 'table') then
        if (type(value) == 'table' and value == input) then
            return true;
        end
        for key,object in pairs(input) do
            if (index_check) then
                if (case and type(input)=='string' and type(key)=='string') then
                    if (value:lower() == key:lower()) then -- to avoid exit the loop
                        return true;
                    end
                else
                    if (key == value) then
                        return true
                    elseif(type(object)=='table') then
                        return self:MultiTableSearch(object,value,case,index_check)
                    end
                end
            else
                if (case and type(value)=='string' and type(object) == 'string') then
                    if (value:lower() == object:lower()) then
                        return true;
                    end
                elseif(type(object)=='table') then
                    if (value == object) then
                        return true;
                    else
                        return self:MultiTableSearch(object,value,case)
                    end
                else
                    if (object == value) then
                        return true;
                    end
                end
            end
        end
    end
    return false;
end
function Backpack:exists(value,case,index_check)
    self:update();
    return self:MultiTableSearch(self.data,value,case,index_check)
end
 -- checks the value 9598, case-insensitive is set to false,
 -- index_checking is set to true (checks table index --> Backpack.data[9598], if it was set it'll return true);
if (Backpack:exists(9598,false,true)) then
    print("User has whacking driller of fate");
end
if (Backpack:exists("Shovel of doom")) then -- will try to find any table-value that has the of "Shovel of doom" (case-sensitive)
    print("User Shovel of doom");
end

If you're worried about the performance of the MultiTableSearch (because it looks a bit heavy), it's pretty fast, ran several tests.

Function

function MultiTableSearch(input,value,case,index_check)
    if (input and type(input) == 'table') then
        if (type(value) == 'table' and value == input) then
            return true;
        end
        for key,object in pairs(input) do
            if (index_check) then
                if (case and type(input)=='string' and type(key)=='string') then
                    if (value:lower() == key:lower()) then -- to avoid exit the loop
                        return true;
                    end
                else
                    if (key == value) then
                        return true
                    elseif(type(object)=='table') then
                        return MultiTableSearch(object,value,case,index_check)
                    end
                end
            else
                if (case and type(value)=='string' and type(object) == 'string') then
                    if (value:lower() == object:lower()) then
                        return true;
                    end
                elseif(type(object)=='table') then
                    if (value == object) then
                        return true;
                    else
                        return MultiTableSearch(object,value,case)
                    end
                else
                    if (object == value) then
                        return true;
                    end
                end
            end
        end
    end
    return false;
end

Tests (appending values into a table and scanning nested tables, both tests are in nonsense table sizes)

local start_time = os.clock();
t = {}
for i=1,500000 do --500k table size
    t[i]=i-1
end
print(os.clock()-start_time) -- 0.05 sec to create the table
local start_time = os.clock();
print(tostring(MultiTableSearch(t,500000,false)))-- will try to find a key with the value of 500,000
print(os.clock()-start_time) -- 0.197sec sec to scan the whole table

function nestedTable(object,times) -- creates nested table -> object={{{{..n times}}}}
    if (times > 0 ) then
        object[#object+1] = {times = times}
        return (nestedTable(object[#object],times-1))
    end
end
local start_time = os.clock();
t = {};
nestedTable(t,15000) --> will create table inside a table x 15000 times.
print(os.clock()-start_time) -- 0.007 sec to create the nested table
local start_time = os.clock();
print(tostring(MultiTableSearch(t,1,false)))-- will try to find a 1 (as a table value), the very last table value
print(os.clock()-start_time) -- 0.014 sec to find the value in the nested table
share|improve this answer
    
I didn't do it that way because i wanted to stop in case i found them all, avoiding unnecessary actions – Mojimi Dec 13 '14 at 20:51
    
@mojimi, the return value will exit the loop, it's better to handle items in arrays(tables), they're quickest anyhow I've wrote another method. – das Dec 13 '14 at 20:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.