Networking. How to Clientsidedly damage other players with a raycast?

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

Ive been searching all over the internet for answers, to no avail.

Each player has a “hp” variable that goes down when taking damage.

What im trying to do but cant figure out is,
how can i use a raycast to identify the clientside non-network-master player node, and subtract from their hp variable.

:bust_in_silhouette: Reply From: MagnusS

I think the main sources you’re looking for are Raycasting and High level Multiplayer. Basically you just write a normal (singleplayer) raycasting script. There are plenty of tutorials on this.
Then you read into the High level Multiplayer section about “RPC” and “Master Puppet Keywords”. The raycast/shooting method should execute only on the puppet and the set_health method should be executed synchronized on the master and all puppets.

i found "rpc_id(TARGET_PEER_ID,“method”), how do i get this TARGET_PEER_ID, from a raycast collision?

existing code:

if colliding:
	var collider = raycast.get_collider()
	if collider.has_method("_take_damage"):
		rpc(collider,"_take_damage")

should something like this work?^^^

dari0us | 2021-05-05 15:11

This is good advice, as MagnusS said start with a singleplayer when everything works implement RPC calls using the NetworkedMultiplayerENet.

Wakatta | 2021-05-05 15:27

You’ll have to research Godot’s Multiplayer framework as RPC’s are disabled by default.

To get the id you’ll have to use either get_tree().get_network_unique_id() or get_tree().get_network_connected_peers()

Wakatta | 2021-05-05 15:36

how would you go about doing get_tree().get_network_unique_id(), in a raycast collision?

dari0us | 2021-05-05 17:26

var ID = get_tree().get_network_unique_id()

But that line alone would be useless as a network needs to be setup, started, and peers connected as well as RPC keywords set to enable packet transfer

Also that function returns your id on the network so using it standalone would only be sending RPC 's to selfpeer. Have a look at the links provided above as it’s a great place to start.

Wakatta | 2021-05-05 21:25

You should save the network id of the player in a variable in the character. Then you can read that variable once your raycast hits a character.

PS: If you liked the answer, please upvote and maybe select it as accepted answer

MagnusS | 2021-05-06 13:21