# Window.set\_listener can't be nil? (DEF-2408) (SOLVED)

**URL:** <https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995>\
**Category:** Bugs\
**Created:** [January 15, 2017, 9:10pm UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995 "2017-01-15T21:10:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [January 15, 2017, 9:10pm UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/1 "2017-01-15T21:10:24Z")

</div>

in documentation we have information tha callback parameter for a function window.set\_listener cab be nil [API reference (Window)](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?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 16, 2017, 8:03am UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/2 "2017-01-16T08:03:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [January 16, 2017, 8:09am UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/3 "2017-01-16T08:09:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 16, 2017, 8:12am UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/4 "2017-01-16T08:12:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [January 16, 2017, 9:13am UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/5 "2017-01-16T09:13:56Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 16, 2017, 9:19am UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/6 "2017-01-16T09:19:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [January 16, 2017, 9:22am UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/7 "2017-01-16T09:22:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [January 16, 2017, 1:17pm UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/8 "2017-01-16T13:17:24Z")

</div>

Library for work with the events

> [@Library for routing the window events](https://forum.defold.com/t/library-for-routing-the-window-events/4000):
>
> Today I [faced with impossibility to remove listener](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408/3995) on window events. Defold API has only window.set\_listener method for add callback and has no method to remove it. I made the library for simple adding and removing listeners to any script with message receiver (with on\_message method) for the window events: window.WINDOW\_EVENT\_FOCUS\_LOST window.WINDOW\_EVENT\_FOCUS\_GAINED window.WINDOW\_EVENT\_RESIZED [https://github.com/AGulev/ag\_window](https://github.com/AGulev/ag_window) function init(self) ... ag\_window.add\_focus\_listener(…

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [January 16, 2017, 1:25pm UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/9 "2017-01-16T13:25:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![AGulev](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/agulev/32/4541_2.png) [@AGulev](https://forum.defold.com/u/AGulev)\
**Post date:** [January 16, 2017, 1:40pm UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/10 "2017-01-16T13:40:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [February 13, 2017, 2:57pm UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/11 "2017-02-13T14:57:32Z")

</div>

> [@sicher](#):
>
> Yes, It’s a documentation bug. It’ll be fixed in the next release

Docs have been updated in Defold 1.2.97

---

<div class="post-metadata">

**Author:** ![Dragosha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dragosha/32/5103_2.png) [@Dragosha](https://forum.defold.com/u/Dragosha)\
**Post date:** [July 5, 2017, 2:29pm UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/12 "2017-07-05T14:29:48Z")

</div>

thanks for the library.

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [April 4, 2019, 9:47am UTC](https://forum.defold.com/t/window-set-listener-cant-be-nil-def-2408-solved/3995/13 "2019-04-04T09:47:12Z")

</div>

> [@britzl](#):
>
> 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.

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