Is there anyway to create a private player variable (bool int and so forth)
Need to be able to adjust the variable from inside other hooks. (change int value or reverse bool value)
Hoping this is something simple as I'm fairly close to finishing my plugin.
Creating private variables for players? (C#)
Discussion in 'Rust Development' started by Rick Assley, Mar 8, 2016.
-
I don't think there is a way of creating a variable inside the BasePlayer class, at least not through a plugin.
I suggest keeping a Dictionary<ulong, variable> if you just need a single variable for each player.
If you need multiple you could use a Dictionary<ulong, CustomClass>.
The ulong value would be the SteamID. -
Thanks for the reply. That is probably what I will need to end up doing.
Could you give some example code on how to pull the information and do an if statement with it? -
Like so?
Code:Dictionary<ulong, bool> playerDictionary = new Dictionary<ulong, bool>();void OnPlayerInit(BasePlayer player) { playerDictionary[player.userID] = false; }void SomeFunction(BasePlayer player) { bool b; if (playerDictionary.TryGetValue(player.userID, out b)) { if (b) { // do something } else { // do something else } } } -
You could also do a List<CustomClass> where the custom class includes the steamID and a Find method.
-
I think a Dictionary is much faster in this case than a List<CustomClass> with a Find function. You could also do Dictionary<ulong, CustomClass>. Dictionarys were made for accessing something through a key, which the SteamID basically is.Last edited by a moderator: Mar 8, 2016
-
Could you give a code example? Sorry relatively new to this
[DOUBLEPOST=1457500377,1457446819][/DOUBLEPOST]That is exactly what I needed. I did some googling, but couldn't find exactly what I needed.
Is there a TrySetValue or something similar that I can call in a hook to change the true/false value of the bool?
Or would I just be able to use this line:
playerDictionary[player.userID] = false;
Every time I wanted to set the value?Last edited by a moderator: Mar 9, 2016 -
Read through the "Remarks" section here: Dictionary(TKey, TValue).Item Property (TKey) (System.Collections.Generic)
This should answer your question!
