Jump to content

Getting selected items on active tab in helper script


CmP

Recommended Posts

One of the ways to make use of GG scripting capabilities is to create helper scripts - scripts that help user by automating certain actions, usually to save time and increase productivity. An example of such script is a script that automates calculation of encrypted/decrypted values in case of non-trivial encryption of values being used in game. Other example is a script for creating and managing custom lists of values when there are several groups of 10-20+ values (that are not necessarily located near each other) of interest and having all of them in GG's saved list becomes inconvenient.

Most of helper scripts have 2 things in common:
  1. They are called using GG's UI button for scripts (https://gameguardian.net/forum/gallery/image/618-900-added-ui-button-for-scripts-gameguardian/).
  2. They work with values/addresses that user can choose in any of the following interface tabs: search results list, saved list, memory viewer.

To make choosing values for script to process more convenient for user GG API has the following functions:
  - gg.getSelectedResults for getting items that are selected in results list;
  - gg.getSelectedListItems for getting items that are selected in saved list;
  - gg.getSelectedElements for getting addresses that are selected in memory viewer;
  - gg.getActiveTab for determining which tab is currently opened in GG's interface.

With these functions it is possible to implement getting items that are selected on currently active tab. It can be done by calling "gg.getActiveTab" function and depending on which constant returned value is equal to, calling corresponding function to get selected items from active tab. Also, since "gg.getSelectedElements" function returns table of addresses, it can be transformed to table of tables with "address" field for the sake of unification of format of returned tables. So custom function to get selected items from currently active tab, in case of success ("getSelected*" functions return string in case of error), will return table of 0 (when no items are selected) or more sub-tables with data about selected items each of which contains at least "address" field.

Here is an example of how described function can be implemented: 

function getActiveTabSelectedItems()
  local items = {}
  local tab = gg.getActiveTab()
  if tab == gg.TAB_SEARCH then
    items = gg.getSelectedResults()
  elseif tab == gg.TAB_SAVED_LIST then
    items = gg.getSelectedListItems()
  elseif tab == gg.TAB_MEMORY_EDITOR then
    for index, addr in ipairs(gg.getSelectedElements()) do
      items[index] = {address = addr}
    end
  end
  return items
end

To show it in action an example script with it's usage has been implemented. When called with script button, it gets selected items from currently active tab and displays alert with text representation of table returned by the function. Here is the code: 

function getActiveTabSelectedItems()
  local items = {}
  local tab = gg.getActiveTab()
  if tab == gg.TAB_SEARCH then
    items = gg.getSelectedResults()
  elseif tab == gg.TAB_SAVED_LIST then
    items = gg.getSelectedListItems()
  elseif tab == gg.TAB_MEMORY_EDITOR then
    for index, addr in ipairs(gg.getSelectedElements()) do
      items[index] = {address = addr}
    end
  end
  return items
end

function displaySelectedItems()
  local items = getActiveTabSelectedItems()
  local choice = gg.alert(tostring(items), "OK", "Exit")
  if choice == 2 then
    os.exit()
  end
end

gg.showUiButton()
while true do
  if gg.isClickedUiButton() then
    gg.hideUiButton()
    displaySelectedItems()
    gg.showUiButton()
  end
  gg.sleep(100)
end

And the file with code for convenience: active_tab_selected_items_example.lua

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.