Window.set_listener can't be nil? (DEF-2408) (SOLVED)

in documentation we have information tha callback parameter for a function window.set_listener cab be nil http://www.defold.com/ref/window/#window.set_listener:callback

But when I try to set nil (for remove a callback) I recieve next error:

ERROR:SCRIPT: collections/game/ui/in_game_ui.gui_script:151: bad argument #1 to ‘set_listener’ (function expected, got nil)

I don’t know, maybe it is mistake in documentation but then how to delete a callback?

This is a bug. You should be able to remove a listener, just like you can with iap and iac. You will have to set this to an empty function for now:

window.set_listener(function() end)

I’ve created a ticket for this: DEF-2408

1 Like

Yes, It’s a documentation bug. It’ll be fixed in the next release. Thanks for pointing this out.

1 Like

I take that back. It is not possible to unset the listener for iap, iac or push. After talking to @Mathias_Westerdahl and @sicher we’ve instead decided to update the documentation for window.set_listener() and our new recommendation is to pass in an empty function if you no longer wish to get callbacks.

1 Like

yes, but if I try to do this in final() function I recive:

ERROR:GAMESYS: Could not run Window callback because the instance has been deleted.
(collection unloaded)

Oh, ok, so you’re setting the window.set_listener in a script inside a collection that gets unloaded? The error has no real effect. It only logs an error in the console. The code will continue to run despite this. It doesn’t look good in the log but it will have no effect. If you have all your engine callback setup in a main/controller script or module you would not run into this error.

1 Like

ok, I will replace my current code to controller, but it looks stange that I can’t remove listener…

Library for work with the events

3 Likes

I believe the idea is that these listeners are app-wide so that, in general, you never need to remove them, but instead manage it’s behaviour depending on the state of the app. But it may be that we need to add remove_listener() functions. Can you describe your use case in a bit more detail?

2 Likes

I think you’re right, and it is just a my misunderstanding.

About my case:
I have few collection. Some collection has own behavior on the window events, for example

  • game collection - show user the pause window when lost focus;
  • some collection - change position of particles when change window size (workaround, because I can’t use particle in gui((( );
  • all collection - turn off music when lost focus and turn on when gained focus (on some old devices music plays on lock screen).

Of course I can make one controller with many if-else for checking current collection and check behavior.
But better way for me have all behavior into collection, not in external controller.

My first solution: window.set_listener in every collection in init() and remove_listener in final(). And It doesn’t work because we cant remove listener.

But now I wrote small routing library for the events and all work as I need.

2 Likes

Docs have been updated in Defold 1.2.97

2 Likes

thanks for the library.

1 Like

I hit this… it looks definitely strange that we can’t remove listener…