Hey there,
is there a way to convert BasePlayer to ProtoBuf.PlayerNameID and back?
Converting BasePlayer to ProtoBuf.PlayerNameID?
Discussion in 'Rust Development' started by Sir BenSon, Dec 12, 2016.
-
There is no direct way, no. From what I remember, PlayerNameID only stores exactly what it says; the player's name and ID. You can create one using the info from baseplayer (their name and ID), but you can't convert these two.
-
Okay i changed a liddle bit code but it doesn't work.
This my way:
Code:int t = 0; ProtoBuf.PlayerNameID pl = new ProtoBuf.PlayerNameID(); pl.userid = players[0].userID; pl.username = players[0].displayName; List<AutoTurret> turrets = Component.FindObjectsOfType<AutoTurret>().ToList(); foreach (AutoTurret turret in turrets) { if (turret.OwnerID == player.OwnerID) { if (!turret.authorizedPlayers.Contains(pl)) { turret.authorizedPlayers.Add(pl); turret.SendNetworkUpdateImmediate(); t++; } } } players[0].SendNetworkUpdateImmediate();
-
You can also check if a player is authed by simply doing turret.IsAuthed(Player), where Player is BasePlayer.
If you need to do a check through authorizedPlayers, you can do this (using LINQ):
Code:var isAuthed = turret.authorizedPlayers.FirstOrDefault(p => p.userid == player.userID) != null;
-
Mhh thanks but it doesnt work. I get 0 players.
Code:ProtoBuf.PlayerNameID pl = new ProtoBuf.PlayerNameID(); pl.userid = players[0].userID; pl.username = players[0].displayName; List<AutoTurret> turrets = Component.FindObjectsOfType<AutoTurret>().ToList(); foreach (AutoTurret turret in turrets) { if (turret.OwnerID == player.OwnerID) { var isAuthed = turret.authorizedPlayers.FirstOrDefault(p => p.userid == players[0].userID) != null; if (isAuthed) { turret.authorizedPlayers.Remove(pl); turret.SendNetworkUpdateImmediate(); t++; } } }
-
Ahh now i know wthats wrong.
List<AutoTurret> turrets = Component.FindObjectsOfType<AutoTurret>().ToList();
There i get all autoturrets. BUT if i check:
if (turret.OwnerID == player.OwnerID)
i get false back. So the OwnerID in the turret is null. But why? -
-
Code:
if ((turret as BaseEntity).OwnerID == player.OwnerID)