MultiplayerSpawner error on spawning projectiles

Godot Version

4.2 (Godotsteam)

Question

Hi , I am having an issue when trying to spawn projectiles with the MultiplayerSpawner.

This is my player class:


class_name Mp_Player
extends PlayerBase


var owner_id: int = 1
var is_host: bool = false

func _enter_tree() -> void:
	owner_id = name.to_int()
	
	if owner_id == 1:
		is_host = true
		
	set_multiplayer_authority(owner_id)


func _input(event: InputEvent) -> void:

	if not is_multiplayer_authority():
		return 
		

	if event.is_action_pressed("action1"):
		action1.rpc()


@rpc("call_local", "any_peer") // I´m trying here, I´m not sure
func action1() -> void:

Spawner.projectile_spawner.spawn([projectile_spawn.global_position, utility_socket.global_transform.x, projectile_speed, projectile_damage])

And this is the autoload spawner with the MultiplayerSpawner attached.


extends Node2D

const PROJECTILE = preload("res://scenes/projectile/projectile.tscn")

@onready var projectile_spawner: MultiplayerSpawner = %ProjectileSpawner
@onready var projectile_container: Node2D = %ProjectileContainer

func _ready() -> void:
	projectile_spawner.spawn_function = spawn_projectile


# [0] position, [1] direction, [2] speed, [3] damage
func spawn_projectile(data):
	var projectile: Projectile = PROJECTILE.instantiate()
	projectile.position = data[0]
	projectile.initialize(data[0], data[1], data[2], data[3])
	return projectile

The projectiles are instantiating well across the players, but I am having an error anyways.

This is the error message:

E 0:00:05:0836 mp_player.gd:118 @ action1(): Conditio

> n "!is_inside_tree() || !get_multiplayer()->has_multiplayer_peer() || !is_multiplayer_authority()" is true. Returning: nullptr
>   <C++ Source>   modules\multiplayer\multiplayer_spawner.cpp:322 @ MultiplayerSpawner::spawn()
>   <Stack Trace>  mp_player.gd:118 @ action1()

Can someone help me?
Thanks!

I solve the issue removing this from the multiplayerSpawner class

func _ready() -> void:
	projectile_spawner.spawn_function = spawn_projectile

And in the player class I´m calling the action1() with a rpc_id(1) like this:

func _input(event: InputEvent) -> void:

	if not is_multiplayer_authority():
		return 
		
	if event.is_action_pressed("action1"):
		action1.rpc_id(1)


@rpc("call_local", "any_peer")
func action1() -> void:

Spawner.projectile_spawner.spawn([projectile_spawn.global_position, utility_socket.global_transform.x, projectile_speed, projectile_damage])

I think you had it right the first way around, I am surprised if the spawn function does anything without spawn_function set. The issue I see is “call_local” on the RPC, you do not want players spawning a projectile themselves since the server will handle it via MultiplayerSpawner.

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