Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
btpoole
Level 7

Save Info To Registry

How is it possible to save some basic, small amount of data to the registry. I wish to store a zip code to be retrieved when the app is loaded. Since you can't write to a file and retain it after the app closes, the only way to save it is in the registry, but how?
Thanks

I have looked at the sdk and see what it says on creating a section in the registry to save info to and also the example on reading it. But how do you assign the info back to say a variable to work with it. For example, I used the following to save the info to the registry, the result comes from user input thru the on screen keyboard.

      
sec = CreateObject("roRegistrySection", "ZIPCODE")
sec.Write("UserZipCode", result)
sec.Flush()


Now, how do I get the info back, if it is even there? I have tried but with no success.

    sec = CreateObject("roRegistrySection", "ZIPCODE")
if sec.Exists("UserZipCode")
return sec.Read("UserZipCode")
zip=sec."ZIPCODE"
endif
return invalid


Thanks again
0 Kudos
19 Replies
destruk
Level 10

Re: Save Info To Registry

- in the SDK -
'******************************************************
'Registry Helper Functions
'******************************************************
Function RegRead(key,section=invalid)
If section=invalid section="Default"
sec=CreateObject("roRegistrySection",section)
If sec.Exists(key) Return sec.Read(key)
Return invalid
End Function

Function RegWrite(key,val,section=invalid)
If section=invalid section="Default"
sec=CreateObject("roRegistrySection",section)
sec.Write(key,val)
sec.Flush() 'commit it
End Function

Function RegDelete(key,section=invalid)
If section=invalid section="Default"
sec=CreateObject("roRegistrySection",section)
sec.Delete(key)
sec.Flush()
End Function

so to read and store in a variable you would use variablename=regread("keyname","registrysectionname")
to write the variable back you would use regwrite("keyname",data to write,"registrysectionname")
to delete the value from the registry is regdelete("keyname","registrysectionname")

AFAIK the roku registry saves/reads all data as a string. So if your zipcode is numeric, you might need to change it to a string first before writing - but definitely after reading it will be in string form and you'll need to convert it back to an integer if you are using it as an integer.
In addition to storing the data to the roku registry (limited to 16KB), you could fairly easily send the data to your server and have the server store it in a database or text file or something on the server. This way if your client has 4 or 5 rokus, the value of this data will persist between physical devices instead of being limited to the roku they used to save to the registry - it's how NetFlix and Amazon do it - saving data to their servers.
btpoole
Level 7

Re: Save Info To Registry

Thanks a ton for the example and info. At one time I was writing the info back to the server but was trying to keep everything possible on the box.

Thanks again
0 Kudos
Komag
Level 9

Re: Save Info To Registry

I'm just now trying to learn using registry, so this is helpful. But I don't understand the
section=invalid
bits, that just seems odd to me, how does that literally work?

It also seems odd to me to always be creating the registry sections with the same name each time the function are run - wouldn't you want to just create each section once?
0 Kudos
destruk
Level 10

Re: Save Info To Registry

The section=invalid sets a default value if it's not specified when calling the function.
It's also not 'creating the section' - it's specifying the section, which is required if you use more than one section.
I'd think of it this way - suppose you use the registry for saving bookmarks for video, saving user data like email address, name, and user number.
I'd use one section for the bookmarks for the channel (independent of channel within the same devid) and a second section for the user data (shared between channels on the same devid)
This way I could sort the bookmarks, limit the number of bookmarks, etc, through the registry without modifying the user data using for each based on the section instead of having to track each key, date, video position, etc.
Sure, you could do it all in the same section, just it makes it a little more complicated as you eliminate stuff you don't want to mess with.
I think you could improve this code easily by setting the sec createobject line as an AA - but really in practice it's not much slower to create it as needed for the relatively slow operation of writing or reading or deleting a value from the registry.
0 Kudos
Komag
Level 9

Re: Save Info To Registry

Hmm, I think I'm starting to get it more.

How can we tell how much of the 16kb limit we've used up?
0 Kudos
destruk
Level 10

Re: Save Info To Registry

AFAIK when it fails to save data, it's full. lol
0 Kudos
Komag
Level 9

Re: Save Info To Registry

Blech!

Any clues on how this is really taken up?
For instance, is one character 1 byte?
Does each key/value pair take up some set amount of space?
Can we use keys/values of very long length, like big sentences or paragraphs?
Do I just have to test all this myself and see what works?

I'll snap to it...
0 Kudos
RokuMarkn
Level 7

Re: Save Info To Registry

Well, it's a little complicated. Each registry entry stores the name and value, with a two byte header in front of each (so 4 extra bytes per entry). In addition there is a small header for each registry section (2 bytes plus the section name I think), and another header for the whole registry (about 30 bytes). However, the whole thing is zlib compressed before storing it, and it's the compressed file that is limited to 16K. This makes it rather difficult to predict exactly how much space you will be using. I'm not sure how knowing the exact size would be helpful, unless you're caching things in the registry that can be obtained elsewhere.

--Mark
0 Kudos
Komag
Level 9

Re: Save Info To Registry

I appreciate the detailed insight into space usage with the extra bytes here and there. Basically I'm implementing a save game file using the registry, with quite a bit of info, so I'm trying to get a feel for how much practical room I really have.

Any idea how much the zlib compression helps? 50%? Varies?
0 Kudos