Want to know exactly what is described in the title.
Solved Run a function from another PLUGIN if it doesn't have an API function?
Discussion in 'Rust Development' started by TheRotAG, Feb 10, 2015.
-
Wulf Community Admin
Yes, here's an example for Lua:
Code:local emailApi = plugins.Find("email") if not emailApi then print("Email API is not loaded! http://oxidemod.org/resources/712/"); return end emailApi.Object:EmailMessage("This is a test email", "This is a test of the Email API for Oxide!")
-
Would kiss u if I could <3 -
Use emailApi.CallHook("EmailMessage", "This is a test email", "This is a test of the Email API for Oxide!") -
Wulf Community Admin
-
-
-
New doubt my friends!!!
In LUA we can do plugins.Find, and how about C#, JS and Python?
NVM. We can call plugins.Find from any variable inside any languageLast edited by a moderator: Feb 19, 2015 -
I have "MyPlugin" and using plugins.Fing("MyPlugin") i can get it, but when i try to use "MyFunction"
Plugin myPlugin = plugins.Find("MyPlugin");
string returnData = myPlugin.MyFunction();
I have an error because the Plugin Class hasn't the function "MyFuncion". But it i try with direct MyPlugin Class and not Plugin
MyPlugin myPlugin = plugins.Find("MyPlugin");
string returnData = myPlugin.MyFunction();
I have an error because not found MyPlugin Class. :S -
Wulf Community Admin
-
Code:[Oxide] 1:28 AM [Error] NullReferenceException while calling MyFunction: (Null ReferenceException: Object reference not set to an instance of an object)
Code:[ChatCommand("ChatCommand")] void MyFunction(BasePlayer player, string cmd, string[] args) { Plugin MyPlugin = plugins.Find("MyPlugin"); string message=MyPlugin.CallHook("TestFunction", new object[] { ""TestHook" }); ConsoleSystem.Broadcast("chat.add \"SERVER\" " + message.QuoteSafe(), new object[0]); }
The Broadcast Messages appear in the Chat. I use if (MyPlugin != null) this.Puts(MyPlugin.Name)
to see the name of plugin and verify if plugin var is not null.
What is wrong?Last edited by a moderator: May 26, 2015 -
string message=MyPlugin.CallHook("TestFunction", new object[] { ""TestHook" });
-
-
Well I'm surprised you even got your code to compile then..
string message=MyPlugin.CallHook("TestFunction", new object[] { "TestHook" }); is invalid as CallHook returns a object every time.
Your better off posting both plugins or pming me them for me to take a look if you want help. -
This is the plugin that use CallHook
Code:// Reference: Oxide.Ext.Rustusing System.Collections.Generic; using System; using UnityEngine; using Oxide.Core; using Oxide.Core.Configuration; using Oxide.Core.Plugins;namespace Oxide.Plugins { [Info("MyPlugin", "Xephiro", "0.0.1")] class MyPlugin : RustPlugin { private Plugin PluginTest; void Loaded() { LoadConfig(); } void OnServerInitialized() { if (plugins.Exists("PTest")) { PluginTest = plugins.Find("PTest"); if (PluginTest != null) this.Puts(PluginTest.Name); } } [ChatCommand("testingcallhook")] void TestingCallHook(BasePlayer player, string cmd, string[] args) { string message = "Hello World!"; message = PluginTest.CallHook("Test", message).ToString(); ConsoleSystem.Broadcast("chat.add \"SERVER\" " + message.QuoteSafe(), new object[0]); } } }
This is the another plugin
Code:// Reference: Oxide.Ext.Rustusing System.Collections.Generic; using System; using UnityEngine; using Oxide.Core; using Oxide.Core.Plugins;namespace Oxide.Plugins { [Info("PTest", "Xephiro", "0.0.1")] class PTest : RustPlugin { public string Test(string text) { return text + " -- CallHook its works!"; } } }
-
Remove the public modifier on your function Test
-
Nop
Code:[Oxide] 2:21 AM [Error] NullReferenceException while calling TestingCallHook: (Null ReferenceException: Object reference not set to an instance of an object) [Oxide] 2:21 AM [Debug] at Oxide.Plugins.MyPlugin.TestingCallHook(.BasePlayer player , System.String cmd, System.String[] args) [0x00000] in <filename unknown>:0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (ob ject,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invoke Attr, System.Reflection.Binder binder, System.Object[] parameters, System.Global
-
Opps sorry was on an older build..
You now have to explicitly state that the method is a hook with recent changes to oxide.
Code:// Reference: Oxide.Ext.Rustusing System.Collections.Generic; using System; using UnityEngine; using Oxide.Core; using Oxide.Core.Plugins;namespace Oxide.Plugins { [Info("PTest", "Xephiro", "0.0.1")] class PTest : RustPlugin { [HookMethod("Test")] public string Test(string text) { return text + " -- CallHook its works!"; } } }
PluginReferenceAttribute for setting of the plugin field, When the plugin that needs referenced the field is set with that instance of said plugin.
Example:
Code:// Reference: Oxide.Ext.Rustusing System.Collections.Generic; using System; using UnityEngine; using Oxide.Core; using Oxide.Core.Configuration; using Oxide.Core.Plugins;namespace Oxide.Plugins { [Info("MyPlugin", "Xephiro", "0.0.1")] class MyPlugin : RustPlugin { [PluginReference("PTest")] private Plugin PluginTest; void Loaded() { LoadConfig(); } void OnServerInitialized() { if (PluginTest != null) this.Puts(PluginTest.Name); } [ChatCommand("testingcallhook")] void TestingCallHook(BasePlayer player, string cmd, string[] args) { string message = "Hello World!"; message = PluginTest.CallHook("Test", message).ToString(); ConsoleSystem.Broadcast("chat.add \"SERVER\" " + message.QuoteSafe(), new object[0]); } } }
-
I put "[HookMethod("Test")]" over Test method and "[PluginReference("PTest")]" Over declaration of PluginTest in MyPlugin
Any other idea ? -
HookMethod is only needed if you wanted you hook method to be public.
This example works:
Code:// Reference: Oxide.Ext.Rustusing System.Collections.Generic; using System; using UnityEngine; using Oxide.Core; using Oxide.Core.Plugins;namespace Oxide.Plugins { [Info("PTest", "Xephiro", "0.0.2")] class PTest : RustPlugin { string Test(string text) { return text + " -- CallHook its works!"; } } }
Code:// Reference: Oxide.Ext.Rustusing System.Collections.Generic; using System; using UnityEngine; using Oxide.Core; using Oxide.Core.Configuration; using Oxide.Core.Plugins;namespace Oxide.Plugins { [Info("MyPlugin", "Xephiro", "0.0.1")] class MyPlugin : RustPlugin { [PluginReference("PTest")] private Plugin PluginTest; void Loaded() { LoadConfig(); } [ChatCommand("testingcallhook")] void TestingCallHook(BasePlayer player, string cmd, string[] args) { string message = "Hello World!"; Puts("Plugin Name: {0}", PluginTest.Name); message = PluginTest.CallHook("Test", message).ToString(); ConsoleSystem.Broadcast("chat.add \"SERVER\" " + message.QuoteSafe(), new object[0]); } } }