If a game has started for example with 2 players and let’s say they do some damage to eachother. If a new player joins the game, how is that new player supposed to be updated with the current players’ health? Of course, I don’t want to use a buffered rpc call for everytime someone does damage.
I’ve been trying to do it on the “OnNetworkInstantiate()” function: something like:
if (networkView.isMine){
if (!Network.isServer){
// ... setting up which team I'm on ...
}
}else{
//otherwise, if im the remote player, send my current health state to the new player
networkView.RPC("sendMyHealth", RPCMode.Others, currentHealth);
}
So, if I’m the instatiated player, it does nothing, otherwise, if I’m a remote player, (e.g. the 2 players that were in the game earlier), send my current health value. But the problem I get is, within my “sendMyHealth()” function, I have this line (which is supposed to show their health above their character):
Which throws an error on the player that just joined because the remote player’s may have not yet loaded on their screen yet. I get “Object reference not set” on that line.
Would like to know how this is usually done or if I’m on the right track.
What if the game has 10 people, all doing damage to eachother. If I buffer each hit to every player, so that new players know their current health, the overhead for that would be huge would it not?
I’ve been reading quite a bit on buffered RPC call’s also, looks like other people steer away from buffered RPC’s because of problems in the long run.
You had also replied to that, but I’m basically in the same situation. I can’t buffer every single bullet for example that a player shoots etc. What I want to achieve is, when a player joins, have all current players already in the game just update their current health/if they’re shooting/any missiles they have shot to that new player.
For example -
A new player joins (player 3 for example), have that player send a request to the server saying “update me with the game state”.
The server replies with a bunch of info, such as:
set player 1’s health to 50/100
set player 2’s health to 80/100
create a missile going in this direction
instantiate a “structure” in this location with this much health
The thing is I don’t know where to start to go about doing this. That’s my question.
Unless I’m wrong in thinking how buffered RPC’s work. I don’t need to update the new player with a bullet that was shot 30minutes ago and is not in the game anymore.
You are mixing multiple things and it’s realy complicating your design.
You don’t need to buffer bullets, you only need to buffer things you want joining players to see.
The players health is something that the server can send to the joining clients.
That is the beauty of auth servers, they already have all the info needed for joining clients.
You can also remove rpc’s from the buffer.
For example when a player dies, you can remove his rpc’s from the buffer.
Hm, ok, so just to clarify. Everytime a player takes damage (e.g. by a bullet), I’m currently doing this each hit (keeping in mind, each bullet gets sent every 0.1 seconds, I’m only calling the updateHealth() function if the player gets hit):
And what about in the case of shooting a missile, if a player shoots a missile, and a new player joins, is that missile RPC instantiation call supposed to be a buffered one also so that the new player will see it mid-flight?
EDIT: just read your above post. I think I need to read up on remove RPC’s from buffer in this case (when a player dies), unless you could provide an example of how to do it.
No, that’s just what I said not to do.
The server can update a joining player during the instantiation.
You can make the missile a buffered one and once the missile explodes, it will be removed from the buffer since it won’t excist anymore.
Ah, I misread your post. But what I’m not catching on is this part:
This is what I’m not following, how do I get the server to update the joining player with other player’s health? I apologize if I’ve misunderstood you somewhere.
EDIT: also ran into a problem for the missile… unfortunately… if I buffer the instantiation of the missile, sure that’s simple, but when a new player joins, that missile has moved by then, but on the new player’s screen, the missile is instantiated at the position that the buffered RPC call has been set to. This is hard to grasp on how to make this work properly to be honest.
I know this is a really old thread, but if you haven’t already, you should look into StateSynchronization for variables such as health and position… anything that is constantly changing.
Buffered RPCs are good for things that don’t change all that often, for instance, applying a custom material to a newly instantiated network object by sending the name of the material from a string and loading that resource. Also state changes in the object, like when the player/NPC dies.