Well I'm here again with other problem.
I want to transform a string (e.g. "Character={lastname="Maciel",firstname="Rui",model="test.mdl",sex="male",}") into a table again.
I tried to use string.ToTable() but it returns me nil.
sv_functions.lua
[CODE]local meta = FindMetaTable("Player")
function meta:GetCharacterFile() return "rpcharacter/characters/" .. self:UniqueID() .. ".txt" end
function meta:HasCharacter() return file.Exists(self:GetCharacterFile(), "DATA") end
function meta:SetCharacter(firstname, lastname, sex, model)
local Character = {}
Character.firstname = firstname
Character.lastname = lastname
Character.sex = sex
Character.model = model
local CharacterString = table.ToString(Character, "Character", false)
file.Write(self:GetCharacterFile(), CharacterString)
end
function meta:GetCharacter()
if self:HasCharacter() then
return string.ToTable(file.Read(self:GetCharacterFile(), "DATA"))
else return {} end
end
function meta:GetFirstName() return self:GetCharacter().firstname end[/CODE]
sv_commands.lua
[CODE]concommand.Add("chdebug", function(ply)
if ply:IsPlayer() then
ply:SetCharacter("Rui", "Maciel", "male", "test.mdl")
print(ply:GetFirstName()) -- method is called from here and just prints nil on console
end
end)[/CODE]
If someone could help me would be great.
A lazy method would be to use util.TableToJSON and util.JSONToTable.
I changed to:
[CODE]function meta:SetCharacter(firstname, lastname, sex, model)
local Character = {}
Character.firstname = firstname
Character.lastname = lastname
Character.sex = sex
Character.model = model
file.Write(self:GetCharacterFile(), util.TableToJSON(Character, true))
end
function meta:GetCharacter()
if self:HasCharacter() then
return util.JSONToTable(file.Read(self:GetCharacterFile(), "DATA"))
else return {} end
end[/CODE]
and worked.
Thanks again ;)
Sorry, you need to Log In to post a reply to this thread.