#Trying to networking players materials :

1 messages · Page 1 of 1 (latest)

wraith idol
#

Hi, I´m trying to network each player color based on the spheres network example. So each of my player´s controller (which already have a reference to its SyncedTransform and a PlayerState) have a reference to a Renderer which is a Skinned Mesh Renderer. In local all works fine but when other players spawn, their material don´t show in my local player.
This is the code for the material, hope that youre doing fine and you could help me 🙂

 @serializable(Renderer)
    mainBodyRenderer?:Renderer;
...

start(): void {
...
const isLocal = this.isLocalPlayer();  

if(isLocal){
            //If the instance of the object is Local then -> Request all of that instance 
            this.gameObject.getComponent(SyncedTransform)?.requestOwnership();
            this.animator?.setBool("move",true);

            try {
                let index = this.userProfileInfo!.Avatar_color as number;
                const mat = this.mainBodyRenderer!.sharedMaterial as MeshStandardMaterial;
                if(mat){
                    const coloredMat = new MeshStandardMaterial();
                    coloredMat.copy(mat);
                    coloredMat.color = this._avatarSettings!.avatarColors![index]; 
                    this.mainBodyRenderer!.sharedMaterial = coloredMat;
                }
            
            } catch (error) {
                console.log("No user info found");
            }
}

...

    isLocalPlayer(): boolean{
        const isConnected = this.context.connection.isInRoom; 
        return !this.playerState || !isConnected || this.playerState.isLocalPlayer; 
    }
halcyon mountain
#

The code that sets the material is conditioned by isLocal, you want to set it regardless of whether the player it is local.

wraith idol
#

Oh I see, let me try. Thanks

#

🙂

halcyon mountain
#

Also mind that you should hook the initialization of your player to when the owner is set to the PlayerState component.

There's no guarantee that the PlayerState got the remote owner set, so by the time your start is fired in your player component, the owner can be undefined. So to implement this properly, utilize the callback as it is shown in the sample :)

wraith idol
#

Oh Thank you : ), I will try . Right now (without implement the this.playerState.onFirstOwnerChangeEvent.addEventListener(() => this.initialize());) when my players spawn, both of them see all with their local color .
I will implement the last comment. Thanks

wraith idol
#

It doesn´t work 😦 players see all other players with its local color.

halcyon mountain
#

That will be most probably an issue with the driving value you use to set the player color.

#

Is it relative to client or absolute on all instances?
As in, if the local player effects that value in any way etc.

wraith idol
#

Is using the localStorage this.userProfileInfo = JSON.parse(localStorage.user_profile);, it should be relative to each client

halcyon mountain
#

Have you logged what you get from the local storage? Check the data you run through your code if they are correct :)

wraith idol
#

I have check it and is correct, each local player has its own data. One question, what does the this.gameObject.getComponent(PlayerState)?.onFirstOwnerChangeEvent.addEventListener(() => this.somefunction()) do ?

halcyon mountain
wraith idol
#

So if its a remote instance then the owner of PlayerState is undefined and calls the somefunction of that remote instance ?

halcyon mountain
#

No, this makes sure that the the logic is run only when the owner is there otherwise it registers to a callback on when it is received.

#

So do i understand that you are using a local stored value on your remote clients?

halcyon mountain
#

That would be the bug, right?

#

So then you need to synchronize the choice for every user.

wraith idol
#

Well I´m trying sending and receiving messages but i´m a bit stuck. One question, there is a way to network the material using @syncfield ?

#

I mean, A renderer, not a material

ivory cairn
#

I think syncField cant do that right now. But you could sync the renderer guid and search for it when the syncField changes