Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Get and add elements from List View to array table [SOLVED]

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Wed Jul 15, 2020 9:22 pm    Post subject: Get and add elements from List View to array table [SOLVED] Reply with quote

Say I have some variables:

Code:
UDF1.Show()

list = UDF1.CEListView1
tab_player = {}

p_name = 'Corroder'
p_gen = 'Male'

table.insert(tab_player,{player_name = p_name, player_gen = p_gen})

-- and then add some elements from List View to same record index

for idx = list.ItemIndex + 1, list.Items.Count-1 do
 mtrl_name = list.Items[idx].Caption
 mtrl_qty =  list.Items[idx].SubItems[0]
 mtrl_unit = list.Items[idx].SubItems[1]
 mtrl_price = list.Items[idx].SubItems[2]
 mtrl_tprice = list.Items[idx].SubItems[3]
 table.insert(tab_player, {v_itemname = mtrl_name, v_itemqty = mtrl_qty,
              v_itemunit = mtrl_unit, v_itemprice = mtrl_price, v_itemttlprice = mtrl_tprice})
end

-- check
for index, data in ipairs(tab_player) do
    print(index)
    for key, value in pairs(data) do
        print('\t', key, value)
    end
end


Result, its created 9 tab_player record indexes (depending how many items on list view).

What I want is like this structure for one record index:

Code:
{
  player_name = p_name,
  player_gen = p_gen,

  {
    v_itemname = mtrl_name,
    v_itemqty = mtrl_qty,
    v_itemunit = mtrl_unit,
    v_itemprice = mtrl_price,
    v_itemttlprice = mtrl_tprice},

   {
    v_itemname = mtrl_name,
    v_itemqty = mtrl_qty,
    v_itemunit = mtrl_unit,
    v_itemprice = mtrl_price,
    v_itemttlprice = mtrl_tprice},

  {
    v_itemname = mtrl_name,
    v_itemqty = mtrl_qty,
    v_itemunit = mtrl_unit,
    v_itemprice = mtrl_price,
    v_itemttlprice = mtrl_tprice}

    -- and so on
}



1. How CE Lua script to get the structure as I want?

2. If done, then how CE Lua script call the data from tab_player to fill
player name editbox, player gen editbox and fill the items to CE List View?

I am use a Combobox for query on tab_player by player_name key.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL


Last edited by Corroder on Fri Jul 17, 2020 10:10 pm; edited 1 time in total
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8585
Location: 127.0.0.1

PostPosted: Thu Jul 16, 2020 2:32 pm    Post subject: Reply with quote

I'd suggest you look into the JSON file format. It's very similar to what you are trying to accomplish here with that structure. There are a handful of JSON libs for Lua already such as:

https://github.com/rxi/json.lua/blob/master/json.lua
https://gist.github.com/tylerneylon/59f4bcf316be525b30ab
http://regex.info/code/JSON.lua

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Fri Jul 17, 2020 1:17 am    Post subject: Reply with quote

Thanks atom0s for support and information. I need to learn how to use JSON Lua script especially in my case.

Meanwhile, I am using text file to save and load data:

Code:
UDF1.Show()

list = UDF1.CEListView1
pname = UDF1.CEEdit1
pgen = UDF1.CEEdit2
bsave = UDF1.CEButton2
bload = UDF1.CEButton1
pPath = TrainerOrigin
load_dialog = createOpenDialog(UDF1)
save_dialog = createSaveDialog(UDF1)

bsave.Enabled = false

function check_input()
 local pn = pname.Text
 local gn = pgen.Text
 if pn == '' and gn == '' then
    return nil
 else
    bsave.Enabled = true
 end
end

function save2txt()
 save_dialog.InitalDir = pPath
 save_dialog.Filter = 'Text files|*.TXT;*.txt|All files (*.*)|*'
 save_dialog.execute()
 local file_name = save_dialog.FileName
 local file_name,err = io.open(file_name,'w')
 if file_name then
    local pn = pname.Text
    local gn = pgen.Text
    file_name:write(pn..'\n'..gn..'\n')
    list = UDF1.CEListView1
    local idx, mtrl_name, mtrl_qty, mtrl_unit, mtrl_price, mtrl_tprice
    for idx = list.ItemIndex + 1, list.Items.Count do
        mtrl_name = list.Items[idx].Caption
        mtrl_qty =  list.Items[idx].SubItems[0]
        mtrl_unit = list.Items[idx].SubItems[1]
        mtrl_price = list.Items[idx].SubItems[2]
        mtrl_tprice = list.Items[idx].SubItems[3]
        file_name:write(mtrl_name..'\n'..mtrl_qty..'\n'.. mtrl_unit..'\n'..mtrl_price..'\n'..mtrl_tprice..'\n')
    end
    file_name:close()
    pname.Text = ''
    pgen.Text = ''
    bsave.Enabled = false
    list.clear()
    showMessage("Save success")
 else
    showMessage("Save error:", err)
 end
