Godot 4.2.1
I followed a tutorial on Godot 4 networking, and it works perfectly when I load the thing on my PC twice, but not on my other device. I have done the following:
- Used both my IPV4 and https://whatismyipaddress.com/ IP to join the host server
- I have tried port forwarding with each of the two devices separately
- I have redone the networking code through many other tutorials to no avail
Now, I wouldn’t imagine my port forwarding is wrong as it works just fine when I port forward for other video games such as Minecraft, and Cry of Fear. The only issue I can see is the other device that I use to test this is my phone. I use Godot’s ability to export to android and load it on there. I have not tried with another PC, so that could be causing issues. My question in the end is from the code below and the information above, is there any error I have made maybe that could have caused this not to work?
My Main Script:
extends Node2D
var peer = ENetMultiplayerPeer.new()
@export var playerScene : PackedScene
@onready var cam = $Camera2D
func _ready():
pass
func _on_host_pressed():
peer.create_server(6000)
multiplayer.multiplayer_peer = peer
multiplayer.peer_connected.connect(add_player)
add_player()
cam.enabled = false
func _on_join_pressed():
peer.create_client("", 6000)
multiplayer.multiplayer_peer = peer
cam.enabled = false
func add_player(id = 1):
var player = playerScene.instantiate()
player.name = str(id)
call_deferred("add_child", player)
func exit_game(id):
multiplayer.peer_disconnected.connect(del_player)
del_player(id)
func del_player(id):
rpc("_del_player", id)
@rpc("any_peer", "call_local") func _del_player(id):
get_node(str(id)).queue_free()
My Player Script:
extends CharacterBody2D
@onready var cam = $Camera2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _enter_tree():
var cam = $Camera2D
set_multiplayer_authority(name.to_int())
cam.enabled = is_multiplayer_authority()
func _physics_process(delta):
if is_multiplayer_authority():
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, 35)
move_and_slide()