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

Solved Error: "Call to a member function set() on array"

Discussion in 'Development' started by ShushImSam, Aug 5, 2019.

  1. ShushImSam

    ShushImSam Spider Jockey

    Messages:
    37
    Hello, so before getting to this point I actually looked threads and threads and went on google without being able to understand what I've done wrong here.. Could anyone help me? Basically Im trying that when a player join it sets in the keys.yml that they have 0 keys etc under their name althought I always get this specific error:
    Error: "Call to a member function set() on array" (EXCEPTION) in "~~~" at line 28
    PHP:
        /** @var Config */
        
    public $config;
        public function 
    onEnable()
        {
            
    $this->config = new Config($this->getDataFolder() . "keys.yml"Config::YAML, []);
            
    $this->config $this->getConfig()->getAll();
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info("Keys are working properly");
        }
        public function 
    onJoin(PlayerJoinEvent $event){
            
    $name $event->getPlayer()->getName();
            
    $common["Common Keys"] = 0;
            
    $common["Uncommon Keys"] = 0;
            
    $common["Rare Keys"] = 0;
            
    $common["Legendary Keys"] = 0;
    //Line 28
            
    $this->config->set($name$common);
    If I find my mistake I'll edit my post and let you guys know
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    In that line you are overwriting your config property of the Config type with an array and an array doesn't have the set method that you're trying to use here:
    So just remove the "$this->config = $this->getConfig()->getAll();" line and if you need the config as an array then use a different variable for that.
     
    ShushImSam likes this.
  3. ShushImSam

    ShushImSam Spider Jockey

    Messages:
    37
    Will try,

    While searching though I found this thread:
    https://forums.pmmp.io/threads/arrays-in-config-yaml.1513/ which actually worked and makes it simplier.
    Basically just use yaml_parse_file.
    For example:

    PHP:
            $name $event->getPlayer()->getName();
            
    $path $this->getDataFolder()."keys.yml";
            
    $toArray yaml_parse_file($path);
            
    $toArray["$name"] = ["Common" => 0"Uncommon" => 0"Rare" => 0,"Legendary" => 0];
            
    yaml_emit_file($path$toArray);
     
  4. ShushImSam

    ShushImSam Spider Jockey

    Messages:
    37
    Just want to state if anyone comes around that this works aswell and is easier :p
    Thanks! ^^
     
    HimbeersaftLP likes this.
  5. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Note that yaml_parse_file and yaml_emit_file will remove comments though
     
    ShushImSam 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.