Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Karken · May 07, 2012 at 02:16 AM · networkrpcnetworkviewnetwork.instantiate

How do you remove RPC's from the RPC buffer that where created on behalf of a player that is now disconnected with an authoritative server?

I struggled with this for days and now have a solution. Here is the setup...You have a client who wants to create an object in your game world but you want your server to be authoritative. So the client sends an RPC to the server asking the server to create an object on his/her behalf. Another client connects, your server sends the new client all of the info about the objects that have been added to the world by other players.

Now, a client disconnects. You want your server to remove all of the disconnected players objects from everyone who is currently connected's game world and prevent any future players from creating objects that should no longer exists in the game world. How do you do this...I will post my solution and see what y'all think.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Karken · May 07, 2012 at 03:51 AM

Here is what I did. I had the client send the server an RPC requesting the server to make the object. The big problem is how do you get the server to remove the RPC that he sends to the clients from the buffer after the player that made the request disconnects? Well you can use the RemoveRPCs() function that accepts a viewID...the problem is you have to give it the viewID that the object was created with so if you try to assigne it a viewID from the players pool and call RemoveRPCs() with the players viewID it wont work. The server is the one that created the object with Network.Instantiate() and its original viewID will be one from the servers pool of viewID's . Quick FYI: Network.Instantiate's are automatically put on the RPC buffer and Network.Destroy()'s are not buffered...so that is not fun :( You have to come up with a system of keeping track of what player request the server to Network.Instantiate what item.

I used a list (see the networkObjects list in the code) to store

 -What player requested the item be made
 -What the viewID the server originally assigned to the object

With those two things you can get the job done. I dont have time to do a full write up/tutorial but i will post my code to show you how I got it done...good luck :)

The main script NetworkingScript: (The thingToInstanciate that i have as "test"...it does nothing so dont let that trip you up)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NetworkScript : MonoBehaviour {
     
     public GameObject PlayerPrefab;
     public Transform cubePrefab;
 
     List<networkObjectsClass> networkObjects = new List<networkObjectsClass>();
     
     void OnServerInitialized(){
         Debug.Log("The Server has been INITALIZED!");
         Network.Instantiate(PlayerPrefab, GameObject.Find("PlayerSpawn").transform.position, Quaternion.identity, 0);
     }
     
     void OnConnectedToServer(){
         if(Network.isClient){
             Network.Instantiate(PlayerPrefab, GameObject.Find("PlayerSpawn").transform.position, Quaternion.identity, 0);
         }
     }
     
     void OnPlayerDisconnected(NetworkPlayer player) {
         Debug.Log("Clean up after player " + player);
         //This will find the viewID's associated with the disconnected player
         for(int i = 0; i < networkObjects.Count; i++) {
             if(networkObjects[i].Player == player.ToString()){
                 Debug.Log ("Removing "+networkObjects[i].viewID);
                 Network.RemoveRPCs(networkObjects[i].viewID);
                 Network.Destroy(networkObjects[i].viewID);
             }
         }
         Debug.Log("Size of the list "+networkObjects.Count);
         //Keeps the networkObjects list from having old info in it
         networkObjects.RemoveAll(tempList => tempList.Player == player.ToString());
         Debug.Log("New size "+networkObjects.Count);
         //Dont think this is removing any RPC's because no
         //player RPC should be buffered but just in case
         Network.RemoveRPCs(player);
         //Delets the player's Character
         Network.DestroyPlayerObjects(player);
     }
     
     void OnGUI(){
         if(!Network.isServer && !Network.isClient){
             if(GUI.Button(new Rect(10,10,Screen.width/8,Screen.height/16), "Start Server")){
                 Network.InitializeServer(32, 1337, false);
             }
             else if(GUI.Button(new Rect(10, 35, Screen.width/8, Screen.height/16), "Connect")){
                 Network.Connect("127.0.0.1", 1337);
             }
         }
         if(Network.isClient){
             if (GUILayout.Button("SpawnBox")) {
                    networkView.RPC("SpawnBox", RPCMode.Server, Network.player, "test", GameObject.FindGameObjectWithTag("Player").transform.position);
                }    
         }
         //Server directly calls the InstanciateToNetwork function
         else if(Network.isServer){
             if (GUILayout.Button("SvrSpawnBox")) {
                 InstanciateToNetwork(Network.player, "test", GameObject.FindGameObjectWithTag("Player").transform.position);
             }    
         }
     }
     void InstanciateToNetwork(NetworkPlayer player, string thingToInstanciate, Vector3 location){
         Transform clone;
         clone = Network.Instantiate(cubePrefab, location, Quaternion.identity, 0) as Transform;
         //Makes sure that the player is not the server
         if(player.ToString() != "0"){
             
             NetworkView nView;
             nView = clone.GetComponent<NetworkView>();
             
             //Keep track of the original viewID so that it can be removed from the RPC buffer
             networkObjects.Add(new networkObjectsClass(player.ToString(), nView.viewID));
             
             //This is just for debuging
             for(int i = 0; i < networkObjects.Count; i++){
                 Debug.Log("Player: "+networkObjects[i].Player+" Original ViewID "+networkObjects[i].viewID);
             }
         }
     }
     [RPC]
     void SpawnBox(NetworkPlayer player, string thingToInstanciate, Vector3 location) {
         if(Network.isServer){
             InstanciateToNetwork(player, thingToInstanciate, location);    
         }
     }
 }



The supporting script networkingObjectsClass:

 using UnityEngine;
 using System.Collections;
 
 public class networkObjectsClass : MonoBehaviour{
     private string _player;
     private NetworkViewID _viewID;
 
     public string Player {  get{ return _player;} set{ _player = value;}}
     public NetworkViewID viewID {  get{ return _viewID;} set{ _viewID= value;}}
 
     public networkObjectsClass(string player, NetworkViewID viewID){
         _player = player;
         _viewID = viewID;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Network.Instantiate dont instantiate object 0 Answers

Can not make RPC calls during Start() 1 Answer

Does the position change need state synchronization? 2 Answers

Help with Network Script. Game with multiple Maps. 2 Answers

RPC Player Name and show it above head 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges