Multiplayer who pressed the key

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Krippi

When a player is in an area and then a certain key e.g. “E” presses something should happen to the player. But since I don’t have the body in the input event, I don’t know how to find out which player pressed the button and then modify the player body

:bust_in_silhouette: Reply From: supper_raptor

You can do this

func get_input():
    if is_network_master():
        if Input.is_action_pressed("some_action_key"):
            #tell the server to do something to this player
            # id 1 is for server 

#function to do something (server)
remotesync func server_do_something_to_this_player():
    #your code to do something

    #then sync what server did to this player with everyone

remotesync func sync_what_server_did():
    #your code to sync changes

Oke Thanks that works. Now another question when i spawn a new Objects then with physics and one player hit it the object is not moving at both screens to the same direction. So the Physics of the objects are not sync how do i change that?

Krippi | 2020-04-05 19:30

? Can u help me with that

Krippi | 2020-04-07 08:24

You need to use Authoritarian networking model (Server controls everything).
As the game is running on different devices , There will be no physics sync. To sync physics you need to consider only server’s physics state.

For example

Your position on your device : 15,25
Your position on server : 20,25

In this case you need reset your position on your device with the one in server.
Now your position

Your position on your device : 20,25
Your position on server : 20,25

Now its synced

But there is a demerit to this method ,You will see your player getting teleported and jitters in movement and with high latency/ping it will get even worse.
To Fix jittery movements you need to implement Client-Side Prediction and Server Reconciliation.

You can learn it here : site1 and site2

Feel free to ask more, Actually i did not see my mail that’s why it took too long to respond.

supper_raptor | 2020-04-07 09:24