Error on using GameInputDevice (1 device not closed)

Why does

package;

import openfl.display.Sprite;
import openfl.events.Event;
import openfl.ui.GameInput;
import openfl.ui.GameInputDevice;
class Main extends Sprite {
	
	private var device:GameInputDevice;
	
	public function new () {
		super ();
		
		device = GameInput.getDeviceAt(0);
		device.enabled = true;
	}
}

result in the error

AL lib: (EE) alc_cleanup: 1 device not closed

while

package;

import openfl.display.Sprite;
import openfl.events.Event;
import openfl.ui.GameInput;
import openfl.ui.GameInputDevice;

class Main extends Sprite {
	
	private var device:GameInputDevice;
	
	public function new () {
		super ();
		
		addEventListener(Event.ENTER_FRAME, onEnterFrame);
	}
	
	function onEnterFrame(event:Event) {
		device = GameInput.getDeviceAt(0);
		device.enabled = true;
	}
}

works perfectly fine?

That’s strange,

but here the device mentioned is the sound device nothing to do with the gamepad device :wink:

I do not understand your comment about the sound device. :pensive:

Sorry :slight_smile: let’s try again.

The message AL lib: (EE) alc_cleanup: 1 device not closed is a warning showed when the application exit which warns you about an unclosed sound device. Basically to get sound out openfl use OpenAL, and you need to open a device for it to works, and if you don’t close it you get this message.

So in this warning the device mentioned got nothing to do with your device variable,
probably just a strange coincidence.

Thanks for the explanation! While googling I saw OpenAL being mentioned but couldn’t see its importance in my situation. When you say it’s just a warning I think this is quite strange:

package;

import openfl.display.Sprite;
import openfl.events.Event;
import openfl.ui.GameInput;
import openfl.ui.GameInputDevice;
class Main extends Sprite {
	
	private var thing:GameInputDevice;
	
	public function new () {
		super ();
		
		trace("1");
		thing = GameInput.getDeviceAt(0);
		trace("2");
		thing.enabled = true;
		trace("3");
	}
}

This prints

Main.hx:14: 1
Main.hx:16: 2

while

package;

import openfl.display.Sprite;
import openfl.events.Event;
import openfl.ui.GameInput;
import openfl.ui.GameInputDevice;
class Main extends Sprite {
	
	private var thing:GameInputDevice;
	
	public function new () {
		super ();
		
		trace("1");
		thing = GameInput.getDeviceAt(0);
		trace("2");
		//thing.enabled = true;
		trace("3");
	}
}

prints

Main.hx:14: 1
Main.hx:16: 2
Main.hx:18: 3

In other words the first code is being stopped from execution. That’s more than just a warning I would say.

Oh so your application crash? That would definitively explain the openal warning, since openfl couldn’t clean up.

If you trace thing is it null?
Could be either a unrecognized gamepad,
or maybe the gamepads aren’t fully available in new.

It does, I could have made that clearer from the beginning. :sweat_smile: thing indeed is null. It is the exact same gamepad that does work outside of new and if I switch codes and leave the gamepad be the results are the same. Apparently you are correct with your assumption about the gamepad not being available in new but this is something that should really be documented and I’d like to know why they aren’t available. I don’t see a good reason for it.

Maybe the connection isn’t done yet,

I wonder if the Gamepad.onConnect:Event<Gamepad -> Void> event fires for gamepads already connect before the app launches.
If it does then it’s a better solution.

Here it crashes because there’s no gamepad and you use a null object.

Otherwise you may want to wrap your code in a if (Gamepad.devices.exists(0)).

It does, I just tried it. :slight_smile: In my opinion the environment should be in place before the application starts but this isn’t a big problem in any way.

Thanks for the input!

Does it work if you listen for DEVICE_ADDED events instead?

var gameInput = new GameInput ();
gameInput.addEventListener (GameInputEvent.DEVICE_ADDED, gameInput_onDeviceAdded);
gameInput.addEventListener (GameInputEvent.DEVICE_REMOVED, gameInput_onDeviceRemoved);

Yes, I just tested it and it does work.

1 Like