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

Objects on async threads

Comments in 'Plugin Development' started by Brutus, Sep 16, 2015.

  1. Brutus
    Offline

    Brutus New Member

    Joined:
    Aug 31, 2015
    Posts:
    21
    I am having an issue using objects outside the main thread. As an example, I have this simple message class: http://hastebin.com/okuwanayak.php

    However, when I try to instantiate this on a thread other than the main thread, it throws a class not found exception. I am not trying to store it as a field or anything like that. I am serializing it to a string, successfully passing that string from the main thread to another thread, and then attempting to create a new Message object on the other thread.

    Both unserialize() as well as manually serializing and then using new Message(...)do not work.

    Message objects are used also in the main thread, but as you can see there are no static variables or other class-wide data.

    Is there something I am doing wrong that will allow the class loader to work as intended across threads? I tried adding extends \Threaded to Message, but that did not have any effect.
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    You need to register the autoloader on the other thread. I am in the middle of work, so I can't give you a snippet, maybe someone else will be able to.
    HotFireyDeath and Brutus like this.
  3. Brutus
    Offline

    Brutus New Member

    Joined:
    Aug 31, 2015
    Posts:
    21
    Thank you. For my interest, I would love if someone could explain why that is. I am somewhat new to PHP.

    Is it that PocketMine does a best-effort attempt to load all source in plugin folders automatically so we can avoid manually defining loading schemes, but this does not work on threads that I create?
  4. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    http://php.net/manual/en/language.oop5.autoload.php has some information. Basically, the way PHP knows how to load new classes depends on the thread you are in.

    I solved this issue in my BuyCraft plugin using https://github.com/Falkirks/BuyCraft/blob/master/src/buycraft/api/ApiAsyncTask.php#L41 (bad practice), but since then PocketMine has implemented better ways to do this if you look around.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I am able to use classes that are not loaded without explicitly passing the autoloader in the LegionPE plugin https://github.com/LegionPE/LegionPE-Theta-Base/tree/master/src/legionpe/theta/query (note the AsyncQuery class). I wonder what the difference is?

    In case you are wondering, the autoload() namespace function was defined in an attempt to adapt to Steadfast2 earlier.
  6. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Yeah, I am not sure, there have been changes made to how the autoloader is handled. I was finding that the autoloader for the other thread would be missing any classes which weren't in the \pocketmine\* namespace.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Not the same PocketMine version? I'm using build #1261

Share This Page

Advertisement