Godot Version
4.3
Question
I am working on a multiplayer fps, and I keep getting these errors, but nothing is wrong in game. I don’t know what it is or how to fix it, but I have a feeling that if I don’t, it will screw me over later.
Thats the error, and here is my code.
For my player script:
extends CharacterBody3D
# Movement variables
var speed
const WALK_SPEED = 3.0
const SPRINT_SPEED = 5.0
const JUMP_VELOCITY = 4.8
const SENSITIVITY = 0.001
# Bobbing variables
const BOB_FREQ = 2.4
const BOB_AMP = 0.08
var t_bob = 0.0
# Field of View (FOV) variables
const BASE_FOV = 75.0
const FOV_CHANGE = 1.5
# Gravity constant
var gravity = 9.8
# Bullet variables
var bullet = load("res://Scenes/bullet.tscn")
var instance
# Onready variables for nodes
@onready var head = $Head
@onready var camera = $Head/Camera3D
@onready var gun_anim = $Head/shotgun/Shotgun_3/AnimationPlayer
@onready var gun_barrel = $Head/shotgun/Shotgun_3/RayCast3D
@onready var shotgun = $Head/shotgun
func _enter_tree():
set_multiplayer_authority(name.to_int())
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
camera.current = is_multiplayer_authority()
func _unhandled_input(event):
# Mouse motion handling for camera rotation
if is_multiplayer_authority():
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * SENSITIVITY)
camera.rotate_x(-event.relative.y * SENSITIVITY)
shotgun.rotation.z = camera.rotation.x
# Clamp camera pitch to prevent excessive rotation
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-40), deg_to_rad(60))
func _process(delta):
pass
func _physics_process(delta):
if is_multiplayer_authority():
# Apply gravity if the character is in the air
if not is_on_floor():
velocity.y -= gravity * delta
# Handle quit input
if Input.is_action_pressed("quit"):
$"../".exit_game(name.to_int())
get_tree().quit()
# Handle Jump input
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Handle Sprint input
if Input.is_action_pressed("sprint"):
speed = SPRINT_SPEED
else:
speed = WALK_SPEED
# Get input direction and move character
var input_dir = Input.get_vector("left", "right", "up", "down")
var direction = (head.transform.basis * transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if is_on_floor():
if direction:
velocity.x = direction.x * speed
velocity.z = direction.z * speed
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 7.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 7.0)
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
# Apply head bobbing based on speed
t_bob += delta * velocity.length() * float(is_on_floor())
camera.transform.origin = _headbob(t_bob)
# Adjust FOV based on speed (e.g., sprinting)
var velocity_clamped = clamp(velocity.length(), 0.5, SPRINT_SPEED * 2)
var target_fov = BASE_FOV + FOV_CHANGE * velocity_clamped
camera.fov = lerp(camera.fov, target_fov, delta * 8.0)
# Shooting behavior
if Input.is_action_pressed("shoot"):
if !gun_anim.is_playing():
gun_anim.play("shoot")
multiplayer_send_bullet() # Send bullet data across multiplayer
# Move the character based on the processed physics
move_and_slide()
func multiplayer_send_bullet():
rpc("_spawn_bullet", gun_barrel.global_position, gun_barrel.global_rotation)
@rpc("any_peer", "call_local")
func _spawn_bullet(global_pos, global_rot):
var new_bullet = bullet.instantiate()
add_child(new_bullet) # Add the bullet to the tree
# Safely set the bullet's position and rotation
new_bullet.call_deferred("set_global_position", global_pos)
new_bullet.call_deferred("set_global_rotation", global_rot)
# Head bobbing effect based on the player's movement speed
func _headbob(time) -> Vector3:
var pos = Vector3.ZERO
pos.y = sin(time * BOB_FREQ) * BOB_AMP
pos.x = cos(time * BOB_FREQ / 2) * BOB_AMP
return pos
and for my main script
extends Node3D
var peer = ENetMultiplayerPeer.new()
@export var player_scene : PackedScene
@onready var username_input = $CanvasLayer/Panel/TextEdit
# Connect to the signal when a new peer is connected
func _on_host_pressed():
if username_input.text != "":
peer.create_server(9012)
multiplayer.multiplayer_peer = peer
multiplayer.peer_connected.connect(add_player) # Use the peer id for each new player
add_player() # Add the first player
$CanvasLayer.hide()
func _on_join_pressed():
if username_input.text != "":
peer.create_client("127.0.0.1", 9012)
multiplayer.multiplayer_peer = peer
$CanvasLayer.hide()
func add_player(id = 1):
# id here is the peer id from the connection (not the manually passed one)
var player = player_scene.instantiate()
player.name = str(id)
add_child(player) # Add the player to the scene
# Increment the counter to ensure each player has a unique username
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()