Solved Safely destroying CUI?

Discussion in 'Rust Development' started by Mheetu, Aug 9, 2017.

  1. I'm trying to create a custom UI for a plugin and on Unload() I'm trying to cycle through all players and delete the container. However, I'm getting Disconnected with an RPC Error - I'm thinking this happens when attempting to destroy an element that doesn't exist?

    What is the safe or proper way to try and delete an element on all players? Do I need to create a dictionary or something that tracks the status of the UI for each connected player?
     
  2. foreach(var player in BasePlayer.activePlayerList)
    CuiHelper.DestroyUi(player, “ui parent name here”);
     
  3. That's essentially what I already have... isn't it? When it's just me in the server and the UI is visible, this (below) removes it correctly. If the UI doesn't exist and it tries to unload, I get disconnected with that RPC Error...

    Code:
            private void Unload() {
                for (int i = 0; i < BasePlayer.activePlayerList.Count; i++) {
                    DestroyUi(BasePlayer.activePlayerList[i], uiName);
                }
            }
     
  4. What’s your DestroyUi method?

    Never known there to be an RPC error when removing a UI
     
  5. Literally just a shorter method name at this point, will probably rename it and add the destruction of timers to it later... haven't gotten that far yet though.
    Code:
            private void DestroyUi(BasePlayer player, string name)
            {
                CuiHelper.DestroyUi(player, name);
            }
     
  6. Maybe try destroying another element that isn’t your main parent?

    Not quite sure
     
  7. Not sure if it helps any, but this is the red error message that appears in the top-left of the client when I'm disconnected.
    Code:
    NETRCV_ReadBytes returned falseNullReferenceException: Object reference not set to an instance of an object
    Facepunch.Network.Raknet.Peer.ReadUInt32 ()
    Facepunch.Network.Raknet.StreamRead.UInt32 ()
    Network.Read.MemoryStreamWithSize ()
    Network.Read.String ()
    (wrapper remoting-invoke-with-check) Network.Read:String ()
    CommunityEntity.DestroyUI (RPCMessage msg)
    CommunityEntity.OnRpcMessage (.BasePlayer player, UInt32 rpc, Network.Message msg)
    UnityEngine.Debug:LogException(Exception)
    CommunityEntity:OnRpcMessage(BasePlayer, UInt32, Message)
    BaseEntity:CL_RPCMessage(UInt32, UInt64, Message)
    Client:OnRPCMessage(Message)
    Client:OnNetworkMessage(Message)
    Facepunch.Network.Taknet.Client:HandleMessage()
    Facepunch.Network.Raknet.Client:Cycle()
    Client:Update()
    [DOUBLEPOST=1502291148][/DOUBLEPOST]Figured it out, the "Object reference not set to an instance of an object" got me thinking... I'm not explicitly defining a name, I'm using a variable.

    Code:
    uiName = elements.Add(new CuiPanel [...]
    The uiName variable was declared but never initialized until the UI was created at least once. If the plugin was unloaded before having to show the UI to anyone, it was essentially passing a null value.
     
    Last edited by a moderator: Aug 9, 2017
  8. Ahhh, yea.

    Good find :)