Jump to content
  • 0

optimizing fuctionallity of script


Platonic

Question

This repetitive behavior don't make sense. would like to reduce this pointless repeating in this function theGiant. If anyone can perhaps explain a more efficient way for writhe this function without the need of making such amount of tables for basically one simple thing. which is editing all the values on given offsets (0x104, 0x100 and 0xFC) either to 2.125478 or to 1.

function theGiant()
  if giants == on then
    gg.setRanges(gg.REGION_ANONYMOUS | gg.REGION_OTHER)
    gg.clearResults()
    gg.searchNumber('h 85 EB 91 3F 6F 12 03 3C', gg.TYPE_FLOAT)
    gg.refineNumber('h 85', gg.TYPE_BYTE)
    p = gg.getResults(50)
    gg.clearResults()
    xHight = {}
    yHight = {}
    zHight = {}
    for i, v in ipairs(p) do
      xHight[i] = {address = v.address - 0x104, flags = gg.TYPE_FLOAT}
      yHight[i] = {address = v.address - 0x100, flags = gg.TYPE_FLOAT}
      zHight[i] = {address = v.address - 0xFC, flags = gg.TYPE_FLOAT}
    end
    xHight = gg.getValues(xHight)
    yHight = gg.getValues(yHight)
    zHight = gg.getValues(zHight)
    
    for i, v in ipairs(xHight) do
      v.value = '2.125478'
      gg.setValues(xHight)
    end
    for i, v in ipairs(yHight) do
      v.value = '2.125478'
      gg.setValues(yHight)
    end
    for i, v in ipairs(zHight) do
      v.value = '2.125478'
      gg.setValues(zHight)
    end
  else
    for i, v in ipairs(xHight) do
      v.value = '1'
      gg.setValues(xHight)
    end
    for i, v in ipairs(yHight) do
      v.value = '1'
      gg.setValues(yHight)
    end
    for i, v in ipairs(zHight) do
      v.value = '1'
      gg.setValues(zHight)
    end
  end
end

 

 

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

maybe something like this will work..?

local t = gg.getResults(50)

for i,v in pairs(t) do
      gg.setValues({
      {address = t[i].address - 0x104,flags = gg.TYPE_FLOAT, value = 2.125478},
      {address = t[i].address - 0x100,flags = gg.TYPE_FLOAT, value = 2.125478},
      {address = t[i].address - 0xFC,flags = gg.TYPE_FLOAT, value = 2.125478} })
end
else
for i,v in pairs(t) do
      gg.setValues({ 
      {address = t[i].address - 0x104,flags = gg.TYPE_FLOAT, value = 1},
      {address = t[i].address - 0x100,flags = gg.TYPE_FLOAT, value = 1},
      {address = t[i].address - 0xFC,flags = gg.TYPE_FLOAT, value = 1} })
end

 

Link to comment
Share on other sites

Nice. Thanks for this. Gave me new ideas. Did not know that you can use gg.setValues like that.

There is another question. Actually i should have included it in to the script but i forgot because i only realized now that it is a requirement in the function to avoid the game to crash.

You already took care of the editing the value. But let's say that before editing the value on the given offsets need to be 1. If the value is not 1 then that value should not be edited. I post below the long version. How would you optimize it?

