Reputation: 1
(Unity) I'm using the new Input System and MultiplayerEventSystem, with 4 active players. So far each player had their own Root (container) of objects to select, but I came across a scene in which the 4 players will have the same UI Buttons available for selection, and then the problem arose that I am not aware of solve: how do I identify which player pressed the button (onclick)? I don't know what type of variable to include in the button's OnClick method to return some value that allows me to link to the player who performed the action. Need help.
Upvotes: 0
Views: 1235
Reputation: 330
Use object references to players and pass it within the OnClick event. This way you can prevent some abstraction introduced by managing indexes, and you may not need to manage a list of players separately. Another advantage is, IntelliSense can be more helpful managing such code.
Depending on your case this can be a GameObject or script reference to the player game object in scene (such as a PlayerMonobehaviour.cs attached to the GameObject), or some C# object that refer to the human players.
// Object reference can be something like:
// Player player, GameObject player, PlayerMonobehaviour player, ...
private void OnClick(Player player)
{
// Control player directly,...
player.DoSomething();
// ...or publish an event with player as the event argument.
// and let other scripts do what they want with the player.
Clicked?.Invoke(player);
}
Upvotes: 0
Reputation: 4283
When every player is instantiated should have an ID, if not, assign them one.
When the player interact with the button, pass it's ID on the onClick method.
for example as a pseudocode:
private void OnClick(int playerID)
{
//Do your button stuff, knowing playerID
if(playerID == 1)
//Do something
else
//Do something else
}
Upvotes: 0
Reputation: 301
maybe generate a id for each player that's unique to them, (i only know qb64 which has some roots in c++). and when they click said button it sends that number to you. maybe display the number next to the name? (ei XxbanananxX:655731)
Upvotes: 0