Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
dan19
Binge Watcher

How to write an Array to Registry in BrightScript

Jump to solution

Hello guys,

I am trying to write an array to a registry section. I don't know if this is possible, but it is neither reading back the value, nor throwing an error, so I thought to ask here.

Example of what I am trying to do:

someStates = ["Florida", "New York", "Texas"]

statesList = CreateObject("roRegistrySection", "statesList")
statesList.WriteMulti(someStates)
statesList.flush()
 
 
0 Kudos
1 Solution

Accepted Solutions
necrotek
Roku Guru

Re: How to write an Array to Registry in BrightScript

Jump to solution

Maybe I did not add enough code in the explanation to support your example

 

someStates = {state1:"Florida", state2:"New York", state3:"Texas"}
data=FormatJson(someStates)
?data
?type(data)

sec=CreateObject("roRegistrySection", "states")

sec.Write("statelist", data)

sec.Flush()

if sec.Exists("statelist")

statesFromRegistry=sec.Read("statelist")

 

StatesList=ParseJson(statesFromRegistry)

?StatesList

?type(StatesList)

end if 

View solution in original post

8 REPLIES 8
renojim
Community Streaming Expert

Re: How to write an Array to Registry in BrightScript

Jump to solution

Did you check the return value of WriteMulti?  If you had, you would see that it returns false.  It seems like it should throw an error since you're trying to save an array and you can only save an roAssociativeArray.

Something like this would work, but I don't know about its usefulness:

statesList = CreateObject("roRegistrySection", "statesList")
someStates = {state1:"Florida", state2:"New York", state3:"Texas"} ?statesList.WriteMulti(someStates) keysarray = ["state1","state2","state3"] ?statesList.ReadMulti(keysarray)

What it's doing is saving a bunch of keys at once in the "statesList" registry section, so something like statesList.Read("state2") would return "New York".

Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
dan19
Binge Watcher

Re: How to write an Array to Registry in BrightScript

Jump to solution

Thanks for replying to my question. I tried the solution given, but it didn't seem to work. 

Here's what I have done so far:

m.movieArray.push(m.selectedMovie)

m.allMovies = {"movie": m.movieArray}
  
  movieList = CreateObject("roRegistrySection", "movieList")
  movieList.WriteMulti(m.allMovies)
  movieList.flush()
  keysArray = ["movie"]
  movieList.ReadMulti(keysArray)
  ? movieList.Read("movie")

The idea for movieArray is to push all movies that someone has selected to save. I am new to Roku and this is just something I am practicing. There is no server to call or anything of such, and this is why I am using the registry to act as a DB.

What am I doing wrong in the code? Thanks.


@renojim wrote:

Did you check the return value of WriteMulti?  If you had, you would see that it returns false.  It seems like it should throw an error since you're trying to save an array and you can only save an roAssociativeArray.

Something like this would work, but I don't know about its usefulness:

statesList = CreateObject("roRegistrySection", "statesList")
someStates = {state1:"Florida", state2:"New York", state3:"Texas"} ?statesList.WriteMulti(someStates) keysarray = ["state1","state2","state3"] ?statesList.ReadMulti(keysarray)

What it's doing is saving a bunch of keys at once in the "statesList" registry section, so something like statesList.Read("state2") would return "New York".


 

0 Kudos
renojim
Community Streaming Expert

Re: How to write an Array to Registry in BrightScript

Jump to solution

First of all, you're still not checking the return value of WriteMulti which is still false.

I don't think there's any way to do what you're trying to do.  Keep in mind that the registry is nothing more than a a bunch of sections where each section has a set of keys with associated values - one value per key.  You're trying to save multiple values under the same key.  That's not possible.

Also, be aware that the registry is a limited resource.  I don't know how many movies you're planning on saving, but I'd suggest having a "most recently used" approach where there's a limited number of movies that can be saved and when all slots are filled you overwrite the oldest.

Because of its limitations, working with the registry can be a pain and you have to get clever.  I've used different approaches to save multiple values under one key like having fixed string lengths and concatenating multiple strings to save under one key, but in your case I'd just use one key for each movie, i.e., "movie1", "movie2", etc.

Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
dan19
Binge Watcher

Re: How to write an Array to Registry in BrightScript

Jump to solution

@renojim wrote:

First of all, you're still not checking the return value of WriteMulti which is still false.

