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 


insert table string path into table as object

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

Joined: 29 Mar 2021
Posts: 64

PostPosted: Sun Mar 06, 2022 1:36 am    Post subject: insert table string path into table as object Reply with quote

ok this is kinda hard for me to explain but, i want to insert a table path for example "clothing.head.hat" into table name: "player".

player is just a normal table: {} without anything in it.

i want the path convert player table into:

Code:
-- from player = {} to \/\/

player = {
  clothing = {
    head = {
      hat = {}
    }
  }
}


how can i do this? i've tried multiple ways but i still cant get it to work.
Back to top
View user's profile Send private message
Frouk
Grandmaster Cheater
Reputation: 5

Joined: 22 Jun 2021
Posts: 512

PostPosted: Sun Mar 06, 2022 2:12 am    Post subject: Reply with quote

Code:
player = {
--Object
}

player.clothing.head.hat = "MODELS//HAT5.TXD"--ID or name
Back to top
View user's profile Send private message
HexaG0n
Advanced Cheater
Reputation: 0

Joined: 29 Mar 2021
Posts: 64

PostPosted: Sun Mar 06, 2022 2:25 am    Post subject: Reply with quote

Frouk wrote:
Code:
player = {
--Object
}

player.clothing.head.hat = "MODELS//HAT5.TXD"--ID or name


that will cause an error: attempt to index a nil value
well really thats why i made this thread
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25807
Location: The netherlands

PostPosted: Sun Mar 06, 2022 3:49 am    Post subject: Reply with quote

Code:

somestring='clothing.head.hat'

function addStringToTable(t,s)
  local pieces=table.pack(s:split('.'))
  for i=1,#pieces do
    t[pieces[i]]={}
    t=t[pieces[i]]
  end

  return t
end

player={}
addStringToTable(player,somestring)
player.clothing.head.hat = "MODELS//HAT5.TXD"--ID or name

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
HexaG0n
Advanced Cheater
Reputation: 0

Joined: 29 Mar 2021
Posts: 64

PostPosted: Sun Mar 06, 2022 6:51 am    Post subject: Reply with quote

Dark Byte wrote:
Code:

somestring='clothing.head.hat'

function addStringToTable(t,s)
  local pieces=table.pack(s:split('.'))
  for i=1,#pieces do
    t[pieces[i]]={}
    t=t[pieces[i]]
  end

  return t
end

player={}
addStringToTable(player,somestring)
player.clothing.head.hat = "MODELS//HAT5.TXD"--ID or name


thanks so much darkbyte, this works for me!

my code now:
Code:
local function setTablePathValue(tbl, path, index, value)
    for _,v in ipairs(path) do
        tbl[v] = tbl[v] or {}
        tbl = tbl[v]
    end

    index = (index or #tbl + 1)
    tbl[index] = value
    return tbl
end

local player = {}
setTablePathValue(player, {'clothing', 'head'}, 'hat', 'MODELS//HAT5.TXD')


i just modified the function abit though, i think i might just use the key list right away instead of making a key string. Very Happy
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