1. Quick question I hope.

    I'm trying to get the player name from OnPlayerChat - the syntax I want is the name with steamID like:

    Code:
    playername[0123456/7656123456789]
    I tried:

    Code:
    string player = (BasePlayer)arg.connection.player;
    But I get an error about converting to string, if I leave as a BasePlayer I get a recursive error in my JSON serialization (sorry, can't remember exact wording).

    Is there a quick way to get this?

    Thanks, as always, for any help :)


    Tony.
     
    Last edited by a moderator: Jan 4, 2017
  2. More detail of how I'm using this:

    Code:
    void OnPlayerChat(ConsoleSystem.Arg arg) {        BasePlayer player = (BasePlayer)arg.connection.player;
            string playerName = arg.connection.player;
            // string playerName = player.displayName;
            // var playerName = player;
            Vector3 location = player.transform.position;        string chat = arg.GetString(0, "");       chat = Uri.EscapeDataString(chat);       chatLog.Add(new {
               Timestamp = nowTimestamp,
               Player = playerName,
               Chat = chat,
               LocationX = location.x,
               LocationY = location.y,
               LocationZ = location.z
           });...
    
    Then this is used in another function:

    Code:
    public void SendLog() {    var serializedChatLog = JsonConvert.SerializeObject(chatLog);
     
  3. Wulf

    Wulf Community Admin

    You're trying to cast the entire BasePlayer object to a string. If you only want the name, it's available under player.displayName. The Steam ID is also available from player.UserIDString and player.userID (ulong). If you want that exact format from the first post, that would the BasePlayer object as a string I believe. You won't be able to store the entire BasePlayer object in JSON though without casting it to string.
     
  4. For now I have built the string up using displayName and UserIDString - it works for my purposes but feels a bit hacky.

    If you think this is a reasonable approach then I'll move onto more important things :p
     
  5. Code:
    void OnPlayerChat(ConsoleSystem.Arg arg)
            {            BasePlayer player = arg.Player();
                string playerName = player.displayName;
                Vector3 location = player.transform.position;
                string chat = arg.ArgsStr;            chatLog.Add(new
                {
                    Timestamp = nowTimestamp,
                    Player = playerName,
                    Chat = chat,
                    LocationX = location.x,
                    LocationY = location.y,
                    LocationZ = location.z
                });
            }
    Would this work?
    [DOUBLEPOST=1483570949][/DOUBLEPOST]if you need more information about the player in your chatlog list you could also add in their ulong id: player.userID