I don't think there's any way to do what you're trying to do.  Keep in mind that the registry is nothing more than a a bunch of sections where each section has a set of keys with associated values - one value per key.  You're trying to save multiple values under the same key.  That's not possible.

Also, be aware that the registry is a limited resource.  I don't know how many movies you're planning on saving, but I'd suggest having a "most recently used" approach where there's a limited number of movies that can be saved and when all slots are filled you overwrite the oldest.

Because of its limitations, working with the registry can be a pain and you have to get clever.  I've used different approaches to save multiple values under one key like having fixed string lengths and concatenating multiple strings to save under one key, but in your case I'd just use one key for each movie, i.e., "movie1", "movie2", etc.


Thanks for you input and expertise. Good to know that it is just a thing with the registry setup. I will just do something else for just practice purposes. 

Thanks again!

0 Kudos
necrotek
Roku Guru

Re: How to write an Array to Registry in BrightScript

Jump to solution

You can also use Json since it is stored as a string


data=FormatJson(array)
sec.Write("registrySection", data)
sec.Flush()


data = sec.Read("registrySection")
array=ParseJson(data)

 

0 Kudos
dan19
Binge Watcher

Re: How to write an Array to Registry in BrightScript

Jump to solution

@necrotek wrote:

You can also use Json since it is stored as a string


data=FormatJson(array)
sec.Write("registrySection", data)
sec.Flush()


data = sec.Read("registrySection")
array=ParseJson(data)

 


BRIGHTSCRIPT: ERROR: FormatJSON: Value type not supported.
 
I don't think BRS likes this solution. Thanks any ways!
0 Kudos
necrotek
Roku Guru

Re: How to write an Array to Registry in BrightScript

Jump to solution

Maybe I did not add enough code in the explanation to support your example

 

someStates = {state1:"Florida", state2:"New York", state3:"Texas"}
data=FormatJson(someStates)
?data
?type(data)

sec=CreateObject("roRegistrySection", "states")

sec.Write("statelist", data)

sec.Flush()

if sec.Exists("statelist")

statesFromRegistry=sec.Read("statelist")

 

StatesList=ParseJson(statesFromRegistry)

?StatesList

?type(StatesList)

end if 

Komag
Roku Guru

Re: How to write an Array to Registry in BrightScript

Jump to solution

I use the FormatJson() and ParseJson() method extensively to save all sorts of data, including arrays.

 

For example:

FUNCTION saveMaze(pslot) ' trig by saveAll(1)
	mzUpdateTimer()
	mzInitTimer()
	mzSec		= {}
	sy		= m.sy
	cAA		= m.cAA
	MZ		= sy.MZ
	tmrs		= sy.tmrs
	autoP		= sy.autoP
	mzArr		= [
		cAA.facing		' 0		' player facing
		cAA.stats.hth		' 1		' player health
		cAA.invMz		' 2		' Simple inventory (number of each Item)
		tmrs.speedTime		' 3		' speed potion remaining time
		MZ.swdPwrCnt		' 4		' sword power remaining hits
		MZ.curDepth		' 5		' Correct depth
		
		MZ.mzDepthInt		' 6		' Any changed maze options
		MZ.mzBreadthInt		' 7
		MZ.mzTypeInt		' 8
		MZ.mzLightInt		' 9
		MZ.mzMonInt		' 10
		
		cAA.preDoor		' 11
		saveMzDoorList()	' 12		' An array of door numbers in doorL of open doors
		MZ.keyUsed		' 13		' Whether stair door is unlocked (go by MZ.keyUsed)
		
		MZ.mzRmtSel		' 14
		cAA.steps		' 15		' Steps player has walked ever
		cAA.resv		' 16		' Healing pool info Array
		saveMzPoolList()	' 17
		autoP.porchCrd		' 18
		autoP.poolFound		' 19
		saveMzAutoP()		' 20		' List of chambers with Int codes whether explored
		MZ.XP			' 21
		MZ.itmGot		' 22
		MZ.allGot		' 23
		MZ.sTot			' 24
		MZ.itmGotT		' 25
		MZ.monGotT		' 26
		MZ.stepsT		' 27
		
		' and autoPlay grid stepping history info, cham info of fully explored chams
		
		]
	mzSec.a		= mzArr
	str		= FormatJson(mzSec)		' Outputs a string
	'? str
	RegWrite("mz", str, pslot)			' Key (maze), Val, Section
	RETURN Len(str)
END FUNCTION
0 Kudos