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: 
Komag
Level 9

Trying to learn "m", need conceptual help

So for a while now I've had this nagging feeling that I REALLY should be using "m". So far I haven't used it, I pass all the variables I need around all over the place, and I'm getting kind of tired of it. I have a LOT of info that I've broken down into a few main associative arrays which I pass to various functions, and I can draw info from them and change any values whenever I want.

Question - If I somehow put everything into "m", would that use more memory somehow? Not sure how garbage collector would handle stuff.
Question - How do I even do that?

The Brightscript Language Reference says:
A BrightScript object is an roAssociativeArray which contains function pointers. When a member function is called "from" an AssociativeArray, the special variable "m" is set to point to that AssociativeArray. "m" is accessible inside the called function to access other data in the AssociativeArray object.
I don't understand that at all (almost). :?

I thought I read somewhere that there are times you can't use "m"
The other relevant quote seems to be:
BrightScript does not support global variables. Except, there is one hard-coded global variable "global" that is an interface to the global BrightScript Component. The global component contains all global library functions. There is also a global context that can be accessed via the GetGlobalAA(). If in function scope and that function is not a method in an object, "m." also references the global associative array accessed with GetGlobalAA().
That too is a mystery to me, mostly. :shock:

Question - If I use "m" for "everything", it would create an additional layer for me, such as "m.cAA.mainMenu.w[1]" vs "cAA.mainMenu.w[1]". Everything would have this extra layer. Would that slow things down, since there is another dictionary lookup happening, or am I totally understanding that wrong?

As you can see I feel a bit lost on this. Smiley Indifferent
0 Kudos
19 Replies
squirreltown
Level 9

Re: Trying to learn "m", need conceptual help

It's just an associative array, but you don't need to pass it around, its built into BS.

m.thing = 0

Now I can use m.thing anywhere in any function.

It's the same as doing

varr={ thing:0}

Sub myFunction( varr as object)

varr.thing = varr.thing+1

End Sub


I think there are some who frown on using it, but I can't see why so they can weigh in themselves.
As it's just an array, I don't think memory applies any differently, m.bitmap still behaves as any bitmap would.
Kinetics Screensavers
0 Kudos
NewManLiving
Level 7

Re: Trying to learn "m", need conceptual help

viewtopic.php?f=34&t=83642#p479321
Gave an example here. It might help
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos
RokuMarkn
Level 7

Re: Trying to learn "m", need conceptual help

Be aware that that are (somewhat confusingly) two different meanings of m. If you are in a function called through an AA, then m refers to that AA. For example:

aa = { func1: my_func1, data: 42 }
aa.func1(5) ' call through an AA

function my_func1(x as Integer)
' x is 5, m.data is 42
end function


On the other hand if you are NOT in such a function, them m refers the the "global" AA:

m.data = 43 ' sets the global AA
func2(5) ' not called through an AA

function func2(x as Integer)
' x is 5, m.data is 43
end function


The first mechanism is how Brightscript implements object-oriented programming, and is something you should definitely use. The second mechanism (global variables) is evil and should be used with caution, if ever.

--Mark
0 Kudos
MatroxRT
Level 7

Re: Trying to learn "m", need conceptual help

That is probably the best summary I have seen Mark. It might be worth adding it to section 4.11 of the Language Ref.

I was just describing this yesterday to a co-worker, and did not do it as gracefully as your post. Smiley Happy
0 Kudos
TheEndless
Level 7

Re: Trying to learn "m", need conceptual help

Think of "m" as BrightScript's "this". It's a reference to the current object instance. If used outside of an object instance, it refers to the global 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
Komag
Level 9

Re: Trying to learn "m", need conceptual help

These responses are helping, good.
"RokuMarkn" wrote:
The second mechanism (global variables) is evil and should be used with caution, if ever.
I like the phrasing! But can you shed any light on why it would be wise to not use it this way much?

- Suppose I used it a lot - do I run the risk of my game breaking with some future Roku firmware update?
- Does "m" used as global variables array lead to other stability problems or memory management issues if it grows too big?
- Is it just "bad programming" but otherwise safe and harmless?
0 Kudos
RokuMarkn
Level 7

Re: Trying to learn "m", need conceptual help

The latter. It's bad programming style, and avoiding globals is one of the reasons Object Oriented programming was invented. I discussed this previously here. Other than poor style and difficulty in maintaining the code, it's not going to break anything if you use globals.

--Mark
0 Kudos
Komag
Level 9

Re: Trying to learn "m", need conceptual help

Thanks for the clarification.
I need to better understand object oriented programming then to make best use, it sounds like.

What about this question though:
"Komag" wrote:
Question - If I use "m" for "everything", it would create an additional layer for me, such as "m.cAA.mainMenu.w[1]" vs "cAA.mainMenu.w[1]". Everything would have this extra layer. Would that slow things down, since there is another dictionary lookup happening, or am I totally understanding that wrong?
0 Kudos
NewManLiving
Level 7

Re: Trying to learn "m", need conceptual help

It would all depend on how the compiler resolves references. If it's a good compiler then it will resolve the reference at compile time. It would be nice to have an official word.
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos