1. How can i access the `public string lastMessage` in Foo.cs from Bar.js?

    Foo.cs

    Code:
    // 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);
            }
        }
    }
    
    Bar.cs
    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
    
     
  2. Wulf

    Wulf Community Admin

    You can't as far as I know, it'd need to a method.
     
  3. 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?
     
  4. Wulf

    Wulf Community Admin

    You're trying to grab variable, not a method. A variable isn't a method.
     
  5. Do you mean this variable `foo.lastMessage = "test";` lastMessage or foo ?
     
  6. Wulf

    Wulf Community Admin

    lastMessage is the variable. bar can't get foo's variables unless they are wrapped in a method that returns each.

    Example:
    Code:
    string LastMessage() => this.lastMessage;
     
  7. I mean, even when i call just the mehods, this.foo is null when accessed from an event.

    `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;
            }
        }
    }
    
     
  8. Wulf

    Wulf Community Admin

    Considering that the code you showed me there has nothing set for foo, then I imagine it would be null. :p
     
  9. Well, i have set it to `this`. That is why i am really confused about what is going on :)

    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);
            }
        }
    }
    
     
  10. Make it static
     
  11. Yep, that is what i did :)

    Still wondering why it happens tho :/