NetworkManager network Communication _ Problem Summary (iv)

Source: Internet
Author: User

In this article, some pits are unet of their own, while most are pits (or the unity definition is too complex).

Problem one: The Islocalplayer value is always false

The scene appears: Networklobbyplayer When overriding the Oncliententerlobby () method (public override void Oncliententerlobby ()), the idea was that when the player enters lobby, the The player is personalized, but the return result is always false. This issue may also occur in other situations.

Solution: This issue is largely unclear about the Islocalplayer,isserver and isclient definitions. Isserver: This value is true on the server side of the active player, which is true regardless of whether the client or server spawn is generated by starthost at the end of the servers, as long as it is displayed on the service side. Isclient: Generated by server-side spawn and run as client, this value is true, which is true whenever spawn is generated, both on the service side and on the client. So the player,isserver and isclient that exist on the server are true, and the client isclient are both true,isserver false. (Isserver and Isclient do not have an opposite relationship). Islocalplayer is the player that the generated player can control, whether it is the server (if there is a player on the server) or if the client has only one player with a islocalplayer value of true. Islocalpayer is assigned true when executing the Onstartlocalplayer callback, which is when the local player is generated, while the Oncliententerlobby callback is only the data plane that the client is connected to. However, the client's gameobject has not yet been established, so Islocalplayer is not assigned at this time (default false). and only Local

Player's player is created, first callback Oncliententerlobby, after the callback function Onstartlocalplayer, and other players (enemies) is the server spawn generated, Only call callback Oncliententerlobby.

Issue two: Server disconnect due to error:timeout

Scenario: When the server is running, client disconnects (the first disconnection does not occur, the second break, and later appears)

Solution: cannot be resolved, but does not affect the operation. Google a lot of this, in the Unity Forum has Unity personnel (self-proclaimed) unet itself bug, this problem is similar to TCP or UDP programming, the client actively disconnects, and the service side although received a broken message (even not received), but still accept the next message, This is an error (if someone has read a tcpserver written by the author, they will see a similar problem). Of course, for unet, this problem may be related to something more complex, claimed to have been resolved, but in the subsequent version has been in existence, the author for unity2017, there is still this problem, did not download the patch to try, Google came out of the source of this problem is as follows, If you do not want to have this problem, you can override Onserverdisconnect without calling the base function, just add networkserver.destroyplayersforconnection (conn).

 Public Virtual void Onserverdisconnect (Networkconnection conn) {    networkserver.destroyplayersforconnection (conn);     if (Conn.lasterror! = Networkerror.ok)    {        if  (logfilter.logerror)        {             debug.logerror ("  " + conn.lasterror);     }}}

Question three: Client disconnect due to error:timeout

Scenario: occurs when you call dropconnection in a lobby scene to disconnect a player from the server

Solution: cannot be resolved, but does not affect the run. Google a lot of this, this issue is not well resolved, a bit similar to the question three.

Question four: A connection have already set as ready ....

Scenario: The remote client calls Removeplayer () to exit the game and rejoin when it appears. This issue also occurs when Addplayer is called in the base of onclientscenechanged (currently select Direct violence comment out base method)

Solution:The Unity remote client and the server establish a connection when establishing the Networkconnection, and then set up the server and the client's game objects, respectively, So calling the Removeplayer method is only removeplayer in Clientscene, and there is no disconnection, so this problem occurs when the game is re-added (that is, its own connection still exists), which does not affect the operation. However, it is not certain that there will be other pitfalls, more in the last article in the remote client disconnected using the Matchmaker Dropconnection method (call this method will automatically remove the server and game side game objects), but this method will cause problems three, Can be used connectiontoserver to solve, specifically look at the question six.

Issue five: Local Connection already exists and Clientscene::addplayer:playercontrollerid of 0 already in use

scene: The game's host destroys the game when it appears

Solution: Host Build game, if you want to destroy the game requires three steps, the first step to destroy the player (remove player method), the second break connection (can be understood as destroying the game is destroy Match), The third part stophost. The first step is to remove the player from Clientscene, and if missing, it raises the Clientscene::addplayer:playercontrollerid of 0 already in use issue. If the second part is missing, issue four is raised and the game is not destroyed. If the third part is missing, how can the local Connection already exists be raised, because the game host's native player, even if the server player, is the client player, regardless of whether the game is destroyed or not, So localconnection will always exist and only stophost will disconnect this local connection.

Question six: Connectiontoclient and Connectiontoserver applications

Scenario: This problem is designed to leave the definition of the game. to Warcraft (Frozenstone exposure age) as an example, LAN set up a game, LAN other players to join the game, then the client (join the game players) can only leave the game on their own, so at this time corresponding to the problem of the scene of the four appear to join the game players themselves left the game. The player who builds the game can choose to quit the game (destroy the game) or choose to play the player who is not pleasing to the eye, the former self exits the game corresponding problem five scene. That's the problem when you kick out other games. That is, if you want to kick out the player, just disconnect the player with the server connection.

Solution : connectiontoclient is defined as a server-to-client connection, but only valid on the server side (this connectiontoclient variable is only valid on the server). So in this case, just call the disconnect method of Connectiontoclient and Dispose. Instead, Connectiontoserver is defined as a connection to the client that is still the service side, but this method is only valid in the local client, So problem four can also call Connectiontoserver's Disconnect method and Dispose (this method is test, but from the interpretation will be feasible, and should be able to avoid problems four and problem three)

Issue seven: Scene reload when drop connection and destroyconnection

Scenario and Solution: The scene is reloaded the first time dropconnection (not called Reload in Dropconnection callback, 20 is reloaded directly in the Dropconnection method). Subsequent calls are not reloaded, and the internal code is not currently visible, so you can only avoid using drop Connection, so use Connectiontoserver to try when you leave the game in question four.

DestroyConnection is every call will load the scene, in fact, it is understandable that each time you recreate the game back to the original is also acceptable, but to not reload can only view the source code.

Issue eight: Dontdestroyonload problem

Scenarios and Solutions appear: at the beginning of the previous article do not add Dontdestroyonload in the custom Lobbymanager, if you repeatedly do the loading of their own scene will continue to add Lobbymanager, That is, a new lobbymanager will be generated without loading it, and the original will not be destroyed. And NetworkManager already exist, so do not call directly, a lot of video demo add this statement, but they are all the demo is a rib run to the end, there is no reload problem, so here to be cautious, implementation like call can see HTTP// www.xuanyusong.com/archives/2938. And if you add dontdestroyonload, there may be intermediate

Lobbymanager lost situation, before doing a project, there is a plug-in also used this method, but a reload will cause the dontdestroyonload corresponding component (script) is not obtained, but the editor in view of this component (script) exists, so call cautiously.

Issue nine: Super cap join game

Scenario and Solution: When the game is set to 2 players, even if the number is full, but still can join this game, but after the game players added to the problem, so only before the game to make their own judgment, can not rely on unet own processing.

Issue 10: Start the game automatically after ready

Scenario and Solution: when all players in the current game are ready to start the game, that is, there is currently only one player, not the number of players, if the player sends the preparation (sendreadytobeginmessage), The server invokes Onlobbyserverplayersready and starts the game immediately in its base method, so its base method cannot be called here.

PS: So far, tread a lot of pits, look for time to see NetworkManager source code, unet idea is very good, if the development of good will make multiplayer online game open becomes more simple. He is different from the traditional client service side (that is, the client and the server are different classes), he rubbed together, but also set up a different role and permissions, although it is difficult to understand, but it is really convenient to apply.

NetworkManager network Communication _ Problem Summary (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.