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

How do I get particular items from an array in config?

Discussion in 'Development' started by RedCraftPM, Oct 4, 2018.

  1. RedCraftPM

    RedCraftPM Spider

    Messages:
    8
    GitHub:
    redcraftgh
    I am having some trouble getting specific items from an array that is in my config.yml file.
    Here is what I have tried:
    Code:
    $array = Main::getInstance()->config->get("Players");
    
    $player1 = Main::getInstance()->getServer()->getPlayerExact($array[0]); //line 84
    $player2 = Main::getInstance()->getServer()->getPlayerExact($array[1]);
    And the inside of my config file:
    Code:
    Players:
    - RedCraftPE
    - ItsMax123 
    Unfortunately, this is not working and I am getting this error:
    Code:
    Server thread/CRITICAL ErrorException: "Undefined offset: 0" (EXCEPTION) in "Plugin/src/RedCraftPE/Tasks/Task" at line 84 
    If it is not possible to get individual elements of an array in a config file like this please let me know the proper way to do so, and explain why the method I used doesn't work.
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    do a var_dump($array); to see the structure of the array so that you will know how to access certain parts of the array
     
  3. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    ["Name"]
     
  4. RedCraftPM

    RedCraftPM Spider

    Messages:
    8
    GitHub:
    redcraftgh
    It is telling me that the array is empty.
    Code:
    array(0) {
    }
    
    I am trying to get the name from the array while assuming that I don't know the name that I am trying to get. The array won't always have the same names in it.
     
  5. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    If its empty, check that you are reading from the correct file,your code should be working fine
     
  6. RedCraftPM

    RedCraftPM Spider

    Messages:
    8
    GitHub:
    redcraftgh
    I am like 98% sure that I am reading the correct file.
    Code:
    $this->config = new Config($this->getDataFolder() . "config.yml", Config::YAML);
    
    I definitely know that this is the correct file because it is writing/reading data in the Main.php file, the problem occurs when I try and use the file in a scheduled repeating task.
     
  7. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Just to be sure, do a
    PHP:
    var_dump(Main::getInstance()->config->getAll());
    You should at least see an array that is not empty
     
  8. RedCraftPM

    RedCraftPM Spider

    Messages:
    8
    GitHub:
    redcraftgh
    That helped me find the problem I think. I put that at the top of my task file and when it ran everything appeared to be normal, so I looked through everything that was there and found a piece of the file which may have been returning the wrong value.
     
  9. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Do you save the config?
     
    Eren5960 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.