I am developing a 2D top-down local multiplayer game and I want to use a single script for both players. However, I encounter some difficulties with the following InputActions:
var direction := Input.get_vector("move_left","move_right","move_up","move_down")
if Input.is_action_just_pressed("roll"):

How can I ensure that each controller operates one player / what is the optimal method to achieve this?

  • Hi,

    By "local multiplayer" you mean 2 different controllers connected to the same PC. It is not connected with any networking?

    In case of 2 controllers connected to the same PC I guess you should create different input actions for each available player (for example something like: p1_move_left, p1_move_right, p1_move_up, p1_move_down, p1_roll, p2_move_left, p2_move_right, p2_move_up, p2_move_down, p2_roll...). I think you need that separate actions anyway, because otherwise how you would handle one player using keyboard and another pad or something else? Even with both players using same keyboard you need to specify different keys for them to use.

    Then you can just have some variable in your script telling which player instance it is and use actions for corresponding player, like:

    #Telling which player instance it is
    #You can set if from editor or from script before adding instanced scene to scene tree
    @export var player_instance : int = 1
    (...)
    #Assume you need to use roll action here
    if Input.is_action_just_pressed("p%d_roll" % player_instance):
        #Do something...

    In case of networking multiplayer no need to have separate actions, you could just probably check network authority to know if current instance is controlled by client or server.

If you are handling the inputs in _input, then you should be able to differentiate them based on device id of the event. Otherwise you might need to create separate actions for each player ("move_left_p1", "move_left_p2") and select them in script based on player id or something

if player_id == 0:
    direction = Input.get_vector("move_left_p1","..._p1",...)
elif player_id == 1:
    direction = Input.get_vector("move_left_p2","..._p2",...)

Hi,

By "local multiplayer" you mean 2 different controllers connected to the same PC. It is not connected with any networking?

In case of 2 controllers connected to the same PC I guess you should create different input actions for each available player (for example something like: p1_move_left, p1_move_right, p1_move_up, p1_move_down, p1_roll, p2_move_left, p2_move_right, p2_move_up, p2_move_down, p2_roll...). I think you need that separate actions anyway, because otherwise how you would handle one player using keyboard and another pad or something else? Even with both players using same keyboard you need to specify different keys for them to use.

Then you can just have some variable in your script telling which player instance it is and use actions for corresponding player, like:

#Telling which player instance it is
#You can set if from editor or from script before adding instanced scene to scene tree
@export var player_instance : int = 1
(...)
#Assume you need to use roll action here
if Input.is_action_just_pressed("p%d_roll" % player_instance):
    #Do something...

In case of networking multiplayer no need to have separate actions, you could just probably check network authority to know if current instance is controlled by client or server.