Hey, I’m trying to make a small plugin for device management. It maintains a separate array devices that are moved over from connected_devices based on inputs while taking_input == true. It retains correct order between scenes and when controllers connect and disconnect, allows for switching positions, dropping out if needed, but in a kind of limited fashion that will require tweaking to suit more specific needs.
It is connected to the Input.joy_connection_changed-signal and causes the scene tree to pause when an active device disconnects, and then starts again when that device rejoins or if a null slut in the array is filled by another connecting device.
However - isn’t the point with a plugin that you should just be able to plug it in and then it should work?
This thing will need to be kind of integrated into whatever game one is making. Like what will happen when someone disconnects? Someone will have a menu where you can see which players are connected and not, someone will just have a pause screen or no reaction at all. Also, you need to kind of manage some stuff like the bool “taking_input” from outside, tied to whatever you’re currently doing. When you are in the join or “player disconnected” menu you may want it to take input - but not at other times. The device manager itself does not keep track of this.
So i’m just wondering is this breaking against too many plugin conventions or is it something that would be useful? Anyone wants to try it out to see if you can find any bugs or ideas for improvements?
extends Control
var joined_devices: Array
var taking_input: bool = true
var make_null_on_disconnect = true
func _ready() -> void:
Input.joy_connection_changed.connect(handle_join_and_remove)
func _input(event: InputEvent) -> void:
if taking_input == true:
handle_input(event)
func handle_join_and_remove(device: int, connected: bool):
if device in joined_devices or null in joined_devices:
if connected == false:
print("connect is false, device: %s" % device)
print(joined_devices)
get_tree().paused = true
if connected == true:
print("connect is true, device: %s" % device)
print(joined_devices)
get_tree().paused = false
if make_null_on_disconnect == true:
if connected == false:
var index_to_null = joined_devices.find(device)
joined_devices[index_to_null] = null
print(joined_devices)
elif connected == true:
var index_to_add_device = joined_devices.find(null)
joined_devices[index_to_add_device] = device
print(joined_devices)
func handle_input(event: InputEvent):
if event is InputEventKey or event is InputEventJoypadButton:
var device_id : int
if event.is_action_pressed("ui_accept"):
if event is InputEventKey:
device_id = -99
elif event is InputEventJoypadButton:
device_id = event.device
print("Device pressed accept: ", device_id)
if not joined_devices.has(device_id):
if not joined_devices.has(null):
joined_devices.append(device_id)
print(joined_devices)
elif joined_devices.has(null):
var entry_to_replace = joined_devices.find(null)
joined_devices[entry_to_replace] = device_id
print("null replaced null with %s" % device_id)
print(joined_devices)
else:
push_error("could not append device_id or replace null")
elif event.is_action_pressed("ui_cancel"):
if event is InputEventKey or event is InputEventJoypadButton:
if event is InputEventKey:
device_id = -99
elif event is InputEventJoypadButton:
device_id = event.device
print("Device pressed cancel: ", device_id)
if joined_devices.has(device_id):
var entry_to_replace = joined_devices.find(device_id)
joined_devices[entry_to_replace] = null
print("replaced device_id with null")
print(joined_devices)
elif not joined_devices.has(device_id) and joined_devices.has(null):
var null_to_clear = joined_devices.find(null)
joined_devices.remove_at(null_to_clear)
print("Cleared null value from array")
print(joined_devices)
elif not joined_devices.has(device_id):
print("Device not in joined devices, can't make null")
else:
return
func clear_nulls():
while null in joined_devices:
joined_devices.erase(null)