How can i access the `public string lastMessage` in Foo.cs from Bar.js?
Foo.cs
Bar.csCode:// Requires: Barnamespace Oxide.Plugins { [Info("Foo", "Game Limits", "1.0.6")] public class Foo : RustPlugin { public Bar bar = new Bar(); public string lastMessage = ""; void Init() { bar.Setup(this); } public void Print() { Puts(lastMessage); } } }
Code:namespace Oxide.Plugins { [Info("Bar", "Game Limits", "1.0.0")] public class Bar : RustPlugin { Foo foo; public void Setup(Foo foo) { this.foo = foo; } object OnPlayerChat(ConsoleSystem.Arg arg) { foo.lastMessage = "test"; foo.Print(); return false; } } }
Code:Failed to call hook 'OnPlayerChat' on plugin 'Bar v1.0.0' (NullReferenceException: Object reference not set to an instance of an object) at Oxide.Plugins.Bar.OnPlayerChat (.Arg arg) [0x00000] in <filename unknown>:0 at Oxide.Plugins.Bar.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00000] in <filename unknown>:0
NullReferenceException: Object reference not set to an instance of an object
Discussion in 'Rust Development' started by black flame, Nov 11, 2017.
-
Wulf Community Admin
You can't as far as I know, it'd need to a method.
-
It seems that foo is null when called from the event OnPlayerChat. I dont realy get what you mean that it needs to a method?
-
Wulf Community Admin
-
-
Wulf Community Admin
Example:
Code:string LastMessage() => this.lastMessage;
-
`Testing B wheeee` will get printed every 5 seconds. However, when typing it says `Is null`.
(I havent touched C# for like 6 years, i am more of a NodeJS guy, but still want to figure out what is happening).
Code:namespace Oxide.Plugins { [Info("Bar", "Game Limits", "1.0.2")] public class Bar : RustPlugin { Foo foo; public void Setup(Foo foo) { this.foo = foo; timer.Repeat(5f, 0, () => { if (foo == null) { Puts("Is null"); } else { foo.Print("Testing B wheeee"); } }); } object OnPlayerChat(ConsoleSystem.Arg arg) { if (foo == null) { Puts("Is null"); return false; } foo.Print("Testing A wheeee"); return false; } } }
-
Wulf Community Admin
-
See: bar.Setup(this);
Code:// Requires: Barnamespace Oxide.Plugins { [Info("Foo", "Game Limits", "1.0.0")] public class Foo : RustPlugin { public Bar bar = new Bar(); void Init() { bar.Setup(this); } public void Print(string str) { Puts("Message: " + str); } } }
-
Make it static
-
Still wondering why it happens tho :/