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

Error with $config->set() function

Discussion in 'Development' started by DanielYTK, Mar 2, 2017.

  1. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    Look the set:
    PHP:
    if($cfga [$nick]["level"] === || $cfga [$nick]["xp"] >= 80){
                        
    $aco->set($nick["level"], 1);
                        
    $aco->set($nick["xp"], + 0);
                        
    $player->sendMessage("§eParabéns, você upou para o nivel §a1§e em §aAcrobacia");
                    }
     
    look the config.yml:
    Sem título.png

    $aco->set([$nick]["level"], 1); don't work.
     

    Attached Files:

  2. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    Creating the archive:
    PHP:
         public function aoEntrar(PlayerJoinEvent $ev){
            
    $player $ev->getPlayer();
            
    $nick $player->getName();
                
    $aco = new Config($this->getDataFolder()."players/acrobacia/".$nick.".yml"Config::YAML, array(
                
    $nick => [
                
    "xp" => 0,
                
    "level" => 0,
                ]));
                
    $aco->save();
    }
     
  3. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    Hi zLeoDZN, what is the $cfga ???
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    You forgot to to save the config right after.
     
  5. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    PHP:
     $cfga = new Config($this->getDataFolder()."players/acrobacia/".$nick.".yml",Config::YAML); 
     
  6. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    look the image.
     
  7. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    In the code you shared, you didn't do $aco->save after you did $aco->set(). You have to save it after you change values.
     
    Sandertv likes this.
  8. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    look this:
    PHP:
    if($ev->getDamage() >= 20){
                    
    $aco->set([$nick]["xp"], $cfga[$nick]["xp"] +10);
                    
    $player->sendMessage("§eForam adicionados §a+10§e pontos a sua §aEXPERIÊNCIA §eem §aAcrobacia");
                    
    $aco->save();
                    return;
                    }
     
     
  9. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    i need set XP and not creating other line!!
     
  10. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Does this work, with $aco->save() added? Your original code did not have that. If not, does the message send? Do you get any errors?
    Saving the value after you set it is necessary for it to actually be set in the config.
     
  11. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    What is the value of
    PHP:
    [$nick]["xp"]
    ?
     
  12. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    PHP:
     $aco = new Config($this->getDataFolder()."players/acrobacia/".$nick.".yml"Config::YAML, array(
                
    $nick => [
                
    "xp" => 0,
                
    "level" => 0,
                ]));
    To set a value for xp, should I do this $ aco-> set ([$ nick] ["xp"], 1) ;? Or do I have to do something else?
     
  13. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    I just need it!
     
  14. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    You're trying to set things in the config as if $cfga is an array, which it is not. It is a Config object. If you want to use the method you currently are, I'd recommend putting (new Config(...)->getAll) instead of just new Config(...).
     
    Muqsit likes this.
  15. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    1. Use a global code format for simplicity.
    2. Understand what functions do rather than blindly using them.
    3.
    PHP:
                $config = new Config(
                                    
    $this->getDataFolder()."players/acrobacia/".                      $nick.".yml",                 Config::YAML
     
    );
                                            
    $config->set(
                         
    $nick["xp"],
    1);
    ...assuming $nick is a variable and not a string.
     
    Last edited: Mar 3, 2017
    Sandertv likes this.
  16. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    No, it's used like a string. He is just using it like an array for whatever the reason. This should be in the facepalm section.
     
    HimbeersaftLP and Sandertv like this.
  17. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Since this is probably gonna end up in the facepalm section, I've updated my code formatting too. Hope people find that useful
     
    Sandertv 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.