function theGiant()
  if giants == on then
    gg.setRanges(gg.REGION_ANONYMOUS | gg.REGION_OTHER)
    gg.clearResults()
    gg.searchNumber('h 85 EB 91 3F 6F 12 03 3C', gg.TYPE_FLOAT)
    gg.refineNumber('h 85', gg.TYPE_BYTE)
    p = gg.getResults(50)
    gg.clearResults()
    xHeightFilt = {}
    yHeightFilt = {}
    zHeightFilt = {}
    for i, v in ipairs(p) do
      xHeightFilt[i] = {address = v.address - 0x104, flags = gg.TYPE_FLOAT}
      yHeightFilt[i] = {address = v.address - 0x100, flags = gg.TYPE_FLOAT}
      zHeightFilt[i] = {address = v.address - 0xFC, flags = gg.TYPE_FLOAT}
    end
    xHeightFilt = gg.getValues(xHeightFilt)
    yHeightFilt = gg.getValues(yHeightFilt)
    zHeightFilt = gg.getValues(zHeightFilt)
    
    -- filtering the tables, so that only tables with value '1' are left to avoid possible crashing during editing.
    xHight = {}
    for i, v in ipairs(xHeightFilt) do
      if v.value == 1 then
        xHight[#xHight + 1] = v
      end
    end
    
    yHight = {}
    for i, v in ipairs(yHeightFilt) do
      if v.value == 1 then
        yHight[#yHight + 1] = v
      end
    end
    
    zHight = {}
    for i, v in ipairs(zHeightFilt) do
      if v.value == 1 then
        zHight[#zHight + 1] = v
      end
    end
    
    -- editing, user input = ON
    for i, v in ipairs(xHight) do
      v.value = '2.125478'
      gg.setValues(xHight)
    end
    for i, v in ipairs(yHight) do
      v.value = '2.125478'
      gg.setValues(yHight)
    end
    for i, v in ipairs(zHight) do
      v.value = '2.125478'
      gg.setValues(zHight)
    end
  else
    -- editing, user input = OFF
    for i, v in ipairs(xHight) do
      v.value = '1'
      gg.setValues(xHight)
    end
    for i, v in ipairs(yHight) do
      v.value = '1'
      gg.setValues(yHight)
    end
    for i, v in ipairs(zHight) do
      v.value = '1'
      gg.setValues(zHight)
    end
  end
end

 

Link to comment
Share on other sites

im not an expert in Lua.

what i gave you.. merely an example that i kept around for sometime now.

@CmP

will be the perfect choice for this.

i already tag him.

just wait for him to response.

or maybe someone else will come to help.

Link to comment
Share on other sites

First and the most important thing that can be done to optimize the code is to call "setValues" once after loop(s) instead of calling it each time for the whole table after setting "value" field of one sub-table. It is extremely redundant to call "setValues" N times for table of N values (sub-tables). It only needs to be called once, after desired values of "value" field of all sub-tables (for which the value needs to be edited) have been set.

One other thing that can be done is to have one table for all values instead of three tables, since all values need to be edited the same way. This won't make the code noticeably more or less efficient, but it will allow to replace fragments of code that are repeated three times (once for each table) with one instance of them for combined table.

The code with the changes suggested above can look like the following: 

function theGiant()
  if giants == on then
    gg.setRanges(gg.REGION_ANONYMOUS | gg.REGION_OTHER)
    gg.clearResults()
    gg.searchNumber('h 85 EB 91 3F 6F 12 03 3C', gg.TYPE_BYTE) -- corrected the type here
    gg.refineNumber('h 85', gg.TYPE_BYTE)
    local results = gg.getResults(50)
    gg.clearResults()
    local heightValuesCheck = {}
    for i, v in ipairs(results) do
      heightValuesCheck[(i - 1) * 3 + 1] = {address = v.address - 0x104, flags = gg.TYPE_FLOAT}
      heightValuesCheck[(i - 1) * 3 + 2] = {address = v.address - 0x100, flags = gg.TYPE_FLOAT}
      heightValuesCheck[(i - 1) * 3 + 3] = {address = v.address - 0xFC, flags = gg.TYPE_FLOAT}
    end
    heightValuesCheck = gg.getValues(heightValuesCheck)
    
    -- filtering the tables, so that only tables with value '1' are left to avoid possible crashing during editing.
    heightValues = {}
    for i, v in ipairs(heightValuesCheck) do
      if v.value == 1 then
        heightValues[#heightValues + 1] = v
      end
    end
    
    -- editing, user input = ON
    for i, v in ipairs(heightValues) do
      v.value = '2.125478'
    end
    gg.setValues(heightValues) -- calling the function once, after desired values are set for all table elements
  else
    -- editing, user input = OFF
    for i, v in ipairs(heightValues) do
      v.value = '1'
    end
    gg.setValues(heightValues) -- calling the function once likewise
  end
end
Link to comment
Share on other sites

8 hours ago, CmP said:

The code with the changes suggested above can look like the following: 

    local results = gg.getResults(50)
    gg.clearResults()
    local heightValuesCheck = {}
    for i, v in ipairs(results) do
      heightValuesCheck[(i - 1) * 3 + 1] = {address = v.address - 0x104, flags = gg.TYPE_FLOAT}
      heightValuesCheck[(i - 1) * 3 + 2] = {address = v.address - 0x100, flags = gg.TYPE_FLOAT}
      heightValuesCheck[(i - 1) * 3 + 3] = {address = v.address - 0xFC, flags = gg.TYPE_FLOAT}
    end
    heightValuesCheck = gg.getValues(heightValuesCheck)

 

I don't quite understand this code [(i - 1) * 3 + 1]. Is i -1 setting index to 1 ?. Your example works flawless, according to the print "heightValuesCheck" has all the needed sub tables are in one main table instead of 3 tables. Is there perhaps someway were i could have more knowledge regarding those syntax during loop "[(i - 1) * 3 + 1]" . would not really know when parentheses, square brackets are applicable in a for loop (beside the basic found on the lua doc/forum/scripts) an if it would be allowed to do math on it.

Also totally out of scope. But i tried writhing part of your code manually but it only seems to works if i copy and past it. If i don't copy past it it wont work. So i converted the part of the code i quoted you in starting from the for loop till the end into hex and put through some online compare tool and it looks like on 2 chars are like interpreted differently. Not sure why is that. (Regex looks totally same)

image.thumb.png.d9ab751d1d60b5b70184712e20004802.pngh

image.thumb.png.ce44e789aaea64900d4ecdfd06f1ddd9.png

Link to comment
Share on other sites

11 minutes ago, Platonic said:

Also totally out of scope. But i tried writhing part of your code manually but it only seems to works if i copy and past it. If i don't copy past it it wont work.

Check the code carefully. Your manually retyped code has difference in second and third lines of loop body.

Second line of loop's body from my code: 

      heightValuesCheck[(i - 1) * 3 + 2] = {address = v.address - 0x100, flags = gg.TYPE_FLOAT}

And how you retyped it: 

      heightValuesCheck[(i - 1) * 3 + 1] = {address = v.address - 0x100, flags = gg.TYPE_FLOAT}

It should be "(i - 1) * 3 + 2" there for the code to work as intended.

Third line of loop's body in your retyped code needs to be fixed accordingly.

19 minutes ago, Platonic said:

I don't quite understand this code [(i - 1) * 3 + 1].

This code is a minor optimization for adding elements to the end of array part of the table for the case of adding 3 elements per loop iteration. It achieves same result as more familiar "[#heightValuesCheck + 1]".

The expression "(i - 1) * 3" is for calculating "base" index for three elements to be added to the table during iteration "i". At the first iteration, when i = 1, the expression evaluates to 0. At the second iteration (i = 2) the expression evaluates to 3. At the third to 6, and so on. To calculate index for the first element to be added during iteration, 1 is added to the result of the expression, giving index of 1 at the first iteration, 4 at the second iteration, 7 at the third iteration, and so on. Likewise, index of the second element to be added during iteration is calculated by adding 2 to the result of the expression and index of the third element - by adding 3.

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.