Jump to content
  • 0

Check value with offset function help


Lover1500

Question

I am making a function in lua.

gg.searchNumber(123, gg.TYPE_DWORD)
t = gg.getResults(gg.getResultsCount())

Now I want to check every float value at offset -0x24. So i wrote a function. This function will check value at commanded offset. And the equal values will be stored in table. and return.

function MemoryOffset(Table, Offset, Type, Value)
local temp={}
local original_flag=Table[1].flags
for i, v in ipairs(Table) do
v.address=v.address+Offset
v.flags=Type end
  
Table=gg.getValues(Table)
for i, v in ipairs(Table) do
if v.value==Value then table.insert(temp, v) end end
  
Table=nil
Table={}
for i, v in ipairs(temp) do
table.insert(Table, v) end
  
for i, v in ipairs(Table) do
v.address=v.address-Offset
v.flags=original_flag end
Table=gg.getValues(Table) return Table end

If I want to check float 45 at offset -0x6c usage will be

t= MemoryOffset(t, -0x6, gg.TYPE_FLOAT, 45)
print(t)

edit::must be -0x6c.my writing mistake.

It works fine.But it seems long texts in my mind. I want to know if there is a more better way. I'm very appreciated whoever help me.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

As far as I understood, the function checks described condition for each value in passed table and returns new table with subset of values from passed table that match the condition. One improvement to your function that I can suggest is to avoid unnecessary table traversal loops and second call to "getValues" function. The function with this improvement and few minor optimizations may look like this: 

function MemoryOffset(Table, Offset, Type, Value)
  local checkValues = {}
  for i, v in ipairs(Table) do
    checkValues[i] = {address = v.address + Offset, flags = Type}
  end
  checkValues = gg.getValues(checkValues)
  
  local result = {}
  for i, v in ipairs(checkValues) do
    if v.value == Value then
      result[#result + 1] = Table[i]
    end
  end
  return result
end
Link to comment
Share on other sites

Another solution i just wrapped repetitive statement in a function so you can re-use it.

And also some advice: avoid using already taken name in lua as a variable name,like table, table is an existing Library it may confuse newbie if they look to your code

untitled2.lua

Link to comment
Share on other sites

10 hours ago, CmP said:

As far as I understood, the function checks described condition for each value in passed table and returns new table with subset of values from passed table that match the condition. One improvement to your function that I can suggest is to avoid unnecessary table traversal loops and second call to "getValues" function. The function with this improvement and few minor optimizations may look like this: 

function MemoryOffset(Table, Offset, Type, Value)
  local checkValues = {}
  for i, v in ipairs(Table) do
    checkValues[i] = {address = v.address + Offset, flags = Type}
  end
  checkValues = gg.getValues(checkValues)
  
  local result = {}
  for i, v in ipairs(checkValues) do
    if v.value == Value then
      result[#result + 1] = Table[i]
    end
  end
  return result
end

After checking your code, i think you forgot that he want to get back to dword before returning the values your code will return float values

Link to comment
Share on other sites

2 hours ago, MAARS said:

i think you forgot that he want to get back to dword before returning the values your code will return float values

I found it works fine whatever return value type.Because i'm just checking other values with offset but not itself.

13 hours ago, CmP said:

One improvement to your function that I can suggest is to avoid unnecessary table traversal loops

Yeah i'll have to try that. rip little brain.

6 hours ago, MAARS said:

avoid using already taken name in lua as a variable name,like table, table is an existing Library it may confuse newbie if they look to your code

I also thought to use next new table.but many tables also confuse me. And i think reusing existing table by set nil will make my function cleaner.

in your script, instead of this

editVal (result, 0x6c, originalFlags)
editVal (result, -0x6c, originalFlags)

I edit a little bit to

editVal (result, offset, originalFlags)
editVal (result, -offset, originalFlags)

 

Yeah it works great now with any offset check.

Thank you two for your codes.It gives me great encourage in writing script.

Link to comment
Share on other sites

8 hours ago, MAARS said:

After checking your code, i think you forgot that he want to get back to dword before returning the values your code will return float values

No, it will return table of tables (that match condition) from passed table. Your claim is wrong because in the code "result" table is populated only with sub-tables of passed table and passed table isn't modified in the function.

Link to comment
Share on other sites

6 hours ago, Lover1500 said:

And i think reusing existing table by set nil will make my function cleaner.

Yes, this can be done to not create new table for result. The main difference is that "pairs" function will need to be used for iterating over elements of returned table because some of them may be set to nil ("ipairs" iterator will stop after encountering first nil element).

Link to comment
Share on other sites

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.