Having Trouble With Preference Library

Hi everyone,

 

I am using the preference library to save some data and it works great with the examples they have posted. But I want to save some string that is from a variable. So I did the following and it returns nil. Can anyone tell me why? Or how to use a variable here?

 

Here is the link to the page:  https://github.com/SatheeshJM/Lua-Preference-Library

 

Thanks!

 

--Store strings local apple = "abc" preference.save{b=apple} value = preference.getValue(b) print("Retrieving string : ",value)

The example on the page shows how to use it like this:

 

local preference = require "preference" --Store numbers preference.save{a=1} value = preference.getValue("a") print("Retrieving number : ",value) --Store strings preference.save{b="1"} value = preference.getValue("b") print("Retrieving string : ",value) --Store Boolean preference.save{c=true} value = preference.getValue("c") print("Retrieving boolean : ",value) --Store Tables preference.save{d = {1,"2",true}} value = preference.getValue("d") print("Retrieving table : ",value)

I am using the same way as storing strings but using a variable instead. How can I do this?

 

Thanks!!!

 

 

Judging by the code you’ve shown, instead of saying:

[lua]value = preference.getValue( b )[/lua]

 

Try:

[lua]value = preference.getValue( “b” )[/lua]

 

That should access the stored value by its “dictionary” reference name. Simply b (without quotes) is not known once you’ve placed it in a table like you did in the previous line.

 

Brent

Judging by the code you’ve shown, instead of saying:

[lua]value = preference.getValue( b )[/lua]

 

Try:

[lua]value = preference.getValue( “b” )[/lua]

 

That should access the stored value by its “dictionary” reference name. Simply b (without quotes) is not known once you’ve placed it in a table like you did in the previous line.

 

Brent

How can I retrieve the preferences?

sorry I am a little new at this but I’m confused. 

the first time the program runs there is no preferences to load

i can run the program and set the preferences but I don’t know how to initially set them before they are created.

i’m thinking something like

if (preference.getValue( “b” )) then

set preference

else preferences= defaultPreferences

end

thanks for any help

Hi Jim,

I recommend that you begin with a Lua lesson on tables and how to create/index and access items in them. The following URL is quite useful in this regard:

http://lua-users.org/wiki/TablesTutorial

Take care,

Brent Sorrentino

Hi Jim, 

Try this.

 [lua]

   local runOnce  = preference.getValue(“runOnce”)    

    if runOnce == nil then

         --Create all your initial settings here. Example,

        preference.save{playerLevel=“easy”}

        

        – change runOnce to true or 1 and save.

        preference.save{runOnce= true}    

        

    else

        – next time it runs here

    end

    [/lua]

Cheers!

burhan

Thank You Burhan, That is what I was looking for

How can I retrieve the preferences?

sorry I am a little new at this but I’m confused. 

the first time the program runs there is no preferences to load

i can run the program and set the preferences but I don’t know how to initially set them before they are created.

i’m thinking something like

if (preference.getValue( “b” )) then

set preference

else preferences= defaultPreferences

end

thanks for any help

Hi Jim,

I recommend that you begin with a Lua lesson on tables and how to create/index and access items in them. The following URL is quite useful in this regard:

http://lua-users.org/wiki/TablesTutorial

Take care,

Brent Sorrentino

Hi Jim, 

Try this.

 [lua]

   local runOnce  = preference.getValue(“runOnce”)    

    if runOnce == nil then

         --Create all your initial settings here. Example,

        preference.save{playerLevel=“easy”}

        

        – change runOnce to true or 1 and save.

        preference.save{runOnce= true}    

        

    else

        – next time it runs here

    end

    [/lua]

Cheers!

burhan

Thank You Burhan, That is what I was looking for