Jump to content
  • 0

Optimizing function - prevent repeating code


Platonic

Question

Posted
function filterSettings(set, offset_0, offset_1, offset_2, dataTye_0, dataType_1, dataType_2)
  local resultTable = gg.getResults(gg.getResultsCount())
  filterTable = {}
  for i, v in ipairs(resultTable) do
    local filter = {{address = resultTable[i]["address"] + offset_0, flags = dataTye_0}}
    local filter_1 = {{address = resultTable[i]["address"] + offset_1, flags = dataType_1}}
    filter = gg.getValues(filter)
    filter_1 = gg.getValues(filter_1)
    if set == 2 then
      if filter[1]["value"] == "2" and filter_1[1]["value"] ~= "0" then
        filterTable[#filterTable + 1] = {address = resultTable[i]["address"] + offset_2, flags = dataType_2}
      end
    end
    if set == 3 then
      if filter[1]["value"] == "2" then
        filterTable[#filterTable + 1] = {address = resultTable[i]["address"] + offset_2, flags = dataType_2}
      end
    end
    if set == 4 then
      if filter[1]["value"] == "1031127695" and filter_1[1]["value"] ~= "0" then
        filterTable[#filterTable + 1] = {address = resultTable[i]["address"] + offset_2, flags = dataType_2}
      end
    end
  end
end

I'm writhing several times the "if" statement, beside the conditions all the actions of the if statements are the same. Can it be optimized? I don't know the proper way to nest the if statements so that i only have to use this code once:

filterTable[#filterTable + 1] = {address = resultTable[i]["address"] + offset_2, flags = dataType_2}

 

3 answers to this question

Recommended Posts

Posted
On 9/1/2022 at 6:38 AM, Platonic said:
function filterSettings(set, offset_0, offset_1, offset_2, dataTye_0, dataType_1, dataType_2)
  local resultTable = gg.getResults(gg.getResultsCount())
  filterTable = {}
  for i, v in ipairs(resultTable) do
    local filter = {{address = resultTable[i]["address"] + offset_0, flags = dataTye_0}}
    local filter_1 = {{address = resultTable[i]["address"] + offset_1, flags = dataType_1}}
    filter = gg.getValues(filter)
    filter_1 = gg.getValues(filter_1)
    if set == 2 then
      if filter[1]["value"] == "2" and filter_1[1]["value"] ~= "0" then
        filterTable[#filterTable + 1] = {address = resultTable[i]["address"] + offset_2, flags = dataType_2}
      end
    end
    if set == 3 then
      if filter[1]["value"] == "2" then
        filterTable[#filterTable + 1] = {address = resultTable[i]["address"] + offset_2, flags = dataType_2}
      end
    end
    if set == 4 then
      if filter[1]["value"] == "1031127695" and filter_1[1]["value"] ~= "0" then
        filterTable[#filterTable + 1] = {address = resultTable[i]["address"] + offset_2, flags = dataType_2}
      end
    end
  end
end

I'm writhing several times the "if" statement, beside the conditions all the actions of the if statements are the same. Can it be optimized? I don't know the proper way to nest the if statements so that i only have to use this code once:

filterTable[#filterTable + 1] = {address = resultTable[i]["address"] + offset_2, flags = dataType_2}

 

function filterSettings(offset_0, offset_1, offset_2, dataTye_0, dataType_1, dataType_2)
  local resultTable = gg.getResults(gg.getResultsCount())
  filterTable = {}
  for i, v in ipairs(resultTable) do
    local filter = {{address = resultTable[i]["address"] + offset_0, flags = dataTye_0}}
    local filter_1 = {{address = resultTable[i]["address"] + offset_1, flags = dataType_1}}
    filter = gg.getValues(filter)
    filter_1 = gg.getValues(filter_1)
    
    filterTable[#filterTable + 1] = ((filter[1]["value"] == "2" and filter_1[1]["value"] ~= "0") and {address = resultTable[i]["address"] + offset_2, flags = dataType_2} )
  or ((filter[1]["value"] == "2") and {address = resultTable[i]["address"] + offset_2, flags = dataType_2}) -- check your function logic i'm not sure about this ( it will negate the condtions above ( filter_1 ) 
  or ((filter[1]["value"] == "1031127695" and filter_1[1]["value"] ~= "0") and {address = resultTable[i]["address"] + offset_2, flags = dataType_2})
    
  end
end

--[[
set arg is useless since you already compairing filter and filter1
instead of if everytime you can shorten it by elseif : 
if somthing then 
elseif somthing then 
elseif somthing then 
end <-- one end instead of every condition end
using bitwise is similar to conditions,
example : 
((filter[1]["value"] == "1031127695" and filter_1[1]["value"] ~= "0") and {address = resultTable[i]["address"] + offset_2, flags = dataType_2})
using () is strict to avoid bugs : 
if both conditions met here :
filter[1]["value"] == "1031127695" and filter_1[1]["value"] ~= "0" -- means true , true
the last and (and {address = resultTable[i]["address"] + offset_2, flags = dataType_2}) will be assigned to the variable


]]

This should work.

Posted

Hi @Platonic, It is good to use some Permutation instead if you're planning on more Sets and check specific Permute are exist on Result. In the meantime, I just wanted to share my logic for this:

function filterSettings(set, offset_0, offset_1, offset_2, dataTye_0, dataType_1, dataType_2)
	local resultTable = gg.getResults(gg.getResultsCount())
	filterTable = {}
	for i, v in ipairs(resultTable) do
		local filter = gg.getValues({{address = resultTable[i]["address"] + offset_0, flags = dataTye_0}})[1]["value"]
		local filter_1 = gg.getValues({{address = resultTable[i]["address"] + offset_1, flags = dataType_1}})[1]["value"]

		--Example 1: Logic (use Set as controller, in case if there's Set 1 or more Set)
		filterTable[#filterTable + 1] = ((((filter == "2") and (((set == 2) and (filter_1 ~= "0")) or (set == 3))) or ((filter == "1031127695") and (filter_1 ~= "0"))) and {address = resultTable[i]["address"] + offset_2, flags = dataType_2})

		--Example 2: Ignore Set (Set doesn't matter if Set 3 or Any Set can also have filter_1 not equal to 0)
		filterTable[#filterTable + 1] = (((filter == "2" or filter == "1031127695") and (filter_1 ~= "0")) and {address = resultTable[i]["address"] + offset_2, flags = dataType_2})

	end
end
Posted

(having access problems with Platonic, replying through other acc) 

This post was a bit old and script has changed by now quite a bit. Since the script functions as a frame and is dependent on other factors I think I could be difficult to do modification like that. But will try. The elseif was implemented. Perhaps you can check the full script and see if the suggestion is applicable. But I am open to changes.

I use if set because it would be easier to recognize which function the hack is coming from in case something goes wrong in filterSettings(). 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.