Proper way to subscribe and unsubscribe from events in JavaScript?

Hi and thanks for your time.

I am using the GameCenter plug in and it has events I can listen for. To the best of my understanding they should be set up like this:

Function OnEnable(){GameCenterManager.playerAuthenticated += playerAuthenticated;}

Then a function to react when they fire like so:

function playerAuthenticated()
	print("Player JUST Authenticated with GameCENTER");
	FailedReport = false;

These both seem to work fine but another aspect of them is that you need to unsubcribe for some reason as well, which I do like so:

function OnDisable(){GameCenterManager.playerAuthenticated -= playerAuthenticated;}

The problem is that unity tosses up a yellow error that reads like, “Assets/Scripts/ManagerLevList.js(1038,51): BCW0005: WARNING: Unsubscribing from event ‘GameCenterManager.reportAchievementFailed’ with an adapted method reference. Either change the signature of the method to ‘callable(String) as void’ or use a cached reference of the correct type.”

I don’t understand the error, can someone provide advice please, and even better, imagine you are talking to a child that hasn’t taken programming classes but needs a specific answer. :slight_smile: To tell the truth, so far I’ve been ignoring it and everything seems okay…

While I have no idea about GameCenter and I’m not sure about the ‘cached reference’ bit, a quick Google search came up with this forum thread, wherein someone experiences a similar warning.
About a third of the way into the page, Prime31 responds with “for example you have the following combination which is not valid:” and points out that the delegate type has a string parameter.
Maybe this holds true for your case as well?

I’m not sure why it wouldn’t generate a similar warning when adding the method to begin with, but that may be something with how the delegates are implemented. If something else is up, I’d probably go ahead and check directly with Prime31, who seems to be in charge of GameCenter, they’d probably be the most help :smiley:

Also, as for why you’re unsubscribing from the events, it’s just what you’d do when you no longer wish to ‘listen’. If performance isn’t an issue with these events, you could alternately just store a private flag on whether to do anything when the event or events pop up, if unsubscribing is causing an issue.

Hope this helps.

No offense to Warwick but when did Unity Answers get to be so useless? I used to at least get some action on questions but it’s gone way downhill since. Yes, I know that this isn’t what Answers is for but to me it’s become nearly useless anyways.