1. 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;
    }
     
  2. Wulf

    Wulf Community Admin

    You could try player.myVariable = whatever, then make it null when disconnecting. You could also use a C# hashset or dictionary.
     
  3. 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)
     
  4. 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);
                }
            }
    
    Usage:
    1. Paste the code somewhere into your class.
    2. Now, you can use:
    3. Code:
       _playerVariables.Add(player, "deaths", "10");
      to for example store the variable named "deaths" for player, with a value of 10.
    To get the variable back simply:
    Code:
    var _deathTotal = _playerVariables.Read(player, "deaths");
    player.ChatMessage($"You have died {_deathTotal} times!");
    
    Note: You dont need the extra variable '_deathTotal' here, but as i think you said in a previous post you were new to this stuff, i would put it there as it's easier to understand - the '_playerVariables.Read(player, "deaths")' could just go inside the chat message string in place of the _deathTotal variable.

    To delete the variable, use:
    Code:
    _playerVariables.Delete(player, "deaths");
    
     
    Last edited by a moderator: Jan 17, 2016
  5. 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;
               }
         }
    }
    
    And wood variable is used by all players so i want that to register global stats (Not this code, similar one) but i need to register single stats aswell :S
    [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 :S
     
    Last edited by a moderator: Jan 18, 2016
  6. Oh wow... I really appreciate it dude. So much thanks! I'll try tomorrow and hopefully i can get my H1Z1 Plugin for Rust Finished :D
    [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?
     
  7. 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());
    
     
  8. 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;
                }
            }
    [DOUBLEPOST=1453148739][/DOUBLEPOST]
    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);
            }