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
3 hours ago, MAARS said:

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

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

Posted
14 hours ago, 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)

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
6 hours ago, MAARS said:

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

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
1 hour ago, 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()

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
2 hours ago, 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.

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
53 minutes ago, 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: 

53 minutes ago, 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)

Thank you very much, it works great!

 

Posted
9 hours ago, 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)

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.