Hi,
in my game I have local multiplayer with the new input system. When a player joins by pressing a button, I instantiate a prefab with a PlayerInput Component.
In my custom PlayerManager, I have a custom class PlayerData which contains a reference to a PlayerInput and additional settings regarding this player (selected character, stats etc.).
When a player joins, I am using OnPlayerJoined to create a new PlayerData object, which references the PlayerInput of the joined player, and add this PlayerData to a list (PlayerDataList), so I have all active players in this list.
Now I use PlayerInput.user.index to identify different players, meaning that I pass the user.index value around to other scripts that need to change settings for this player. If some script needs to access the PlayerData object I loop through the PlayerDataList and compare the user.index to find the correct player.
Now this seems to work fine. The problem I have appears when players leave the game.
On my custom PlayerManager I use OnPlayerLeft(PlayerInput player) to identify the player (using player.user.index), and I remove him from my PlayerDataList.
The Input System in Unity automatically reorders the users, so if the player with user.index 0 leaves, the user with user.index 1 is now at user.index 0, 2->1, etc.
What I want to do now is to pass the updated user.indexes to the scripts that need the values to identify the player. In OnPlayerLeft I go through my PlayerDataList, and for each PlayerInput I pass the user.index to the other scripts.
The problem is that the moment OnPlayerLeft executes the user.indexes are still the old ones, even though the Input Debugger already shows the new indexes. I can check that by logging PlayerInput.all.
I am really scratching my head why when in OnPlayerLeft I ask for the user.index of the PlayerInput (in the PlayerData object of my PlayerDataList) I get the old ones. Shouldn’t they have been updated?
Thinking about it, I could probably circumvent that by, instead of passing the user.index around, directly passing the PlayerInput around (or the PlayerData object). But my thought was that passing an Integer around is way quicker.