He Guys...I have two Plugins...The Plugin Test1 should call a public function from the Plugin Test2....But the public function from the Plugin Test2 isnt in the Main! So how to write this? Thanks Marcelo234
Either construct a new class instance from a class int your plugin or create an instance and store it in the main instance and create a way to access it from another plugin/class. PHP: // plugin one<?phpnamespace pluginone;use pocketmine\plugin\PluginBase;class PluginOne extends PluginBase { public $class = null; public function onEnable() { $this->class = new MyClass(); }}// a class within plugin one<?phpnamespace pluginone;class MyClass { public function myFunction($params) { echo "My function was called!\nParams: {$params}\n"; }}// second plugin<?phpnamespace plugintwo;use pocketmine\plugin\PluginBase;use pluginone\PluginOne;class PluginTwo extends PluginBase { public function onEnable() { $pluginone = $this->getServer()->getPluginManager()->getPlugin("PluginOne"); if($pluginone instanceof PluginOne) $pluginone->class->myFunction("Params"); }}
Code: Warning: Missing argument 1 for McKaff_BowBash\Arena\Arena::__construct(), called in C:\Users\Marcelo\Desktop\mckaff_network\Genisys\plugins\McKaff_BowBash\src\McKaff_BowBash\BowBash.php on line 42 and defined in C:\Users\Marcelo\Desktop\mckaff_network\Genisys\plugins\McKaff_BowBash\src\McKaff_BowBash\Arena\Arena.php on line 79 [18:13:01] [Server thread/CRITICAL]: TypeError: "Argument 2 passed to McKaff_BowBash\Arena\Arena::__construct() must be an instance of McKaff_BowBash\BowBash, none given, called in C:\Users\Marcelo\Desktop\mckaff_network\Genisys\plugins\McKaff_BowBash\src\McKaff_BowBash\BowBash.php on line 42" (EXCEPTION) in "/McKaff_BowBash/src/McKaff_BowBash/Arena/Arena" at line 79
PHP: public function __construct($id, BowBash $plugin){ $this->id = $id; $this->plugin = $plugin; $this->data = $plugin->arenas[$id]; $this->checkWorlds(); }
Recently I've been working more on static functions. PHP: class SomeClass {private static $instance=null;public function __construct(){ if(self::$instance === null) self::$instance = $this;}public static function getInstance() : SomeClass { return self::$instance; } Now you can get the class object from anywhere in the code like this PHP: SomeClass:getInstance(); Just make sure you've constructed that class at least once.
PHP: public static function getInstance() : SomeClass//notpublic static function getInstance() : Some Class
Yes, static can't access class properties but you can use them to hold objects and pass it throughout code without having the real object which holds them.
Learn the errors, please. Argument 2 should be an instance of BowBash class, looks like you instanced the class with only one parameter. PHP: $classInstance = new Arena($id); Instead with PHP: $classInstance = new Arena($id, $bowBashInstance);
We're not here to do everything for you, you need to have some level of programming knowledge. Make sure you 'use' the plugin class if the classes aren't in the same directory.
Cases: Simply acts as a library/utility function Use static function Interacts with the plugin You need a non-static function
It's better to use the singleton design pattern. Also, if the class contains something that is unique in each class, don't do it.
A singleton structure, an utility class (a class where all methods and properties are static) and global variables + namespace functions are technically the same but have different syntax. However, a singleton object and an utility class may have faster variable loading than global variables.
Can you stop polluting this forum with crappy, incorrect answers? I hate to be the one guy that goes and says it but it's just plain annoying.
Sorry, if you think this answer is wrong, please point out what is wrong. If you can't point out the wrong part in my post, please do not accuse others for no reason. I fail to see the mistake in my post.
And if you would like me to elaborate: A singleton structure is basically a class that is only supposed to have one instance per runtime, and it has a static class property that stores the supposedly-only instance of the class. Therefore, even though all the method calls on that instance are non-static, since they only manipulate that single object, there is technically (by technically, I mean "to the developer" and "in terms of the outcome") no difference between singletons and static methods and class properties other than syntax difference. Furthermore, as long as they do not duplicate other existing variables or functions, using global variables and namespace functions has similar nature. Otherwise, please point out the technical difference between these three: PHP: class SingletonClazz{ public $property = null; public static $instance = null; public static function getInstance() : SingletonClazz{ return self::$instance ?? (self::$instance = new self); } public function method(){ return $this->property; } public function methodAgain($param){ $this->property = $param; }}$v = SingletonClazz::getInstance()->method();SingletonClazz::getInstance()->methodAgain($v); PHP: class StaticClazz{ public static $property = null; public static function method(){ return self::$property; } public static function methodAgain($v){ self::$property = $v; }}$v = StaticClazz::method();StaticClazz::methodAgain($v); PHP: $property = null;function method(){ global $property; return $property;}function methodAgain($v){ global $property; $property = $v;}$v = method();methodAgain($v); Please point out the technical difference between the three, apart from possible name collisions? And to explain my point that classes are more efficient, it is that memory related to the same class are stored under the same small index (this is natural for programming languages coded in any memory-based languages like C), like this: PHP: function method(){ global $v; return $v;}method(); The search processes done: Code: Search method method() from the big pool of namespace functions Resolve memory address of $v from the bbig pool of variables vs PHP: class StaticClazz{ public static $v; public static function method(){ return $this->v;}StaticClazz::method(); The search processes done: Code: Search class StaticClazz from a relatively smaller pool of classes Search method method() under the really small index of class methods of the class StaticClazz, where the index is already searched in the step above Search static class property from the really small index of static class properties of the class StaticClazz, where the index is already searched in the first step. Obviously, static class is faster. I'm not going to compare the singleton way and the global way, because I hereby humbly acknowledge you that it is out of the scope of my knowledge on how to correctly compare them, but my instinct tells me that objects are faster, and I fail to explain this instinct. P.S. Can you stop polluting this forum with crappy, incorrect accuses of others being wrong? I hate to be the one guy that goes and says it but it's just plain misleading. If you say that I am wrong while I am correct, other people holding the same understanding would be confused that they are wrong about what they understand while it is correct.
'Global variables'. They're called class properties. I'll be quite frank, a computer doesn't care about your 'instinct'. Wether a static function or a class based function is used is irrelevant here. They want to know how they can access functions in one plugin from another plugin. P.S. I'm pretty sure you meant accusations, not accuses. P.P.S. Pleaseeeee stop polluting this forum with crappy incorrect an irrelevant posts. Thanks!