1. Hiya,

    Having a spot of bother with calling methods from another plugin - I have the following code in my 'core' plugin:

    Code:
    using Oxide.Core.Plugins;
    using Oxide.Core.Libraries;namespace Oxide.Plugins {    [Info("Core", "Rebajas", "0.0.1")]
        [Description("Oxide plugin.")]    class RustAlertCore : RustPlugin {        string privateKey;        protected override void LoadDefaultConfig() {            Config.Clear();            Config["privateKey"] = "11111111111111111111111111111111";            SaveConfig();        }        void Init() {            privateKey = (string)Config["privateKey"];        }        string GetPrivateKey() {            /* Make the private key accessible to other plugins */            Puts("In GetPrivateKey: " + privateKey);            return privateKey;        }    }}
    And this in a second plugin:

    Code:
            [PluginReference] Plugin RustAlertCore;        string privateKey;        void Init() {            /* Get the private key from core */            privateKey = (string)RustAlertCore?.Call("GetPrivateKey");            Puts("Private key: " + privateKey);        }
    But the value is not returned :( additionally I don't see the message from the core method in the console.

    Any help appreciated,


    T.
     
  2. I think you have to precede GetPrivateKey() with [HookMethod("GetPrivateKey")] to make it accessible to other plugs.
     
  3. If I do the method call like this:

    Code:
            void Init() {
               
                var RustAlertCore = plugins.Find("RustAlertCore");
                privateKey = (string)RustAlertCore?.Call("GetPrivateKey");            Puts("Private key: " + privateKey);        }
    It works, but I don't really want a bunch of references to the same plugin floating around, and when I put the reference in the class root it throws an error about using var.

    Basically I am trying to tidy up some code and the way as described in my first post seems the cleanest.

    ---

    Steenamaroo: not sure where you mean - literally on the line before I define the method? I've not seen an example using this syntax before...
     
  4. Yep.
    My knowledge is very limited here but any time I've seen/used external calls it's been

    Code:
            [HookMethod("NameHere")]
            public string NameHere()
            {
               //return some string
            }
    then

    string result = (string)SomePlugin?.CallHook("NameHere");
     
    Last edited by a moderator: Jun 22, 2018
  5. Hmm, I had tried that but it didn't seem to make any difference - I see you added in more info but I've started down a different route now :) If what I'm trying now doesn't sort this out I'll revisit using your syntax.
     
  6. Ok, cool.
    I'm sure there's more than one way to skin the cat.
    That's just the one I'm using. ;)
     
  7. My plugin looks like Frankenstien's Monster right now :p

    I am wondering if it might have been some sort of race condition where the Init was firing before the other plugin was ready. I changed the Init to OnPluginLoaded and it seems to be working now... fingers crossed.
     
  8. I think you can check if a referenced plugin is null.
    In your case - if (RustAlertCore == null)
     
  9. Oo, that might be useful later - currently looking at something else I've managed to break while getting this sorted :(
     
  10. Wulf

    Wulf Community Admin

    No you don't. That's only useful if a method is public or you are developing an extension. The methods in these plugins are all private and accessible via .Call. 99.9% of the time; HookMethod is not necessary in normal plugins.

    The ? in RustAlertCore?.Call is what that is already, though none of that needs to be done if it is null really.
     
  11. Hi Wulf, I think I'm getting there - piecing things together slowly. Alot of it seemed to be the Init() not triggering when I thought it would... anyway, getting there :)
     
  12. Wulf

    Wulf Community Admin

    RustAlertCore comes after Core when loaded, so you'd either need to change the plugin order or use a different hook such as Loaded() or OnServerInitialized(). A plugin reference will always be null in Init().
     
  13. Good to know, thanks. ^^