Jump to content

System:SetProperty()


Rick
 Share

Recommended Posts

Ok, I see what you are saying that it doesn't appear to write to the cfg file until after game closes. But is still appears that setting a property in game allows for you to get that value via GetProperty() while in the same session. So what are you trying to do that requires it to write immediately?

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Interesting that when I send the results directly from GetProperty() to Print() it errors but if I assign it to a variable first it works.

 

System:Print(System:GetProperty("profile1"))

 

vs

 

 

local r = System:GetProperty("profile1")
System:Print(r)

 

 

 

The other thing is that I'm saving a table that I convert to string so it might be because of that. The jist that I'm trying to do is save a lua table to string, then read it back and convert it back to a table using loadstring() so I can treat this as a way to save game data, but so far the loadstring() is returning nil after reading the table back in. This is the code I'm using to convert table to string and it seems to work well.

 

function serializeTable(val, name, skipnewlines, depth)
skipnewlines = skipnewlines or false
depth = depth or 0

local tmp = string.rep(" ", depth)

if name then tmp = tmp .. name .. " = " end

if type(val) == "table" then
tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")

for k, v in pairs(val) do
tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
end

tmp = tmp .. string.rep(" ", depth) .. "}"
elseif type(val) == "number" then
tmp = tmp .. tostring(val)
elseif type(val) == "string" then
tmp = tmp .. string.format("%q", val)
elseif type(val) == "boolean" then
tmp = tmp .. (val and "true" or "false")
else
tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
end

return tmp
end

Link to comment
Share on other sites

Just taking a simple approach that seems to work okay for setting a table as one property in the config file then reading back in that property and setting to a table.

--Set table values as one key's value

function System:SetTableAsOneKey(t,name)

for k,v in pairs(t) do

t[k] = tostring(v)

end

local s = table.concat(t, "|")

s = s.."|"

System:SetProperty(name, s)

end

 

--Get one key's value and set as table

function System:GetOnePropertyAsTable(t,name)

local s = System:GetProperty(name)

local k = 1

for v in string.gmatch(s, "%w+") do

t[k] = v

k = k + 1

end

return t

end

 

--test code

mytable = {}

mytable[1] = "string1"

mytable[2] = true

mytable[3] = 545.0003

System:SetTableAsOneKey(mytable, "OneKey")

 

OneKey = {}

OneKey = System:GetOnePropertyAsTable(OneKey, "OneKey")

 

if (OneKey[2]=="true") then

OneKey[2] = true

else

OneKey[2] = false

end

enabled = OneKey[2]

if enabled==true then System:Print("ENABLED!") end

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Just curious, have you tried calling set in 1 run, and then only calling the get in a new run of the exe? The config file seems to be empty when I do that. If they are both in the same run it works, but that's not how we'd want it.

 

I have the standard Main() setup when testing this and I do it after the window is created (other GetProperty()'s called).

Link to comment
Share on other sites

and this is how i would approach setting a table as multiple keys:

--Set table as multiple properties

function System:SetAllTableProperties(t)

for k,v in pairs(t) do

System:SetProperty(k, tostring(v))

end

end

 

--Get properties as a table

function System:GetAllTableProperties(t)

for k,v in pairs(t) do

t[k] = System:GetProperty(k)

end

return t

end

 

--test code

enabled = true

mytable1 = {}

mytable1.color = "1,0,1,1"

mytable1.name = "testkey and value"

mytable1.boolean = enabled

System:SetAllTableProperties(mytable1)

 

mytable2 = {}

mytable2.name = 0

mytable2.boolean = 0

mytable2.color = 0

mytable2 = System:GetAllTableProperties(mytable2)

 

if (mytable2.boolean=="true") then

mytable2.boolean = true

else

mytable2.boolean = false

end

if mytable2.boolean==true then System:Print("ENABLED!") end

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Just curious, have you tried calling set in 1 run, and then only calling the get in a new run of the exe? The config file seems to be empty when I do that. If they are both in the same run it works, but that's not how we'd want it.

When I run just this:

--Set table values as one key's value

function System:SetTableAsOneKey(t,name)

for k,v in pairs(t) do

t[k] = tostring(v)

end

local s = table.concat(t, "|")

s = s.."|"

System:SetProperty(name, s)

end

 

--test code

mytable = {}

mytable[1] = "string1"

mytable[2] = true

mytable[3] = 545.0003

System:SetTableAsOneKey(mytable, "OneKey")

 

I get this in my cfg file:

OneKey=string1|true|545.0003|

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Right, so you are setting them there. Now restart the exe and only do the getting part. For me the config is empty when I do that.

 

Even if I don't do the reading part. It's like the config file gets cleared out each run. I would think the idea of a config file would be to maintain itself between runs.

Link to comment
Share on other sites

Right, so you are setting them there. Now restart the exe and only do the getting part. For me the config is empty when I do that.

So I run just this code now:

--Get one key's value and set as table

function System:GetOnePropertyAsTable(t,name)

local s = System:GetProperty(name)

local k = 1

for v in string.gmatch(s, "%w+") do

t[k] = v

k = k + 1

end

return t

end

 

OneKey = {}

OneKey = System:GetOnePropertyAsTable(OneKey, "OneKey")

 

System:Print(OneKey[1])

System:Print(OneKey[2])

System:Print(OneKey[3])

 

And I get this in the console:

string1
true
545

 

And my cfg file still has this in it:

OneKey=string1|true|545.0003|

 

My key/value property doesn't get written over unless i tell it to...

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Very strange. That's not what I'm experiencing.

Without seeing what you are doing I can't say what's different. Ive tried it sandboxed and not, and it makes no difference, the key doesn't get deleted or overwritten by just performing a GetProperty.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Wow, very strange stuff here. So I just did: System:SetProperty("test", "Rick") and it showed up and stayed up between runs when I commented it out after. Then I wrote my map table again which results in 451KB in size. I then closed the exe, commented out any set or get and reran. Once I closed again the file size went back down to 1KB and only had my test=Rick in it. Maybe it's a size thing of the file?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...