Jump to content
  • 0

How to create Menu with values


hoangninyb

Question

Posted

i have

A = 1

B = 4

C = 5

D = 3

E = 6

.......

 

i have a code:

----

gg.clearResults()

gg.searchNumber("1000" , gg.TYPE_DWORD)

p = gg.getResultCount()               

q = gg.getResults(p)                   

for i = 1,p do

            l = {}

   l = {}

      l.address = q.address + 4

   l.flags = 4           

gg.getValues(l)

gg.addListItems(l)

gg.loadResults(gg.getListItems(l))

end

------

and in the list there will be values n

I want to create a menu with those values, example: in list have 1,4,5,3,6 then the menu will have 4 lines A,B,C,D,E(picture 1) or in list have 1,3,6 then the menu will have 3 lines A,D,E (picture 2)

Can you help me with a model?

plase help me!

Screenshot_2021-06-30-04-12-52-733_com.f1player.jpg

Screenshot_2021-06-30-04-12-01-251_com.f1player.jpg

8 answers to this question

Recommended Posts

Posted
  On 6/30/2021 at 7:26 AM, MAARS said:

ipairs dont work if the table is holed, use pairs instead

Expand  

Can you give me a clear example? I just learned about lua script so my knowledge is very limited!

Posted
  On 6/29/2021 at 9:57 PM, CmP said:

First of all, it seems that using saved list and loading results is not required in your case, so the code can be rewritten to: 

gg.clearResults()
gg.searchNumber("1000", gg.TYPE_DWORD)
local count = gg.getResultsCount()               
local results = gg.getResults(count)                   
for i, v in ipairs(results) do
  v.address = v.address + 4
end
local values = gg.getValues(results) -- table with target values

Then, the menu that you described can be created from data in "values" table with the following code: 

local mapping = {
  [1] = "A",
  [4] = "B",
  [5] = "C",
  [3] = "D",
  [6] = "E"
}

local menuItems = {}
for i, v in ipairs(values) do
  local item = mapping[v.value]
  if item ~= nil then -- ignoring values that are absent in "mapping" table
    table.insert(menuItems, item)
  end
end

gg.choice(menuItems)
Expand  

Hey bro, how when i press A it will take me to function A() or if there is B it will take me to function B()

Posted
  On 6/30/2021 at 7:26 AM, MAARS said:

ipairs dont work if the table is holed, use pairs instead

Expand  

As you can notice, "ipairs" iterator function is used on "values" table obtained from the call to "getValues" function that has "results" table as it's argument that in turn is obtained from the call to "getResults" function that returns a table with all it's elements being traversable with "ipairs" function. So usage of "ipairs" function in the code from my post is correct and replacing it with "pairs" won't make any difference in this case.

Posted
  On 6/30/2021 at 12:55 PM, hoangninyb said:

Hey bro, how when i press A it will take me to function A() or if there is B it will take me to function B()

Expand  

For example, with the following adaptation of the code:

local function A()
  print("A")
end

local function B()
  print("B")
end

local function C()
  print("C")
end

local function D()
  print("D")
end

local function E()
  print("E")
end

local mapping = {
  [1] = {name = "A", func = A},
  [4] = {name = "B", func = B},
  [5] = {name = "C", func = C},
  [3] = {name = "D", func = D},
  [6] = {name = "E", func = E}
}

local menuNames = {}
local menuFunctions = {}
for i, v in ipairs(values) do
  local item = mapping[v.value]
  if item ~= nil then -- ignoring values that are absent in "mapping" table
    table.insert(menuNames, item.name)
    table.insert(menuFunctions, item.func)
  end
end

local choice = gg.choice(menuNames)
if choice ~= nil then
  menuFunctions[choice]()
end

Let me know whether it works as you expect it to.

Posted
  On 6/30/2021 at 2:50 PM, CmP said:

For example, with the following adaptation of the code:

local function A()
  print("A")
end

local function B()
  print("B")
end

local function C()
  print("C")
end

