GitHub | Documentation | Become a Patron

EntityMap iterator



  • Is there a better way to do this? I want to call update on all player objects in this.

    players: EntityMap<Player> = {};
    
    ..
    ..
    
    for (let client of this.clients)
    {
        this.state.players[client.sessionId].update(dt);
    }
    


  •     players: EntityMap<Player> = {};
    
        ........
        //add players something like this 
        const player = new Player(client.sessionId);
        this.players[client.sessionId] = player; 
    
        ........
    
        for (let sessionId in this.players) {
               this.players[sessionId].update();
        }


  • I know how to add players to the entity map. I am trying to find a better way to loop pver them without adding more variables. I was hoping the EntityMap had an iterator I did not know about.

    Atm I must iterate over the list of clients to get an Id I can use to get a reference to a player object so I can call the update() function on that player object. This feels like a lot of work that could be made simpler.

    Btw, I do not understanding your code. To me it looks like it would error out. The loop does not make use of "sessionId" at all and is calling update() on the players object which is of EntiyMap type.



  • @plyoung updated the code, yeah missed one thing.



  • oh, I see.. Use "in" rather than "of".
    It looks much better now. Thanks.