So im doing a MMO, wich will have a lot of entities, monsters, players etc ..
So i want opinions from you experts, wich a aproch should i go for...

create the entity when its near the view port so that the game dnt struggle updating each entity or will it cause lag creating and disposing many entities when they come to screen and go off screen?

cause i plan to add many enemys and it be useless to have it updating that entity if the player cant see it. but at the same time it doesnt feel right to create and dispose so much? idk what are your opinions?

sorry for my english. thanks in advance!<3

it just came to my mind i could create it but not update it unless is near screen?

If you don't keep them all updated, how will you know what their correct state is when they come on-screen? If you don't keep their position updated, how will you know when they come on-screen??

Keep the state separate from the GUI, so updating the state is a quick as possible. Run that in a non-GUI thread. Only worry about how they are painted etc when they are visible.

How many entities are we talking about?

If you don't keep them all updated, how will you know what their correct state is when they come on-screen?

let me edit this, and clarify more.
when a player move the server sends the x and y cordinates to evry client.

i could get the x and y cordinate from the move packet recieved from the server, if is near the screen then add that player to the array is updating and rendering entities?
else remove it

I'm confused about the client updating entities.
Yes, the client will render all the entities that are in its current viewport. But the clients do not have enough information to update all the entities.
Normally the server will hold the complete state of all the entities. Clients will get mouse clicks, keys presses etc, and send requests to the server to update the state. The server will send the latest state info to the clients, who render what they need to render.

It sounds like you may be mixing updating and rendering together in some way? (it's important to keep them completely separate)

they are separate, okay let me explain what the update do(for now)
in the client

  1. updates stateTime of animation
  2. updates the position of the listener that listen for touch of the player
  3. updates the move direction wich has to be recived by the server
    (unessary visual things which doesnt have to be updated if is not in screen)

the client have a class PlayerMP.java
wich is the same class i used for the server but in the server are just variables(for now).

the client send a move packet, the server recive it and update the position in the server, then send it back to all clients

when the client recive position of a player i assing it to the player directly
like this

for(PlayerMP p: entityManager.getPlayers()){
    if(p.getUsername().equalsIgnoreCase(username)){
        //player has moved
        if(p.getPosition() != tempPos){
            p.setPosition(tempPos);
            p.setMoveDir(moveDir);
        }
        else{
            p.setMoveDir(0);
        }
    }
}

So what am suggesting is something like this

Array<PlayerMP> updateAndRenderThisPlayers = Array<PlayerMP>();

for(PlayerMP p: entityManager.getPlayers()){
        if(p.getUsername().equalsIgnoreCase(username)){
            //player has moved
            if(p.getPosition() != tempPos){
                p.setPosition(tempPos);
                p.setMoveDir(moveDir);
                if(p.getBounds().overlaps(screen)){
                    updateAndRenderThisPlayers.add(p);
                }else{
                    updateAndRenderThisPlayers.removeValue(p,false);
                }
            }
            else{
                p.setMoveDir(0);
            }
        }
 }

and then in the entity manager render and update this new array instead of all players array

Thanks james for always trying to help me i apreciate it :)

Looking at that I wonder if you can/should split updateAndRender into two methods so you can call update for all the entities, but only call the expensive redering for the visible ones?

so you think that i should call update for evry entities?
even if they are 3,000+ entities that are updating at 60fps?

cus i cant imagin when i will do a significant call in the update, that is not a visual thing

btw right now im just rendering the entities on screen like you are saying. but the updates its call always for evry entity

... that sounds like you are doing the right thing already.

Maybe this is just an example of "premature optimisation" - trying to fix a performance problem before you know that there is one?

Unless you're in some kind of expert corporate-scale development, the best advice is to get it working with sensible simple implementations first. Then, and only then, if there's a performance issue, identify exactly what/where it is before spending time making the code more complicated.

Thanks mate, that sounds reasonable. :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.