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: 
jbrave
Channel Surfer

shared global object?

My app has several modules which each have their own global object through which I call functions and also which contains important variables. When I call a function from this object: x=obj.function(value) the object is available within that function as an "m" variable. I also have some things I assign when main first runs such as m.isplaying=false, m.audioplayer=createobject("roaudioplayer"). Now I want to create one global variable that is available to all the other objects. I've got glob=initglob() which creates the object, but this value ceases to exist once the subs are called. I tried passing it into the sub directly as a parameter, but this doesn't seem to work either.

any ideas? if it isn't clear, an example would be m.audioplayer being a valid variable in all contexts. Now that I moved all the assignments to main, it doesn't exist anymore in any subroutines
- Joel
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
7 REPLIES 7
TheEndless
Channel Surfer

Re: shared global object?

I accomplish this through a global function that returns the global m. object..

Function Config() As Object
Return m.Configuration
End Function

Since the Config() function is not associated with the object that's calling it, it runs in the global context, and therefore has access to the global m. object.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
jbrave
Channel Surfer

Re: shared global object?

So you just have one object (to rule them all)?

- Joel
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
TheEndless
Channel Surfer

Re: shared global object?

No, I have multiple objects, but calls to functions that aren't associated with a specific object are executed in the "global m." context.
As a basic example...
Sub RunUserInterface()
m.Value = "global value"
myObj = NewObject()
myObj.GetValues()
End Sub

Function GetGlobalValue() As String
Return m.Value
End Function

Function NewObject() As Object
obj = {
Value: "local value"
GetValues: Object_GetValues
}
Return obj
End Function

Sub Object_GetValues()
' GetGlobalValue is not a member of this object
' so it pulls it's value from the global m object
globalValue = GetGlobalValue()

' We're inside a method of this object,
' so m.Value returns the local m object
localValue = m.Value
?globalValue
?localValue
End Sub
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
jbrave
Channel Surfer

Re: shared global object?

Well, I'm still trying to figure this out... the trouble started when I switched this:

function initplayer() 
m.isplaying=false
m.ispaused=false
m.audioplayer=createobject("roaudioplayer")
'm.audioplayer.addheader("x-roku-reserved-dev-id","")
m.historyList=createobject("roarray",9,true)
end function


to this:

sub main()
glob=initplayer()
moduleone()
end sub

function initplayer() as object
return {isplaying:false,ispaused:false,audioplayer:createobject("roaudioplayer"),historylist:createobject("roarray",9,true)}
end function



then when I call:

moduleone()
modobj=initobj()


I no longer have m.audioplayer or any other m variable.

Every function I call: modobj.funcname()

used to have m referencing modobj, now it references nothing...

I switched it back, and everything is working, but I don't really understand why glob doesn't translate to m in moduleone() and modobj doesn't translate to m in functions called from modobj.funcname()

shouldn't any function called as a method of an object reference that object as m? That was working before I made the switch. Shouldn't a function called from that function reference the preceding function as m and include both parent functions in the cumulative list of globals?

- Joel
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
TheEndless
Channel Surfer

Re: shared global object?

In your first initplayer(), you're setting properties on the global m. object. In your second initplayer(), you're creating a brand new object with its own properties. So, with the first, you'd be able to use m.audioplayer from any global function, but with the second, you'd need use glob.audioplayer, and only in the main function, since you're not setting a global reference to it.

You didn't list what initobj() does, so I can't say what's going on in there, but assuming you assigned functions directly to that object, then yes m should refer to itself within those functions, and not to the global m.

On that note, I think I may need to defer to someone else to try to explain it, as I think I'm just going to end up confusing you more... Maybe let us know what you primary programming language is, and it can be better explained in those terms.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
jbrave
Channel Surfer

Re: shared global object?

Actually, this does help. Just one more question, how might I properly set a global reference to glob.audioplayer? Actually, I'm trying right now to create a global search history object that will be available everywhere as m.searchhist. It seems to make it down one level of context, but not down to the next. (By the time the search function is called: modobj.search() again, it doesn't exist anymore. I'm getting around that by passing m.searchhist directly to the search subroutine, like modobj.search(m.searchhist), but it would be good to understand how to do it a different way, I'm evolving my programming from what was originally tons of procedural code to more modular OO style, which I never used before Brightscript. My prior understanding of globals was "define once use anywhere", but clearly that is not the case here.

- Joel
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
TheEndless
Channel Surfer

Re: shared global object?

See my first example for how to access the global object from other objects. You need to make a call to a function that's associated with the global context, where your global object is stored. As for making global reference to glob.audioplayer, the same applies...
Sub Main()
m.glob = initobj
End Sub

Function GetGlob() As Object
Return m.glob
End Function

Then you can call GetGlob() from pretty much anywhere in your code to get the global m.glob object.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos