Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Can other plugins access functions?

Comments in 'Plugin Development' started by nfell2009, Jul 5, 2015.

  1. nfell2009
    Offline

    nfell2009 New Member

    Joined:
    Jul 5, 2015
    Posts:
    10
    Minecraft User:
    nfell2009
    Heya,

    I'm new to the whole plugin scene for PocketMine but hope to make some great things. I was wondering, if I make a function in my plugin, can other plugins access it? I assume so as the function is public, so I'd use something like require or include? Maybe use?

    Thanks
  2. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    use maybe?
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Code in your plugin is treated as the same as code in another plugin. They are run on the same runtime, so no need to include or require. Other people can get the instance of your plugin via PluginManager::getPlugin($name) and call nonstatic methods. Static methods can be accessed directly since they are on the same runtime, but PHP has some problems with static properties and we discourage using them.
    Last edited: Jul 5, 2015
    iksaku and nfell2009 like this.
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    He/She is asking about API in plugin.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Ok thank you so much, Mr. Obvious.
    iksaku and nfell2009 like this.
  6. nfell2009
    Offline

    nfell2009 New Member

    Joined:
    Jul 5, 2015
    Posts:
    10
    Minecraft User:
    nfell2009
    Thanks for the help! :D That has cleared it up.

    Also, are arrays global and last until restart? Or are they during the event?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Depends on how you save them. Local variables are local to functions (not even visible to the functions you call). Object properties last until the object is destructed (for main class, when the plugin is unloaded, which might be /reload)
    iksaku likes this.
  8. nfell2009
    Offline

    nfell2009 New Member

    Joined:
    Jul 5, 2015
    Posts:
    10
    Minecraft User:
    nfell2009
    That's great. I never normally go into this much when doing PHP and only make plugins for PC Minecraft with Java. I wanted something like a hashmap so I could store things without having to start creating files etc. Thanks for your help :)
  9. InusualZ
    Offline

    InusualZ Active Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    218
    Plugins:
    1
    Minecraft User:
    Inusu4lZ
    Basically in PHP a variable can be whatever you want.
    PHP:
    $name "Alfred";
    //Right now the $name variable is a string
    echo $name;

    $name 18;
    //Now the $name variable is a int
    echo " is a" $name ."years old boy";

    $name = [];
    $name["alfred"] = "He is a 18 years old boy";
    //Right now the variable $name is the equivalent of HashMap in Java
    echo $name["alfred"];

    $name[0] = "Hello World\n";
    //Now the variable $name is normal array
    echo $name[0];

    Last edited: Jul 5, 2015
    iksaku likes this.
  10. nfell2009
    Offline

    nfell2009 New Member

    Joined:
    Jul 5, 2015
    Posts:
    10
    Minecraft User:
    nfell2009
    I know about variables and how they work. I was on about if $array = array(); can be used in multiple functions rather than just the current one it's in.
  11. nfell2009
    Offline

    nfell2009 New Member

    Joined:
    Jul 5, 2015
    Posts:
    10
    Minecraft User:
    nfell2009
    However... you can actually help me :p. Can I use a variable that can be used across the entire script that isn't in an array? It also includes across script files.
  12. InusualZ
    Offline

    InusualZ Active Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    218
    Plugins:
    1
    Minecraft User:
    Inusu4lZ
    You have two options:
    1. Make the variable static. Define it in your plugin main class and then call it.
    2. Define the variable inside your main class plugin and then create a getter for that variable. Then use that getter to use that variable outside the plugin main class.
    nfell2009 likes this.
  13. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    If you wanted a HashMap you just do something like this:
    PHP:
    <?php
    $map 
    = [
         
    "something" => 41,
         
    23 => 4
    ];

    echo 
    $map["something"] . "\n";
    echo 
    $map[23] . "\n";
    $map[23] = 60;
    echo 
    $map[23] . "\n";
    Outputs:
    Except you can only use strings and numerical types for keys.
    Last edited: Jul 6, 2015
    nfell2009 likes this.
  14. nfell2009
    Offline

    nfell2009 New Member

    Joined:
    Jul 5, 2015
    Posts:
    10
    Minecraft User:
    nfell2009
    Thanks :)

    Ahhh, thank you :D

Share This Page

Advertisement