end

---- load
function get_line(filename, line_number)
 local i = 0
 for line in io.lines(filename) do
     i = i + 1
     if i == line_number then
        return line
      end
 end
 return nil
end

function loadtext()
 pname.Text = ''
 pgen.Text = ''
 list.clear()
 bsave.Enabled = false

 load_dialog.InitalDir = pPath
 load_dialog.Filter = 'Text files|*.TXT;*.txt|All files (*.*)|*'
 load_dialog.execute()
 local file_name = load_dialog.FileName

 local ctr = 0
 for _ in io.lines(file_name) do
    ctr = ctr + 1
 end

 local pn = get_line(file_name, 1)
 local gn = get_line(file_name, 2)
 pname.Text = pn
 pgen.Text = gn

 local items = list.Items
 local item = items.Add()
 item.Caption = get_line(file_name, 3)
 local sub_item = get_line(file_name, 4).."\n"..get_line(file_name, 5).."\n"..get_line(file_name, 6).."\n"..get_line(file_name, 7)
 item.SubItems.text = sub_item

 local items = list.Items
 local item = items.Add()
 item.Caption = get_line(file_name, 8)
 sub_item = get_line(file_name, 9).."\n"..get_line(file_name, 10).."\n"..get_line(file_name, 11).."\n"..get_line(file_name, 12)
 item.SubItems.text = sub_item

end

pname.OnChange = check_input
pgen.OnChange = check_input
bsave.OnClick = save2txt
bload.OnClick = loadtext


When save all variables to a text file, it produces like this (example 12 line text) :

Code:
Corroder   -- line 1
Man   --- line 2
Golden Sword
2
Pcs
50000
100000
Magic Wind Spell
100
Shack
1000
100000  -- line 12


When load data from text file to form, I have no problem to get player name and player gen. Those reference by line 1 and line 2 from text file. But the problem is when add remain text file lines to the list view on the form.
I can do it manually by giving lines number to list view caption and sub items text which always start from line 3 of text file and end on line 7 for first list view item and then repeat start from line 8 to 12 for next list view item.

Question:
How do the process with loop statement and place each item from text file correctly on the list view?.

Change this part:

Code:
 local items = list.Items
 local item = items.Add()
 item.Caption = get_line(file_name, 3)
 local sub_item = get_line(file_name, 4).."\n"..get_line(file_name, 5).."\n"..get_line(file_name, 6).."\n"..get_line(file_name, 7)
 item.SubItems.text = sub_item

 local items = list.Items
 local item = items.Add()
 item.Caption = get_line(file_name, 8)
 sub_item = get_line(file_name, 9).."\n"..get_line(file_name, 10).."\n"..get_line(file_name, 11).."\n"..get_line(file_name, 12)
 item.SubItems.text = sub_item


_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Fri Jul 17, 2020 10:09 pm    Post subject: Reply with quote

SOLVED:

Change this part:

Code:
local items = list.Items
 local item = items.Add()
 item.Caption = get_line(file_name, 3)
 local sub_item = get_line(file_name, 4).."\n"..get_line(file_name, 5).."\n"..get_line(file_name, 6).."\n"..get_line(file_name, 7)
 item.SubItems.text = sub_item

 local items = list.Items
 local item = items.Add()
 item.Caption = get_line(file_name, 8)
 sub_item = get_line(file_name, 9).."\n"..get_line(file_name, 10).."\n"..get_line(file_name, 11).."\n"..get_line(file_name, 12)
 item.SubItems.text = sub_item


to:

Code:
local i = 3
 for line in io.lines(file_name) do
     local items = list.Items
     local item = items.Add()
     item.Caption = get_line(file_name, i)  --3
     item.SubItems.Add(get_line(file_name, i+1)) --4
     item.SubItems.Add(get_line(file_name, i+2)) --5
     item.SubItems.Add(get_line(file_name, i+3)) --6
     item.SubItems.Add(get_line(file_name, i+4)) --7
     i = i + 5
  end
end

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites