newbie needs help with error

Hi there. I’m very new to Roku development. I’m working on trying to get an overlay to display on the screen. I have some legacy code that I’m trying to move from one file to another. General algorithm in english:

  1. when there’s more than one channel
  2. and a user presses left or right button
  3. show the channel bar
  4. load the channel bar information

so far, we’re good through #2.

#2 calls a function:

showOverlay("channel bar")

and the function:


Function showOverlay(overlayType as string)
	InitOverlay()
	if overlayType = "channel bar" then show_channel_bar()
End Function

Sub show_channel_bar()
	if m.channelManager.liveChannels.Count() > 1 then
		print "show_channel_bar()"
		m.canvas.AllowUpdates(false)
		m.updateChannelBar()
		m.canvas.Show()
		m.canvas.AllowUpdates(true)
		m.channelBarActive = true
	end if
End Sub

The debugger is showing this error, however…
OverlayController.brs(44): runtime error f3: Interface not a member of BrightScript Component
044: m.canvas.AllowUpdates(false)

What am I missing here? FWIW, this same code works when in a different file. we’re looking to abstract it a bit and allow multiple types of overlays.

Thanks!

after a little more playing around, it looks as if the BrightScript “this” pointer isn’t available in my sub functions. I’m a little confused as to how this works.

I’ve updated my code a bit:


Function showOverlay(overlayType as string)
	overlay = InitOverlay()
	if overlayType = "channel bar" then overlay.showChannelBar()
End Function

Function InitOverlay() As Object
	print "InitOverlay()"
	this = {
		showChannelBar: show_channel_bar
		updateChannelBar: update_channel_bar
		channelBarActive: false
	}
	
	return this
End Function

Sub show_channel_bar()
	if m.channelManager.liveChannels.Count() > 1 then
		print "show_channel_bar()"
		m.canvas.AllowUpdates(false)
		m.updateChannelBar()
		m.canvas.Show()
		m.canvas.AllowUpdates(true)
		m.channelBarActive = true
	end if
End Sub

I’m not sure why the m. functionality isn’t working. Is there a tutorial on this? The brightscript reference doc is a little sparse here… Shouldn’t the m.channelManager be available from my previous file that was using it?

thanks!

InitOverlay() is essentially a class constructor and its return value will become the value of the m pointer for any operations you perform on it. So, when you say overlay.ShowChannelBar(), you are calling the ShowChannelBar() function of the overlay object and within the scope of ShowChannelBar(), m will point to your overlay object.

Building on what RokuChris said, within a function m is a special variable that equates to either a global associative array (if the function is called as a stand-alone function) or to the associative array the function is referenced from (if called as a method on an associative array).

Anywhere you see this used, it’s just a plain associative array. In the code you referenced, it’s used to build the object that’s returned. Using m from the building function would actually result in a singleton pattern.

E.g.


function printFoo()
    if m.exists("foo") then print m.foo
end function

obj = { foo: "bar", sayFoo: printFoo }

printFoo() ' does nothing, global "m" object has no "foo" element

obj.sayFoo() ' prints "bar", as in this case "m" refers to the "obj" associative array

I hope that clarifies the object system somewhat.

yes - thanks! I think I’m catching on a bit now