Hi!
I would like to get the name of a player in OnPlayerInit(Baseplayer player) and pass it to {player.name} in a message in the lang file of my plugin, which is then loaded when the message is displayed with the player's name.
I am unsure of how to do this even after referencing a few plugins out there. Would some one mind suggesting how I could do this or point me in the right direction?
Cheers
Solved Getting player's name in a message? (C#)
Discussion in 'Rust Development' started by JoeSheep, May 20, 2016.
-
There are multiple way's to do it, I use the first method because I am lazy and can't be bothered always writing replace
Code:string testmessage = "{0} has joined the server"; PrintToChat(string.Format(testmessage, player.displayName)); // string.Format replaces {0}, {1}, {2} etc with the supplied arguments. In this case I am supplying 1 arg which is the players name string furthertestmessage = "{0} {1} {2}"; PrintToChat(string.Format(furthertestmessage, arg1, arg2, arg3)); string othermessage = "{playername} has joined the server"; PrintToChat(othermessage.Replace("{playername}", player.displayName)); // Using the replace function finds and replaces the 1st arg with the second. string furtherexample = "{attacker} has killed {victim}"; PrintToChat(furtherexample.Replace("{attacker}", attacker.displayName).Replace("{victim}", victim.displayName)); // The replace function can be used multiple times on 1 string
-
Ah so simple! I have seen that being used before but didn't think to use it for this. Thank's @k1lly0u
I just found the string displayName in Assembly-CSharp.dll too.Last edited by a moderator: May 20, 2016 -
Wulf Community Admin
Code:PrintToChat($"{player.displayName} has joined the server");
-
-
Wulf Community Admin
Code:string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args); ... PrintToChat(Lang("MyMessage", player.UserIDString, player.displayName));
-
I basically just did:
Code:CreatePrivateMsgGUI(welcomeMessageText.Replace("{playername}", player.displayName), bannerTintColor, textColor, player);
I now just need to figure out how to wait until the player's sleep ends after joining before I send the GUI to them, any ideas? -
Wulf Community Admin
-
Maybe I could do something with OnPlayerInit setting bool joined = true and OnPlayerSleepEnded checking if joined = true then proceeding with creating the welcome GUI message after which setting joined = false? -
Wulf Community Admin
-
[DOUBLEPOST=1463772382][/DOUBLEPOST]Hmm with the bool true until the player wakes up, it will create issues when multiple players are joining. I don't think it is possible to sort of "append" a variable to a player.
[DOUBLEPOST=1463951871,1463769810][/DOUBLEPOST]Code:if (player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot)) { timer.Once(2, () => OnPlayerInit(player)); return; }
EDIT:
This does everything I wanted to have done.
Code:void OnPlayerInit(BasePlayer player) { if(welcomeAnnouncement) { WelcomeAnnouncement(player); } } void WelcomeAnnouncement(BasePlayer player) { if (player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot)) { timer.Once(2, () => WelcomeAnnouncement(player)); return; } if (player.HasPlayerFlag(BasePlayer.PlayerFlags.Sleeping)) { timer.Once(2, () => WelcomeAnnouncement(player)); return; } destroyPrivateGUI(player); string bannerTintColor = bannerTintGrey; string textColor = textWhite; CreatePrivateMsgGUI(welcomeAnnouncementText.Replace("{playername}", player.displayName), bannerTintColor, textColor, player); }
Last edited by a moderator: May 22, 2016 -
-
Wulf Community Admin
-
-
-
Cheers.