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: 
FCF-Ben
Reel Rookie

Setting environment variables in brigthscript (best practice)?

Jump to solution

There is one thing that I've been wondering, where do people set their environment variables, and is there anything like best practices. 

For example, we run two data environments, a dev and a production one, and it would make our lifeeasier if we could just have an environment file where we set DATA_SOURCE, and we can set it to something like

 

DATA_SOURCE="https://www.ourdomain.com/api/dev/v1.01"

 

Then in the code being able to do something like:

 

xfer.SetURL="$$DATA_SOURCE$$/get_content"

 

 

I looked at the manifest file, and there is a function (or option) called 

uri_resolution_autosub

https://developer.roku.com/en-gb/docs/developer-program/getting-started/architecture/channel-manifes... 

Which works this way, but it seems like a reserved word (or something like that). 
 
And I guess I can put it in an screengraph XML, but truth to be told, every time I've thought I've understood first thing about data scopes in Roku it throws a curveball and I feel rather lost (I'm also relatively new to Roku development). So was hoping there is a simple way of setting constant variables available anywhere just for environment settings. 

Thank so very much, and sorry if this isn't the right place to ask. 
 
 
0 Kudos
1 Solution

Accepted Solutions
RokuKC
Roku Employee
Roku Employee

Re: Setting environment variables in brigthscript (best practice)?

Jump to solution

There's at least a few different ways to run dev. vs. production environments.


One is you could write dynamic code to check roAppInfo IsDev(), which will return true for side-loaded channels, and then vary the runtime behavior based on that.

E.g.

appInfo = CreateObject("roAppInfo")
if appInfo.isDev()
    my_url = "http://something.dev"
else
    my_url = "http://something.prod"
end if


If you prefer to control behavior by changing a source file, you can use the manifest file for that.


One option is to use conditional compilation:

https://developer.roku.com/en-gb/docs/references/brightscript/language/conditional-compilation.md 

E.g. in the manifest file put:

bs_const=USE_DEV_SERVER=false

Then in .brs do:

#if USE_DEV_SERVER
    my_url = "http://whatever.dev"
#else
    my_url = "http://whatever.prod"
#end if


You can also put custom key entries in the manifest file, e.g.

my_url=http://something.dev

vs.

my_url=http://something.prod

where you change the manifest file between runs.


Then in code, you can retrieve the configured value using

appInfo = CreateObject("roAppInfo")
my_url = appInfo.GetValue("my_url") 

Note that although I mention it, I don't recommend this approach myself, and it can often cause confusion in development if you mix Roku OS-defined manifest attributes with your own custom entries.

A better approach is to just make your own configuration file, e.g. my_config.json, and read that from your Main function or wherever you need it.

Hope that helps.

View solution in original post

3 REPLIES 3
renojim
Community Streaming Expert

Re: Setting environment variables in brigthscript (best practice)?

Jump to solution

This is the place to ask, but I don't have a good answer for you.  One method that comes to mind is to save "constants" in the registry right at the start of Main/RunUserInterface and then you can access them anywhere.  It's not very elegant and hardly a simple substitution line the $$$ thing, but it works.

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.
RokuKC
Roku Employee
Roku Employee

Re: Setting environment variables in brigthscript (best practice)?

Jump to solution

There's at least a few different ways to run dev. vs. production environments.


One is you could write dynamic code to check roAppInfo IsDev(), which will return true for side-loaded channels, and then vary the runtime behavior based on that.

E.g.

appInfo = CreateObject("roAppInfo")
if appInfo.isDev()
    my_url = "http://something.dev"
else
    my_url = "http://something.prod"
end if


If you prefer to control behavior by changing a source file, you can use the manifest file for that.


One option is to use conditional compilation:

https://developer.roku.com/en-gb/docs/references/brightscript/language/conditional-compilation.md 

E.g. in the manifest file put:

bs_const=USE_DEV_SERVER=false

Then in .brs do:

#if USE_DEV_SERVER
    my_url = "http://whatever.dev"
#else
    my_url = "http://whatever.prod"
#end if


You can also put custom key entries in the manifest file, e.g.

my_url=http://something.dev

vs.

my_url=http://something.prod

where you change the manifest file between runs.


Then in code, you can retrieve the configured value using

appInfo = CreateObject("roAppInfo")
my_url = appInfo.GetValue("my_url") 

Note that although I mention it, I don't recommend this approach myself, and it can often cause confusion in development if you mix Roku OS-defined manifest attributes with your own custom entries.

A better approach is to just make your own configuration file, e.g. my_config.json, and read that from your Main function or wherever you need it.

Hope that helps.

FCF-Ben
Reel Rookie

Re: Setting environment variables in brigthscript (best practice)?

Jump to solution

Deleted, because I did not read properly before responding 🙂

 

0 Kudos