Solved Dictionary two values

Discussion in 'Rust Development' started by cookiecool, Feb 3, 2018.

  1. So I'm trying to make a dictionary with two values. 1st value the player and second value the prop the player is disguised as. How would I save both values in one line so It looks like "player, prop" So I would check if the dictionary ContainsKey(player) but if it returns true how do I get the prop as a value?
     
  2. If there were multiple dictionary things how would I get TValue from Tkey? Confused a bit.
     
  3. I see what to do but still bit confused about it. So if there's multiple items like 1 "player1, prop1" 2 "player2, prop2" and you did getKey player2 with the code how would you get the 2nd argument for player2 but not player1 using
    Code:
    Dictionary<int, string> dictionary = new Dictionary<int, string>();
    Type[] arguments = dictionary.GetType().GetGenericArguments();
    Type keyType = arguments[0];
    Type valueType = arguments[1];
     
  4. I was looking in a plugins coding and this is how they did it would it be correct?
    Code:
    var prop = props[player];
     
  5. So I'm new to plugin creation and trying to create a plugin but I get this error and since I'm not very experienced I don't know what to do. Anyone got ideas on how to fix it??

    Code
    Code:
    namespace Oxide.Plugins
    {
        [Info("ControlProp", "cookiecool", 0.1)]
        [Description("Allows you to control props to diguise your self or to hide.")]    private Dictionary<IPlayer, BaseEntity> props = new Dictionary<IPlayer, BaseEntity>();    class ControlProp : RustPlugin
        {
            string perm = "controlprop.use";
            #region Loaded
            void Loaded()
            {
                permission.RegisterPermission("controlprop.use", this);
                while (props.Count >= 1)
                {
                    foreach (IPlayer player in props.Keys)
                    {
                        var basePlayer = player.Object as BasePlayer;
                        var prop = props[basePlayer] as BaseEntity;
                        prop.transform.position = basePlayer.transform.position;
                    }
                }
            }
    
    Error

    Code:
    Error while compiling: ControlProp.cs(14,56): error CS1525: Unexpected symbol `Dictionary', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
     
    Last edited by a moderator: Feb 4, 2018
  6. You need to share all the code, that's not enough to go off of. It looks like you declared your dictionary in the incorrect place.
     
  7. Code:
    namespace Oxide.Plugins
    {
        [Info("ControlProp", "cookiecool", 0.1)]
        [Description("Allows you to control props to diguise your self or to hide.")]    private Dictionary<IPlayer, BaseEntity> props = new Dictionary<IPlayer, BaseEntity>();    class ControlProp : RustPlugin
        {
            string perm = "controlprop.use";
            #region Loaded
            void Loaded()
            {
                permission.RegisterPermission("controlprop.use", this);
                while (props.Count >= 1)
                {
                    foreach (IPlayer player in props.Keys)
                    {
                        var basePlayer = player.Object as BasePlayer;
                        var prop = props[basePlayer] as BaseEntity;
                        prop.transform.position = basePlayer.transform.position;
                    }
                }
            }
     
  8. Wulf

    Wulf Community Admin

    The dictionary needs to be in the class.
     
  9. There's a few issues with your code.
    1. You shouldn't use a while loop, try a timer. Better yet, use a class inherited from MonoBehaviour and attach it to the entity.
    2. Your casts have no purpose, it's a RustPlugin, so you should be storing BasePlayers anyway, additionally, your dictionary is of IPlayers and BaseEntities, so that as BaseEntity is useless.
    3. Your perm field should be constant, so it's baked into the assembly when compiled.
    4. You should move that code to OnServerInitialized, that's when the server begins accepting connections.
     
  10. oof now I got another error
    Code:
    private void Unload()
            {
                var propList = props.Keys.ToList();
                foreach (IPlayer player in props.Keys)
                {
                    props.remove(player);
                }
            }
    
    Code:
    Error while compiling: ControlProp.cs(115,23): error CS1061: Type `System.Collections.Generic.Dictionary<Oxide.Core.Libraries.Covalence.IPlayer,BaseEntity>' does not contain a definition for `remove' and no extension method `remove' of type `System.Collections.Generic.Dictionary<Oxide.Core.Libraries.Covalence.IPlayer,BaseEntity>' could be found. Are you missing an assembly reference?
     
  11. Looks like you're calling props.remove(...);, the method name is Pascal case. Try props.Remove(...);.
     
    Last edited by a moderator: Feb 4, 2018
  12. same region now I'm getting
    Code:
    Error while compiling: ControlProp.cs(112,34): error CS1955: The member `System.Collections.Generic.Dictionary<Oxide.Core.Libraries.Covalence.IPlayer,BaseEntity>.Keys' cannot be used as method or delegate
     
  13. Post your code.
     
  14. Here it is
    Code:
    private void Unload()
            {
                var propList = props.Keys.ToList();
                foreach (IPlayer player in props.Keys)
                {
                    props.remove(player);
                }
            }
     
     
  15. Even if the above code did compile it would return an error, as you may not modify a collection while enumerating it. Now that I look at it, that's why you had .ToList(), whether it was intentional or not. Regardless, you'd be better off just using props.Clear();. :)
     
  16. Code:
    void GetIntInFront(BasePlayer player)
            {
                RaycastHit hit;
                var propi;
                BasePlayer bplayer = player as BasePlayer;
                if (Physics.Raycast(bplayer.eyes.HeadRay(), 3))
                {
                    propi = hit.GetEntity();
                    Puts(propi.ShortPrefabName);
                    return propi;
                }
            }
    
    Code:
    Error while compiling: ControlProp.cs(113,17): error CS0127: `Oxide.Plugins.ControlProp.GetIntInFront(BasePlayer)': A return keyword must not be followed by any expression when method returns void
     
    Last edited by a moderator: Feb 4, 2018
  17. class NewClass {
    public int Lol1;
    public int Lol2;
    }
    private Dictionary<int, NewClass> lols = new Dictionary<int, newClass>();

    foreach (var check in lols)
    Puts(check.Lol1 + " " + check.Lol2);