Results 1 to 6 of 6
  1. #1

    Question Multiple classes in a plugin?

    Hey there!

    I am trying to create a plugin with multiple classes like this:
    Code:
    // All the required namespaces here
    
    namespace PRoConEvents {
    	public class myPlugin : PRoConPluginAPI, IPRoConPluginInterface {
    		public void console(String message) {
    			ExecuteCommand("procon.protected.pluginconsole.write", message);
    		}
    	
    		// Rest of the code here..
    
    		example ex = new example();
    		public override void OnGlobalChat(String speaker, String message) { ex.writetoconsole("This should appear in the PRoCon console."); }
    	}
    	
    	public class example {
    		public void writetoconsole(String message)
    		{
    			myPlugin mp = new myPlugin();
    			mp.console(message);
    		}
    	}
    }
    This compiles just fine but the issue is that I'd like to have multiple classes in the plugin but nothing happens when console function is called from my example class, it works if it's called from myPlugin class but that's not what I'm trying to do.

    Am I missing something?
    Last edited by Kjell; 30-05-2018 at 17:13. Reason: Typos.

  2. #2
    Quote Originally Posted by Kjell View Post
    Hey there!

    I am trying to create a plugin with multiple classes like this:
    Code:
    // All the required namespaces here
    
    namespace PRoConEvents {
        public class myPlugin : PRoConPluginAPI, IPRoConPluginInterface {
            public void console(String message) {
                ExecuteCommand("procon.protected.pluginconsole.write", message);
            }
        
            // Rest of the code here..
    
            example ex = new example();
            public override void OnGlobalChat(String speaker, String message) { ex.writetoconsole("This should appear in the PRoCon console."); }
        }
        
        public class example {
            public void writetoconsole(String message)
            {
                myPlugin mp = new myPlugin();
                mp.console(message);
            }
        }
    }
    This compiles just fine but the issue is that I'd like to have multiple classes in the plugin but nothing happens when console function is called from my example class, it works if it's called from myPlugin class but that's not what I'm trying to do.

    Am I missing something?
    You can have SUBCLASSES in a plugin, but not multiple main classes. There is only one instance of the main plugin tied back to procon events for each server. Procon is not listening for fired events from your new instance you create with new myPlugin();. You cannot create new instances of the plugin, procon does that on its own and ties all the proper events together.

    If you want multiple classes that's fine, you can see many MANY subclasses in the AdKats plugin, one specifically for logging. I would suggest checking that out, it's around line 50000 or so. 'public class Logger'.
    ____

  3. #3
    Thanks for the information @ColColonCleaner, I tried to use a nested class but for some reason it doesn't allow me to do it:

    Code:
    // All the required namespaces here
    
    namespace PRoConEvents {
    	public class myPlugin : PRoConPluginAPI, IPRoConPluginInterface {
    		public readonly exampleClass consoleLog;
    	
    		// Rest of the code here..
    
    		public override void OnGlobalChat(String speaker, String message) { consoleLog.writetoconsole("This should appear in the PRoCon console."); }
    		
    		public class exampleClass {
    			public void writetoconsole(String message)
    			{
    				public void writetoconsole(String message) {
    					ExecuteCommand("procon.protected.pluginconsole.write", message);
    				}
    			}
    		}
    	}
    }
    Error:
    Cannot access a non-static member of outer type 'PRoCon.Core.Plugin.CPRoConMarshalByRefObject' via nested type 'PRoConEvents.myPlugin.exampleClass'
    Did I miss something fundamental for this?

  4. #4
    You have two writetoconsole methods, one inside the other. That is not valid C# syntax. It might be helpful to use an IDE to catch syntax issues before deployment. I use visual studio 2017 community, which is free and a decent IDE for C#.

    Also, that inner class doesn't have the method ExecuteCommand, that method only belongs to PRoConPluginAPI/Interface. Please take the suggestion i mentioned with AdKats and look at how the logging subclass is implemented there. The way I handled it was holding a reference to the outer plugin in my logger. You can do this with the API type but i used the AdKats type which is technically a bad coding practice because it increases coupling.
    ____

  5. #5
    Thanks @ColColonCleaner for the advice, I compared it against yours and now the plugin is able to launch but I'm still not able to log anything to the console through writetoconsole():

    Code:
    // All the required namespaces here
    
    namespace PRoConEvents {
    	public class myPlugin : PRoConPluginAPI, IPRoConPluginInterface {
    		public readonly exampleClass consoleLog;
    	
    		// Rest of the code here..
    
    		public void OnPluginEnable() { consoleLog.writetoconsole("This should appear in the PRoCon console."); }
    		
    		public class exampleClass {
    			private readonly myPlugin _plugin;
    		
    			public void writetoconsole(String message)
    			{
    				_plugin.ExecuteCommand("procon.protected.pluginconsole.write", message);
    			}
    		}
    	}
    }
    I can enable/disable the plugin and nothing gets logged to the console, why is that?

  6. #6
    When you instantiate the exampleClass you need to give it the instance of your plugin.

    Add a constructor to it like this:

    public exampleClass(myPlugin plugin) {
    _plugin = plugin;
    }

    Then when you make a new instance of exampleClass it will have your original plugin instance. Also, it's bad form to have a class begin with a lowercase letter. Lowercase is normally reserved for variables, it helps distinguish between them. For example you could have something called SpecialClass and an instance of that could be specialClass, it makes it easier to understand when reading especially when static variables get involved.
    ____

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •