GitHub | Documentation | Become a Patron

Not getting updated state in Unity client (SOLVED)



  • I'm only getting the first state and it's not being updated beyond that.
    Is there something obvious that I'm not seeing that is causing the issue?

    Here is my room code:

    import BaseRoom from "./BaseRoom";
    import {Client} from "colyseus";
    
    export class State {
        ticks: number;
        constructor() {
            this.ticks = 0;
        }
    }
    
    export default class ChatRoom extends BaseRoom<State> {
        onDispose(): void | Promise<any> {
            return undefined;
        }
    
        onInit(options: any): void {
            console.log('ChatRoom created');
            this.setState(new State());
            setInterval(() => {
                this.setState({ticks: this.state.ticks + 1});
                console.log('Updated state', this.state);
            }, 1000);
        }
    
        onJoin(client: Client, options?: any, auth?: any): void | Promise<any> {
            return undefined;
        }
    
        onLeave(client: Client): void | Promise<any> {
            return undefined;
        }
    
        onMessage(client: Client, data: any): void {
            console.log('ChatRoom received message', data);
        }
    }
    

    And on the Unity Side:

        Client client = new Colyseus.Client("ws://localhost:6900");
        client.OnError += Client_OnError;
        yield return StartCoroutine(client.Connect());
        Room room = client.Join("chat", new Dictionary<string, object>() {
            {"accessToken",accessToken},
            {"username", username}
        });
        room.OnReadyToConnect += (sender, e) => {
            Debug.Log("Ready to join room");
            StartCoroutine(room.Connect());
        };
    
        room.OnError += Room_OnError;
    
        room.OnStateChange += OnStateChange;
        room.Listen("message", this.OnMessage);
        room.Listen("ticks", this.OnTick);
        room.OnJoin += Room_OnJoin;
        room.OnMessage += Room_OnMessage;
    
        int i = 0;
    
        while (true)
        {
            client.Recv();
    
            i++;
    
            if (i % 50 == 0)
            {
                room.Send("some_command");
            }
    
            yield return 0;
        }

  • administrators

    Hey @imallic,

    The the setState() method actually will overwrite the state - it doesn't work like React does. You'd usually mutate the state instead:

    this.state.ticks++;
    

    Hope this helps!



  • That solved the issue. You are correct, I was taking a React approach to it.
    Thanks for saving me again @endel!



  • This may be off topic, but does colyseus support UDP? with state changes?


  • administrators

    @imallic You're welcome <3

    Maybe in the future! I'm currently working on TCP support to create a Game Maker client (they don't support WebSocket neither UDP)

    It would need to be some sort of Reliable UDP, though. I'm still not sure how to go about it, since the order of messages are important.

    Cheers!



  • But things like updating player movement wouldn't need to be that reliable. I understand that things like interpolation could be used but it's still going to make something like an FPS too glitchy.


Log in to reply
 

Looks like your connection to Colyseus Forum was lost, please wait while we try to reconnect.