local function D()
  print("D")
end

local function E()
  print("E")
end

local mapping = {
  [1] = {name = "A", func = A},
  [4] = {name = "B", func = B},
  [5] = {name = "C", func = C},
  [3] = {name = "D", func = D},
  [6] = {name = "E", func = E}
}

local menuNames = {}
local menuFunctions = {}
for i, v in ipairs(values) do
  local item = mapping[v.value]
  if item ~= nil then -- ignoring values that are absent in "mapping" table
    table.insert(menuNames, item.name)
    table.insert(menuFunctions, item.func)
  end
end

local choice = gg.choice(menuNames)
if choice ~= nil then
  menuFunctions[choice]()
end

Let me know whether it works as you expect it to.

Expand  

it works great bro!

Posted

First of all, it seems that using saved list and loading results is not required in your case, so the code can be rewritten to: 

gg.clearResults()
gg.searchNumber("1000", gg.TYPE_DWORD)
local count = gg.getResultsCount()               
local results = gg.getResults(count)                   
for i, v in ipairs(results) do
  v.address = v.address + 4
end
local values = gg.getValues(results) -- table with target values

Then, the menu that you described can be created from data in "values" table with the following code: 

local mapping = {
  [1] = "A",
  [4] = "B",
  [5] = "C",
  [3] = "D",
  [6] = "E"
}

local menuItems = {}
for i, v in ipairs(values) do
  local item = mapping[v.value]
  if item ~= nil then -- ignoring values that are absent in "mapping" table
    table.insert(menuItems, item)
  end
end

gg.choice(menuItems)
Posted
  On 6/29/2021 at 9:57 PM, CmP said:

First of all, it seems that using saved list and loading results is not required in your case, so the code can be rewritten to: 

gg.clearResults()
gg.searchNumber("1000", gg.TYPE_DWORD)
local count = gg.getResultsCount()               
local results = gg.getResults(count)                   
for i, v in ipairs(results) do
  v.address = v.address + 4
end
local values = gg.getValues(results) -- table with target values

Then, the menu that you described can be created from data in "values" table with the following code: 

  On 6/29/2021 at 9:57 PM, CmP said:

First of all, it seems that using saved list and loading results is not required in your case, so the code can be rewritten to: 


gg.clearResults()
gg.searchNumber("1000", gg.TYPE_DWORD)
local count = gg.getResultsCount()               
local results = gg.getResults(count)                   
for i, v in ipairs(results) do
  v.address = v.address + 4
end
local values = gg.getValues(results) -- table with target values

Then, the menu that you described can be created from data in "values" table with the following code: 


local mapping = {
  [1] = "A",
  [4] = "B",
  [5] = "C",
  [3] = "D",
  [6] = "E"
}

local menuItems = {}
for i, v in ipairs(values) do
  local item = mapping[v.value]
  if item ~= nil then -- ignoring values that are absent in "mapping" table
    table.insert(menuItems, item)
  end
end

gg.choice(menuItems)
Expand  

Thank you very much, it works great!

Expand  

 

Posted
  On 6/29/2021 at 9:57 PM, CmP said:

First of all, it seems that using saved list and loading results is not required in your case, so the code can be rewritten to: 

gg.clearResults()
gg.searchNumber("1000", gg.TYPE_DWORD)
local count = gg.getResultsCount()               
local results = gg.getResults(count)                   
for i, v in ipairs(results) do
  v.address = v.address + 4
end
local values = gg.getValues(results) -- table with target values

Then, the menu that you described can be created from data in "values" table with the following code: 

local mapping = {
  [1] = "A",
  [4] = "B",
  [5] = "C",
  [3] = "D",
  [6] = "E"
}

local menuItems = {}
for i, v in ipairs(values) do
  local item = mapping[v.value]
  if item ~= nil then -- ignoring values that are absent in "mapping" table
    table.insert(menuItems, item)
  end
end

gg.choice(menuItems)
Expand  

ipairs dont work if the table is holed, use pairs instead

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.