1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved How do I get a random level from config?

Discussion in 'Development' started by rektpixel, Jan 29, 2018.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Hello pmmp,
    I am trying to get a random level from the config, I have this:
    PHP:
    $this->arenas $config->get("arenas");
    foreach(
    $this->arenas as $lev);
    $this->newgame $lev[mt_rand($lev)];
    but I keep getting this error when the plugin starts up:
    Code:
    [20:33:39] [Server thread/CRITICAL]: ErrorException: "mt_rand() expects exactly 2 parameters, 1 given" (EXCEPTION) in "sg/src/SurvivalGamesV2/SurvivalGamesV2" at line 63
    [20:33:39] [Server thread/DEBUG]: #0 (): pocketmine\error_handler(integer 2, string mt_rand() expects exactly 2 parameters, 1 given, string C:\Users\Admin\Creative Cloud Files\Desktop\Lobby - DEV\plugins\sg\src\SurvivalGamesV2\SurvivalGamesV2.php, integer 63, array Array())
    [20:33:39] [Server thread/DEBUG]: #1 sg/src/SurvivalGamesV2/SurvivalGamesV2(63): mt_rand(string sgtest)
    [20:33:39] [Server thread/DEBUG]: #2 src/pocketmine/plugin/PluginBase(90): SurvivalGamesV2\SurvivalGamesV2->onEnable()
    [20:33:39] [Server thread/DEBUG]: #3 DevTools (2).phar/src/FolderPluginLoader/FolderPluginLoader(127): pocketmine\plugin\PluginBase->setEnabled(boolean 1)
    [20:33:39] [Server thread/DEBUG]: #4 src/pocketmine/plugin/PluginManager(610): FolderPluginLoader\FolderPluginLoader->enablePlugin(SurvivalGamesV2\SurvivalGamesV2 object)
    [20:33:39] [Server thread/DEBUG]: #5 src/pocketmine/Server(1955): pocketmine\plugin\PluginManager->enablePlugin(SurvivalGamesV2\SurvivalGamesV2 object)
    [20:33:39] [Server thread/DEBUG]: #6 src/pocketmine/Server(1941): pocketmine\Server->enablePlugin(SurvivalGamesV2\SurvivalGamesV2 object)
    [20:33:39] [Server thread/DEBUG]: #7 src/pocketmine/Server(1735): pocketmine\Server->enablePlugins(integer 1)
    [20:33:39] [Server thread/DEBUG]: #8 src/pocketmine/PocketMine(554): pocketmine\Server->__construct(BaseClassLoader object, pocketmine\utils\MainLogger object, string C:\Users\Admin\Creative Cloud Files\Desktop\Lobby - DEV\, string C:\Users\Admin\Creative Cloud Files\Desktop\Lobby - DEV\plugins\)
    [20:33:39] [Server thread/DEBUG]: #9 C:/Users/Admin/Creative Cloud Files/Desktop/Lobby - DEV/PocketMine-MP.phar(1): require(string phar://C:/Users/Admin/Creative Cloud Files/Desktop/Lobby - DEV/PocketMine-MP.phar/src/pocketmine/PocketMine.php)
    
    what do I do, I'm very confused :eek:
     
    Last edited: Jan 29, 2018
  2. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Also, how would I then save that random level as a variable?
    edit: would this be correct:
    PHP:
    $this->arenas $config->get("arenas");
    $level $this->arenas;
    $this->newgame $level[array_rand($level)];
     
    Last edited: Jan 29, 2018
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Didn't you already store it in $this->newgame?
     
  4. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Yeah I did, I just thought by using $this->newgame it would run random again but it doesn't because it happens on server enable :)
     
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You're storing the level, not the expression that resolves the level. PHP isn't that powerful.
     
    jasonwynn10 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.