How to get all players to recognise a raycast2d

Godot Version

4.2.2

Question

So basically I have no idea how to get a Raycast from one client to be recognized by another, I’m so confused as to what is the problem, the Multiplayer Syncronizer has the position and target position of the raycast set to on and always, and I’m also using an rpc

Here’s some relevant code:

		if Fist_Raycast.is_colliding():
			var Object_Hit = Fist_Raycast.get_collider()
			print (Object_Hit)
			Hit.rpc(Object_Hit, player)
@rpc("call_local", "any_peer")
func Hit(area, creator):
	if area is Hitbox and area.Parent != creator:
		area.Parent.Health = area.Parent.Health - 5
		area.Parent._damage_effects()
		var knockback_direction = (area.Parent.global_position - creator.global_position).normalized()
		area.Parent.apply_knockback(knockback_direction, 100.0, 0.12)

I do want to add that on the attacking players screen it is in-fact hitting the other player, so it is not a problem with collisions, it is absolutely a networking issue

I got it to woooooork! anyone with the same problem this is the new code:

		if Fist_Raycast.is_colliding():
			var Object_Hit = Fist_Raycast.get_collider()
			var Object_Hitbox_Name = Object_Hit.name
			var Object_Hit_Name = Object_Hit.Parent.name
			Hit.rpc(str(Object_Hit_Name), str(Object_Hitbox_Name) ,str(player.name))
@rpc("call_local", "any_peer")
func Hit(area, hitbox, creator):
	var origin
	if get_parent().get_parent().get_parent().has_node(creator):
			origin = get_parent().get_parent().get_parent().get_node(creator)
	if hitbox == "Hitbox":
		if get_parent().get_parent().get_parent().has_node(area):
			var target = get_parent().get_parent().get_parent().get_node(area)
			target.Health = target.Health - 5
			target._damage_effects()
			var knockback_direction = (target.global_position - origin.global_position).normalized()
			target.apply_knockback(knockback_direction, 100.0, 0.12)

there is likely a much better way of doing this

You can use Object_Hit.get_path() instead of str(Object_Hit_Name), then getting the node is much easier as any get_node(area) will find it instead of using get_parent() to find a relative starting point.

1 Like

Thank you so much! I appreciate the help, I knew there would be a better way

I appreciate you coming to my rescue yet again haha

Also the reason I use the string is because otherwise I get an error on the other client and it crashes due to network encoding or something, I’ll try it anyways though!

Yes, it does have to be a string, so everyone connected can find the same object. If anything is still erroring you can use get_node_or_null to check once instead of twice using has_node and get_node

var found_object := get_node_or_null(area)
if found_object != null:
    pass # do area stuff with found_object

Since objects are passed by reference it’s like saying “look at this memory location”, but different computers have different memory layout so you cannot pass that through the network.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.