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 


Populating a ListView

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
bknight2602
Master Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 355

PostPosted: Thu Mar 30, 2017 9:48 am    Post subject: Populating a ListView Reply with quote

I have a different ListBox question, this time populating from a table. The data table looks like this, followed by my attempt to load the data into a table.
Code:
trainer =    {
            Data =    {
                       PlayerColor = {
                                          [0] = 'Red';
                                          [1] = 'Blue';
                                          [2] = 'Tan';
                                          [3] = 'Green';
                                          [4] = 'Orange';
                                          [5] = 'Purple';
                                          [6] = 'Teal';
                                          [7] = 'Pink';
                                          [8] = 'No Color';
                                     };--PlayerColor
                        Characters =    {
                                          [0] = 'Orrin';
                                          [1] = 'Valeska';
                                          [2] = 'Edric';
                                          [3] = 'Sylvia';
                                          [4] = 'Lord Haart';
                                          [5] = 'Sorsha';
                                          [6] = 'Christian';
                                          [7] = 'Tyris';
                                           ...
                                          [127] = 'Tiva';
                                          [128] = "No One";

...
   self.listview = createListView(self.form)
   setProperty(self.listview, 'ViewStyle', 'vsReport')
   setProperty(self.listview, 'RowSelect', 'True')
   setProperty(self.listview, 'ReadOnly', 'True')
   setProperty(self.listview, 'HideSelection', 'False')
   self.listview.top = 190;
   self.listview.width = 140;
   self.listview.left = 100;
   self.listview.height = 230;
   self.listview_colum1 = self.listview.getColumns().add()
   self.listview_colum2 = self.listview.getColumns().add()
   self.listview_colum1.Width = 0;
   self.listview_colum2.Width = 140;
   self.listview_colum1.Caption = 'Index';
   self.listview_colum2.Caption = 'Character';
                                       i = self.Data.Characters[index] + 1;
                                       for _,__table in pairs(self.Data.Characters[i]) do
                                          local entry = self.listview.getItems().add();
                                          entry.Caption = __table[1];
                                          local subentry = entry.getSubItems().add(__table[2]);
                                       end

The table when opened gives the error:
Error:[string "--function FormShow(sender)..."]:896: attempt to perform arithmetic on field '?' (a nil value)

Since an "index" has not been defined, I can understand the error. If the i = self.Data.Characters[index] + 1; is deleted, I get this error:
Error:[string "--function FormShow(sender)..."]:896: bad argument #1 to 'pairs' (table expected, got nil)
What would be the proper code steps to load the data? Once the data is loaded and one of the characters is selected then I need to use this selection to load another ListView as shown in the attachment.



2017-03-29_22-12-59.png
 Description:
 Filesize:  22.86 KB
 Viewed:  4324 Time(s)

2017-03-29_22-12-59.png


Back to top
View user's profile Send private message Yahoo Messenger
Zanzer
I post too much
Reputation: 119

Joined: 09 Jun 2013
Posts: 3217

PostPosted: Thu Mar 30, 2017 5:01 pm    Post subject: Reply with quote

After you deleted the instruction, what did you expect "i" to equal?
Of course this returned nil: trainer.Data.Characters[nil]
Your Characters table is a list of strings. Why are you trying to treat them like tables?
Code:
for _, name in pairs(trainer.Data.Characters) do
  print(name)
end

I can only assume you wanted:
Code:
for _, name in pairs(trainer.Data.Characters) do
  local entry = self.listview.getItems().add()
  entry.Caption = name
  for _, color in pairs(trainer.Data.PlayerColor) do
    local subentry = entry.getSubItems().add(color)
  end
end
Back to top
View user's profile Send private message
bknight2602
Master Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 355

PostPosted: Thu Mar 30, 2017 5:47 pm    Post subject: Reply with quote

Zanzer wrote:
After you deleted the instruction, what did you expect "i" to equal?
Of course this returned nil: trainer.Data.Characters[nil]
Your Characters table is a list of strings. Why are you trying to treat them like tables?
Code:
for _, name in pairs(trainer.Data.Characters) do
  print(name)
end

I can only assume you wanted:
Code:
for _, name in pairs(trainer.Data.Characters) do
  local entry = self.listview.getItems().add()
  entry.Caption = name
  for _, color in pairs(trainer.Data.PlayerColor) do
    local subentry = entry.getSubItems().add(color)
  end
end


Almost but not what I was looking to achieve. The output is Red for however many lines
Code:
for _, name in pairs(self.Data.Characters) do
  local entry = self.listview.getItems().add()
  entry.Caption = name
  for _, color in pairs(trainer.Data.Characters) do
    local subentry = entry.getSubItems().add(name)
  end
end

Results in(see image), now if one of the names is selected, what is the syntax to use that name in the Lv, Exp ListView to the right? This is intended to be used to edit the experience of the selected character.

You asked why I'm treating them as a table, from a different CE table, same data structure as listed above, Of course I'm not using a RadioGroup to select the "name" as is the current case, however this look suspiciously like a data table {index, data}.
Code:
   self.characters_rg.onClick =    function (sender)
                                    local index = strings_getString(sender.getItems(), sender.ItemIndex);
                                    local i = nil;
                                    i = self.Data.Characters[index] + 1;
                                    if i then
                                       heroname = index;
                                       --print("The index selected is", self.Data.Characters[index]);
                                       --print("The hero selected from index is ", index);
                                       print("The hero name is ", heroname);
                                       self.listview.clear();
                                       for _,__table in pairs(self.Data.Stats[i]) do
                                          local entry = self.listview.getItems().add();
                                          entry.Caption = __table[1];
                                          local subentry = entry.getSubItems().add(__table[2]);
                                       end
                                    end
                                    if expvalue and goldvalue and heroname then
                                       RecalculateAddresses()
                                    end
                                 end

This set of data is from, just like the current example immediately after the names
Code:
                       Stats =   { -- Exp and level
                                    {{1,999};{2,1999};{3,3199};{4,4599};{5,6199};{6,7999};{7,9999};{8,12199};{9,14699};{10,17499};{11,20599};{12,24319};{13,28783};{14,34139};{15,40566};{16,48278};{17,57532};{18,68636};{19,81960};{20,97948};{21,117133};{22,140155};{23,167781};{24,200932};{25,240713};{26,288450};{27,345734};{28,414474};{29,496962};{30,595947};{31,714729};{32,857267};{33,1028312};{34,1233566};{35,1479870};{36,1775434};{37,2130110};{38,2555721};{39,3066454};{40,3679333};{41,4414787};{42,5297331};{43,6356383};{44,7627245};{45,9152279};{46,10982319};{47,13178367};{48,15813624};{49,18975932};{50,22770701};{51,27324423};{52,32788889};{53,39346248};{54,47215078};{55,56657674};{56,67988789};{57,81586127};{58,97902932};{59,117483098};{60,140979297};{61,169174735};{62,203009260};{63,243610690};{64,292332406};{65,350798465};{66,420957735};{67,505148859};{68,606178207};{69,727413424};{70,872895684};{71,1047474396};{72,1256968850};{73,1508362194};{74,1810034206}};--0
                                    {{1,999};{2,1999};{3,3199};{4,4599};{5,6199};{6,7999};{7,9999};{8,12199};{9,14699};{10,17499};{11,20599};{12,24319};{13,28783};{14,34139};{15,40566};{16,48278};{17,57532};{18,68636};{19,81960};{20,97948};{21,117133};{22,140155};{23,167781};{24,200932};{25,240713};{26,288450};{27,345734};{28,414474};{29,496962};{30,595947};{31,714729};{32,857267};{33,1028312};{34,1233566};{35,1479870};{36,1775434};{37,2130110};{38,2555721};{39,3066454};{40,3679333};{41,4414787};{42,5297331};{43,6356383};{44,7627245};{45,9152279};{46,10982319};{47,13178367};{48,15813624};{49,18975932};{50,22770701};{51,27324423};{52,32788889};{53,39346248};{54,47215078};{55,56657674};{56,67988789};{57,81586127};{58,97902932};{59,117483098};{60,140979297};{61,169174735};{62,203009260};{63,243610690};{64,292332406};{65,350798465};{66,420957735};{67,505148859};{68,606178207};{69,727413424};{70,872895684};{71,1047474396};{72,1256968850};{73,1508362194};{74,1810034206}};--1
                                    {{1,999};{2,1999};{3,3199};{4,4599};{5,6199};{6,7999};{7,9999};{8,12199};{9,14699};{10,17499};{11,20599};{12,24319};{13,28783};{14,34139};{15,40566};{16,48278};{17,57532};{18,68636};{19,81960};{20,97948};{21,117133};{22,140155};{23,167781};{24,200932};{25,240713};{26,288450};{27,345734};{28,414474};{29,496962};{30,595947};{31,714729};{32,857267};{33,1028312};{34,1233566};{35,1479870};{36,1775434};{37,2130110};{38,2555721};{39,3066454};{40,3679333};{41,4414787};{42,5297331};{43,6356383};{44,7627245};{45,9152279};{46,10982319};{47,13178367};{48,15813624};{49,18975932};{50,22770701};{51,27324423};{52,32788889};{53,39346248};{54,47215078};{55,56657674};{56,67988789};{57,81586127};{58,97902932};{59,117483098};{60,140979297};{61,169174735};{62,203009260};{63,243610690};{64,292332406};{65,350798465};{66,420957735};{67,505148859};{68,606178207};{69,727413424};{70,872895684};{71,1047474396};{72,1256968850};{73,1508362194};{74,1810034206}};--2
                                    {{1,999};{2,1999};{3,3199};{4,4599};{5,6199};{6,7999};{7,9999};{8,12199};{9,14699};{10,17499};{11,20599};{12,24319};{13,28783};{14,34139};{15,40566};{16,48278};{17,57532};{18,68636};{19,81960};{20,97948};{21,117133};{22,140155};{23,167781};{24,200932};{25,240713};{26,288450};{27,345734};{28,414474};{29,496962};{30,595947};{31,714729};{32,857267};{33,1028312};{34,1233566};{35,1479870};{36,1775434};{37,2130110};{38,2555721};{39,3066454};{40,3679333};{41,4414787};{42,5297331};{43,6356383};{44,7627245};{45,9152279};{46,10982319};{47,13178367};{48,15813624};{49,18975932};{50,22770701};{51,27324423};{52,32788889};{53,39346248};{54,47215078};{55,56657674};{56,67988789};{57,81586127};{58,97902932};{59,117483098};{60,140979297};{61,169174735};{62,203009260};{63,243610690};{64,292332406};{65,350798465};{66,420957735};{67,505148859};{68,606178207};{69,727413424};{70,872895684};{71,1047474396};{72,1256968850};{73,1508362194};{74,1810034206}};--3
...


I'm not arguing as I am in a position of weakness, just trying to code some events.



Thanks



2017-03-30_18-19-43.png
 Description:
 Filesize:  26.43 KB
 Viewed:  4270 Time(s)

2017-03-30_18-19-43.png


Back to top
View user's profile Send private message Yahoo Messenger
Zanzer
I post too much
Reputation: 119

Joined: 09 Jun 2013
Posts: 3217

PostPosted: Thu Mar 30, 2017 7:22 pm    Post subject: Reply with quote

That code is correctly indexing the Stats variable, which is a table.
Code:
for _,__table in pairs(self.Data.Stats[i]) do

You tried to index the Characters variable, which is a string.
Code:
for _,__table in pairs(self.Data.Characters[i]) do


That data looks more like the level number and the maximum experience.
Level 1 takes you to 999 experience. Level 2: 1999. Level 3: 3199.
And it's the same list over and over again, which seems... useless.

Your third box would always be populated with the same data, so just populate it once, during startup.

If there are indeed other leveling increments, you could make your table smaller by reusing the distinct tables.
Code:
local DefaultStats = {{1,999};{2,1999};{3,3199};...}
local OtherStats = {{1,10};{2,20};{3,30};...}
Stats = { DefaultStats; DefaultStats; DefaultStats; OtherStats; DefaultStats; }


As far as selecting the right index, you may be able to get away with:
Code:
local i = sender.ItemIndex + 1
self.listview.clear();
for _,__table in pairs(self.Data.Stats[i]) do
   local entry = self.listview.getItems().add();
   entry.Caption = __table[1];
   local subentry = entry.getSubItems().add(__table[2]);
end
Back to top
View user's profile Send private message
bknight2602
Master Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 355

PostPosted: Thu Mar 30, 2017 8:33 pm    Post subject: Reply with quote

Zanzer wrote:
That code is correctly indexing the Stats variable, which is a table.
Code:
for _,__table in pairs(self.Data.Stats[i]) do

You tried to index the Characters variable, which is a string.
Code:
for _,__table in pairs(self.Data.Characters[i]) do


That data looks more like the level number and the maximum experience.
Level 1 takes you to 999 experience. Level 2: 1999. Level 3: 3199.
And it's the same list over and over again, which seems... useless.

For this case I would agree, but it is a specific case the general case would have different numbers. So useless now, not necessarily for all time.
Quote:

Your third box would always be populated with the same data, so just populate it once, during startup.

Again for this case the data would be the same but not as a general rule.
Quote:


If there are indeed other leveling increments, you could make your table smaller by reusing the distinct tables.
Code:
local DefaultStats = {{1,999};{2,1999};{3,3199};...}
local OtherStats = {{1,10};{2,20};{3,30};...}
Stats = { DefaultStats; DefaultStats; DefaultStats; OtherStats; DefaultStats; }

That seems doable.
Quote:


As far as selecting the right index, you may be able to get away with:
Code:
local i = sender.ItemIndex + 1
self.listview.clear();
for _,__table in pairs(self.Data.Stats[i]) do
   local entry = self.listview.getItems().add();
   entry.Caption = __table[1];
   local subentry = entry.getSubItems().add(__table[2]);
end
Back to top
View user's profile Send private message Yahoo Messenger
bknight2602
Master Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 355

PostPosted: Fri Mar 31, 2017 1:24 pm    Post subject: Reply with quote

bknight2602 wrote:
Zanzer wrote:
...

As far as selecting the right index, you may be able to get away with:
Code:
local i = sender.ItemIndex + 1
self.listview.clear();
for _,__table in pairs(self.Data.Stats[i]) do
   local entry = self.listview.getItems().add();
   entry.Caption = __table[1];
   local subentry = entry.getSubItems().add(__table[2]);
end
Actually I was looking for whatever control/item that could be derived from selecting the Character Listview, setting heroname = to that item.
Back to top
View user's profile Send private message Yahoo Messenger
Zanzer
I post too much
Reputation: 119

Joined: 09 Jun 2013
Posts: 3217

PostPosted: Sat Apr 01, 2017 7:52 am    Post subject: Reply with quote

Code:
local i = sender.ItemIndex
heroname = trainer.Data.Characters[i]
Back to top
View user's profile Send private message
bknight2602
Master Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 355

PostPosted: Sat Apr 01, 2017 10:04 am    Post subject: Reply with quote

Zanzer wrote:
Code:
local i = sender.ItemIndex
heroname = trainer.Data.Characters[i]


That results in
(Error executing this tables lua script: Undefined lua error)-->as a pop-up window
error attempt to index global 'sender' (a nil value)-->in the debug window
at table load.
EDIT:
As a side note, although i isn't set to anything, that I can find, a print statement of heroname results in Valeska ItemIndex 1 after the sender line is commented out.
Back to top
View user's profile Send private message Yahoo Messenger
Zanzer
I post too much
Reputation: 119

Joined: 09 Jun 2013
Posts: 3217

PostPosted: Sat Apr 01, 2017 1:23 pm    Post subject: Reply with quote

That line needed to be in the list's OnClick event.
If heroname is populating, it sounds like other code already is doing this functionality.
Back to top
View user's profile Send private message
bknight2602
Master Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 355

PostPosted: Sun Apr 02, 2017 4:57 pm    Post subject: Reply with quote

Excellent. Good eye and/or experience.
Thanks
Back to top
View user's profile Send private message Yahoo Messenger
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