I want to make a variable that's created when player connected and removed when dissconected for each player :S Any idea?
void OnPlayerInit(BasePlayer player)
{
int variable;
}
Temp variables for players?
Discussion in 'Rust Development' started by TheMechanical97, Jan 15, 2016.
-
Wulf Community Admin
You could try player.myVariable = whatever, then make it null when disconnecting. You could also use a C# hashset or dictionary.
-
player.MyVariable would make the variable for each player?
[DOUBLEPOST=1452888923][/DOUBLEPOST]I dont mind rmoving it when dissconnected
Just I need a variable for each player
[DOUBLEPOST=1452889074][/DOUBLEPOST]So i need to create one when first connected or even everytime player connects (Exodus plugin does something like that) -
Hey,
I whipped up this class in Visual Studio for you, it's totally untested but it should work.
If i recall, you're the chap that is learning C# for Rust Plugins? - so instead of using the variable on the player object, this way you can learn how to achieve the same result yourself with a solution that is tailored for you.
The below code allows for multiple variables to be stored against the player, not just one.
Code:private PlayerVariables _playerVariables = new PlayerVariables(); private class PlayerVariables { //Create a sub-class to store the variables in a useful manner private class Variable { public ulong playerID; public string Name; public string Value; } //Create a list object to store our data private readonly List<Variable> _variables = new List<Variable>(); //Adds a new variable to the data store, so it can be returned later. public void Add(BasePlayer player, string name, string value) { var _existCheck = GetVariableFromNameAndID(name, player.userID); if (_existCheck != null) { _existCheck.Value = value; return; } _variables.Add(new Variable() { playerID = player.userID, Name = name, Value = value }); } //Deletes a variable for a player, by it's name public void Delete(BasePlayer player, string name) { var _variable = GetVariableFromNameAndID(name, player.userID); if (_variable == null) return; _variables.Remove(_variable); } //Reads a variable for a player, by it's name public string Read(BasePlayer player, string name) { var _variable = GetVariableFromNameAndID(name, player.userID); return _variable?.Value; } //Helper method, finds the userid in the list, and the variable by it's name //Returns it, otehrwise returns null. private Variable GetVariableFromNameAndID(string name, ulong id) { return _variables.Where(x => x.playerID == id).DefaultIfEmpty(null).FirstOrDefault(x => x.Name == name); } }
- Paste the code somewhere into your class.
- Now, you can use:
-
Code:
_playerVariables.Add(player, "deaths", "10");
Code:var _deathTotal = _playerVariables.Read(player, "deaths"); player.ChatMessage($"You have died {_deathTotal} times!");
To delete the variable, use:
Code:_playerVariables.Delete(player, "deaths");
Last edited by a moderator: Jan 17, 2016 -
I have this code:
Code:private int wood; void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item) { var _player = entity as BasePlayer; foreach (var _player in BasePlayer.activePlayerList) { if (_player == null) return; switch (dispenser.gatherType) { case ResourceDispenser.GatherType.Tree: if (!permission.UserHasPermission(_player.UserIDString, "stats.register")) return; if (wood == null) wood = 0; { wood = wood + 1; SendReply(_player, wood.ToString()); (DEBUG) if (wood == 10000) { SendReply(_player, "TEST MESSAGE"); return; } return; } return; break; } } }
[DOUBLEPOST=1453133452][/DOUBLEPOST]I want the wood var to be temporal so playaer can see how many wood they gathered while online in that game, once dissconnected, the variable gets destroyed for that player and when connecter, created, I might do this with dinamic arrays but dk how :SLast edited by a moderator: Jan 18, 2016 -
[DOUBLEPOST=1453134782,1452918637][/DOUBLEPOST]"To store with value "10", I want this variable to increase, so i need to put
_playerVariables.Add(player, "deaths", "1");
everytime i want the variable to increase by 1? -
No, you will need to read it first, then add it on, and set it again.
Code:var _playerVar = Convert.toInt32(_playerVariables.Read(player, "deaths")); _playerVariables.Add(player, "deaths", _playerVar++.ToString());
-
So how can i create the veriable on player connect?
void OnPlayerInit(BasePlayer player)
}
_playerVariables.Add(player, "variable", 0);
}
?
[DOUBLEPOST=1453145243][/DOUBLEPOST]`System.Collections.Generic.List<Oxide.Plugins.Jobs.PlayerVariables.Variable>' does not contain a definition for `where' and no extension method `where' of type `System.Collections.Generic.List<Oxide.Plugins.Jobs.PlayerVariables.Variable>' could be found. Are you missing an assembly reference?
[DOUBLEPOST=1453145633][/DOUBLEPOST]Okay I added using System.Linq;
[DOUBLEPOST=1453148686][/DOUBLEPOST]seems not working
Code:void OnDispenserGather(ResourceDispenser dispenser, BaseEntity entity, Item item) { var _player = entity as BasePlayer; if (_player == null) return; switch (dispenser.gatherType) { case ResourceDispenser.GatherType.Tree: if (!permission.UserHasPermission(_player.UserIDString, "randomperm")) return; int myvar= Convert.ToInt32(_playerVariables.Read(_player, "var")); SendReply(_player, myvar.ToString()); _playerVariables.Add(_player, "var", (myvar+ 1)); SendReply(_player, myvar.ToString()); if (myvar == 10) { SendReply(_player, "TEST"); _playerVariables.Add(_player, "var", 0); return; } break; } }
Code:void OnPlayerRespawned(BasePlayer player) { _playerVariables.Add(player, "var", 0); } void OnPlayerDisconnected(BasePlayer player, string reason) { _playerVariables.Delete(player, "var"); } void OnPlayerInit(BasePlayer player) { _playerVariables.Add(player, "var", 0); }