Runner.Spawn OnBeforeSpawned
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on Fusion.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Runner.Spawn OnBeforeSpawned
Crushl
2022-06-17 11:54:14
Hey guys,
I'm currently trying to spawn Network Objects in Host / Client Mode.
While spawning "naked" objects works quite well, I seem to fail to understand how to use the OnBeforeSpawn method in the Runner.Spawn properly.
On the Host / Server everything works. But on the clients, the initialisation method never runs and therefore, values, Hitboxes etc of the spawned object are not set up correctly.
Is there a simple way to have the server run the OnBeforeSpawn method first and then sending the final object with initialised values to the network?
Or is there another way to do this?
Best regards,
Chris
Comments
So, I have checked the Kart Game example. If I understand correctly, the Kart Game Items simply have a general timer that enables the hitbox after 0.5 sec.
I do however want to track which kart the item originated from for various reasons (tracking scores for hits, disabling homing for the Kart that shot the item etc) and fail to do so.
I need this to be initialised before the first frame of the objects existence to avoid invalid collision calls on clients. Isn't the OnBeforeSpawn method in the Runner.Spawn meant for exactly that? 🤔
Isaac_Augusto
2022-06-20 09:49:06
Hi,
Yes, the OnBeforeSpawn
is a good place for initialise custom networked variables.
Isaac Augusto
Photon Fusion Team
Isaac_Augusto 2022-06-20T09:49:06+00:00
Hi,
Yes, the
OnBeforeSpawn
is a good place for initialise custom networked variables.-----
Isaac Augusto
Photon Fusion Team
Ok, but initialisation only runs on the server, right?
On the Init method, I'm referencing the collider of the Playerkart to the Object that is being created / shot to avoid that the objects collides with the Kart that shot it.
So, I tried to add "[Networked]" to the collider reference which gave me a weaving Error in unity. But if the hitbox reference is a non-networked variable, it stays empty / null on clients.
How do I get the reference synced to the clients?
Maybe to clarify, here is what I did in code:
//Spawning the object:
Runner.Spawn(itemPrefab, castProps.castPosition, castProps.castRotation, inputAuthority: null, InitializeObjBeforeSpawn, predictionKey: null);
//BeforeSpawn Method:
private void InitializeObjBeforeSpawn(NetworkRunner runner, NetworkObject obj) {
obj.GetComponent
}
//Init Method:
public void Initialize(ItemCastProperties props) {
col = GetComponent
casterCol = props.castCollider;
Physics.IgnoreCollision(col, casterCol, true);
}
Isaac_Augusto 2022-06-20T09:49:06+00:00
Hi,
Yes, the
OnBeforeSpawn
is a good place for initialise custom networked variables.-----
Isaac Augusto
Photon Fusion Team
Ok. So is this also possible for referencing Colliders? If I put a Networked on them, I get a Fusion weaving Error.
To clarify what I'm doing, here are some code pieces:
// spawning the projectile:
Runner.Spawn(itemPrefab, castProps.castPoint, castProps.castRotation, inputAuthority: null, InitializeObjBeforeSpawn, predictionKey: null);
// Before Spawn Method:
private void InitializeObjBeforeSpawn(NetworkRunner runner, NetworkObject obj) {
obj.GetComponent<SpawnedProjectileItem>().Initialize(castProps);
}
// Init m
ethod on the to be spawned object:
public void Initialize(ItemCastProperties props) {
col = GetComponent<Collider>();
casterCol = props.castCollider;
Physics.IgnoreCollision(col, casterCol, true);
}
As stated before, this works on the server but on clients, the colliders stay null and therefor I get errors or invalid collisions on clients.
How do I get the server-side initialised colliders set up on the clients correctly?
Isaac_Augusto
2022-06-20 20:12:25
Hi,
OnBeforeSpawn
only runs on the Host/Server. So everything you initialize there should be part of the game state (e.g modifying networked properties).
You cannot add a Collider to the game state, the weaver will complain. But you can set the NetworkID
of the caster and use it on the client to get the NetworkObject
(Considering that the caster is a NO).
So it should be something like this:
OnBeforeSpawn
- only runs on the Host/Server
col = GetComponentSpawned()
- of the projectile spawned.
CasterCollider should be replaced with the NetworkID
of the caster NetworkObject
, on the Spawned()
of the projectile, you can try find the object with: Runner.TryFindObject()
- using the ID, then get the collider on the client side.
Physics.IgnoreCollision(col, casterCol, true); -> This should probably be on Spawned()
- as well.
Isaac Augusto
Photon Fusion Team
Thank you for the clarification. I'll try it that way 👍️
hello, please what solution did you use @Crushl, cause i am in the same situation now
Back to top