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 Data Base To Table Name

 
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: Mon Apr 29, 2019 1:23 am    Post subject: Get Data Base To Table Name Reply with quote

Hi there,

How this should be work.

table1 = {1,2,3}
table2 = {4,5,6}
level = 1

I can't use / not work:

Code:
if  level == 1 then
 tbl = table1
else
 tbl = table2
end


With a helper function:

Code:
local function getTableName(tbl)
  for k, v in pairs(_G) do
    if v == tbl then
          return k
    end
  end
  return nil
end

tbl = getTableName(table1)

if tbl == 'table1' then
--- assign a current table to table1
--- get data from table1
--- do some kinds of stuff
else
--- assign a current table to table2
--- get data from table2
--- do some kinds of stuff
end


With helper function, it should work but I think that not effective.
How to assign tbl = [assigned table] should work?

Regards,

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed May 01, 2019 9:14 pm    Post subject: Reply with quote

Personally, I think you should probably go about this in a different manner.

While you can get the variable names different ways, it comes with the hit of performance, especially when it requires the use to the 'debug' library that Lua comes with. Since that kind of stuff is meant only for debugging code and finding problems, it shouldn't be used in real-world code unless specifically required.

This is the kind of thing that can be resolved approaching the problem a different way instead.

You can pass a more defined object that contains both the name of the object and the data itself, which would probably be better suited for this. For example, something like this:

Code:
table1 = {
    name = 'table1',
    data = { 1, 2, 3 }
};
table2 = {
    name = 'table2',
    data = { 3, 4, 5 }
};


Then you can access the name as it will always be tied to the object.

Based on your code above as well you can then setup things like:
Code:
levels = {
    [1] = table1,
    [2] = table2,
};

if (level == 1) then
    tbl = levels[1];
else
    tbl = levels[2];
end


Assuming you stick with a proper indexed table for levels too, you can just replace the if/then block with direct indexing or table lookups. You can also build out the levels table to include an id for the level and attach the data table to that, then use a helper function to do a simple lookup to get a level entry.

Code:

levels = {
    { id = 1, level = table1 },
    { id = 2, level = table2 },
};

local function getLevel(id)
    for _, v in pairs(levels) do
        if (v.id == id) then
            return v.level;
        end
    end
    return nil;
end

_________________
- 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 May 03, 2019 8:45 am    Post subject: Reply with quote

Nah, atom0s thank so much for details explanation and I clearly understand. Great...
_________________
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