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

[HELP] Class $sound not found

Comments in 'Plugin Development' started by TonyDroide, Jun 25, 2015.

  1. TonyDroide
    Offline

    TonyDroide Active Member Plugin Developer

    Joined:
    Jun 5, 2015
    Posts:
    122
    Plugins:
    1
    Minecraft User:
    TonyDroidd
    i have a problem with addSound:

    when i use the code:
    PHP:
              $level->addSound(new $Sound($p->getLocation()));
    it works!


    but, when i use the code with variables (on this case, $sound):

    PHP:
                            $Sound=$cfg->get("Sound");
                
    $level->addSound(new $Sound($p->getLocation()));
    the code does not work :( this is the log:
    [​IMG]

    i added ALL the sound classes.
    (((is not problem of my config folder)))
  2. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
  3. Guillaume351
    Offline

    Guillaume351 Active Member Plugin Developer

    Joined:
    Oct 11, 2013
    Posts:
    150
    Plugins:
    1
    Minecraft User:
    Guillaume351
    I know you said you imported everything, but are you sure there are use pocketmine\level\sound\PopSound; ? :p
    Try $this->plugin->getServer()->getDefaultLevel()->addSound(new PopSound($pos));
    To see if it's from popsound not correctly imported or if it's something else

    Edit : Sound is not a variable :3
    Last edited: Jun 25, 2015
    iksaku and TonyDroide like this.
  4. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    What!? You are trying to create a variable instance!
    PHP:
    $level->addSound(new $Sound($p->getLocation()));
    You should create a class instance
    iksaku likes this.
  5. TonyDroide
    Offline

    TonyDroide Active Member Plugin Developer

    Joined:
    Jun 5, 2015
    Posts:
    122
    Plugins:
    1
    Minecraft User:
    TonyDroidd
    Yes, im sure
  6. TonyDroide
    Offline

    TonyDroide Active Member Plugin Developer

    Joined:
    Jun 5, 2015
    Posts:
    122
    Plugins:
    1
    Minecraft User:
    TonyDroidd
    How?
  7. Guillaume351
    Offline

    Guillaume351 Active Member Plugin Developer

    Joined:
    Oct 11, 2013
    Posts:
    150
    Plugins:
    1
    Minecraft User:
    Guillaume351
    addSound(new PopSound($pos))
    iksaku and EvolSoft like this.
  8. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    remove $ on before Sound! Sound is a class not a variable!
    PixelGuy75 and RekkuzaRage like this.
  9. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    252
    Minecraft User:
    RekkuzaRage
    You should learn more about OOP in PHP. Sound can't be a varible. "new Sound" creates an instance of the sound class in PocketMine.
    iksaku likes this.
  10. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    If you are using a variable to store the class of the object you are creating you must use the full class path.

    So using "use" to import and only the base name is not enough.
    Falk and iksaku like this.
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    $Sound "pocketmine\\level\\sound\\" $config->get("things");
    $level->addSound(new $Sound (position));
  12. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    In deed, you can use a variable as a class identifier, But you must be sure that the class instance is correctly assigned to the variable, so you will be able to clone it
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    To make sure the config didn't give wrong classes, use class_exists and is_subclass_of.
    TonyDroide likes this.
  14. TonyDroide
    Offline

    TonyDroide Active Member Plugin Developer

    Joined:
    Jun 5, 2015
    Posts:
    122
    Plugins:
    1
    Minecraft User:
    TonyDroidd
    That '$' is because '$Sound=$cfg->get("Sound");'
  15. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    Of course that won't work! You can't create an instance with just a variable(like the one you did)! Please take a look here: http://php.net/manual/en/language.oop5.php and learn something from it.
  16. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    The S shouldn't be capitalized if we are complying to standards.
  17. Gamecrafter
    Offline

    Gamecrafter Notable Member Plugin Developer

    Joined:
    Nov 20, 2014
    Posts:
    981
    Plugins:
    9
    Isn't it a good practice to keep all variables lowercase?
  18. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Yeah, camelCase for variable names AndTheOtherCaseIForget for classes.
    TonyDroide likes this.
  19. TonyDroide
    Offline

    TonyDroide Active Member Plugin Developer

    Joined:
    Jun 5, 2015
    Posts:
    122
    Plugins:
    1
    Minecraft User:
    TonyDroidd
    thank you, ir works :*
  20. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    My personal practice to use capital prefixes if the variable is a class name.
    TonyDroide likes this.

Share This Page

Advertisement