Device Ids and state
4 min
it's important to know that in airconsole controllers can join and leave at any time some developers have wasted a lot of hours debugging, because they did not understand the following points device ids every device in a airconsole session has a device id the screen always has device id 0 you can use the \<font color="#eb144c">airconsole screen\</font> constant instead of \<font color="#eb144c">0\</font> all controllers also get a device id \<font color="#eb144c">do not hardcode controller device ids! you can not assume that the device ids of controllers are consecutive or that they start at 1 \</font> within an airconsole session, devices keep the same device id when they disconnect and reconnect different controllers will never get the same device id in a session every device id remains reserved for the device that originally got it \<font color="#eb144c">airconsole getcontrollerdeviceids();\</font> returns all currently connected controllers that have loaded your game setactiveplayers to the rescue! (airconsole api 1 3 0 and above) devices can join and leave at any point in time during an airconsole session when you're game is loaded, maybe only controller device ids \<font color="#eb144c">\[3, 7, 12]\</font> are still present and not \<font color="#eb144c">\[1, 2, 3]\</font> this is where \<font color="#eb144c">setactiveplayers()\</font> comes into play you don't have to use this helper function, but this mechanism is very convenient if you want to know which device is the first player, the second player, the third player \<font color="#eb144c">airconsole setactiveplayers()\</font> takes all currently connected controllers and assigns them a player number the assigned player numbers always start with 0 and are consecutive you can hardcode player numbers, but not device ids once the screen has called \<font color="#eb144c">airconsole setactiveplayers()\</font> , you can get the device ids of players by calling \<font color="#eb144c">airconsole convertplayernumbertodeviceid(0)\</font> for the first player \<font color="#eb144c">airconsole convertplayernumbertodeviceid(1)\</font> for the second player \<font color="#eb144c">airconsole convertplayernumbertodeviceid(2)\</font> for the third player you can also convert device ids to player numbers by calling \<font color="#eb144c">airconsole convertdeviceidtoplayernumber(device id)\</font> you can get all device ids that are active players by calling \<font color="#eb144c">airconsole getactiveplayerdeviceids()\</font> the screen can call this function every time a game round starts in the beginning of a round in your game the screen can call \<font color="#eb144c">airconsole setactiveplayers()\</font> then you can easily send a message to the first player by calling \<font color="#eb144c">airconsole message(airconsole convertplayernumbertodeviceid(0), "hello first player!")\</font> custom device states custom device states are really powerful! \<font color="#eb144c">airconsole setcustomdevicestate(custom data)\</font> sets data for a device, readable by all other devices at any point in time \<font color="#eb144c">airconsole oncustomdevicestatechange(device id, custom data)\</font> will be called on all other devices, when data is set for a device even if a device joins at a later point and was not connected when the data was set a typical use case for custom device states is to control what "view" is displayed on the controller check out https //github com/airconsole/airconsole view manager https //github com/airconsole/airconsole view manager if you want to do this \<font color="#eb144c">airconsole setcustomdevicestateproperty(key, value)\</font> sets a single property in the custom device state, instead of overwriting the whole object \<font color="#eb144c">airconsole getcustomdevicestate(device id)\</font> gives you access to any devices custom device state at any point in time with custom device states you do not have to send messages to devices that connect after the games has already started with information about "state" of the game a custom device state example if a player chooses to be the color blue, he sets his custom device state with \<font color="#eb144c">airconsole setcustomdevicestateproperty("my color", "blue")\</font> all present devices will get notified with \<font color="#eb144c">airconsole oncustomdevicestatechange(device id, {"my color" "blue", \[ ]})\</font> if a new device joins later it will also get notified with \<font color="#eb144c">airconsole oncustomdevicestatechange(device id, {"my color" "blue", \[ ]})\</font> right after \<font color="#eb144c">onready\</font> gets called
