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

How to prevent an array from unset() on server restart

Discussion in 'Development' started by Atomization, Jun 7, 2018.

  1. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    My array keeps getting reset on server restart, how can i prevent this?
    PHP:
    public $array = [];
     
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Save it when plugin disables, and load it when plugin enables.

    PHP:
    public function onEnable(){
      if(
    is_file($arraySl $this->getDataFolder() . "array.sl")){
        
    $this->array unserialize(file_get_contents($arraySl));
      }
    }
    public function 
    onDisable(){
      
    file_put_contents($this->getDataFolder() . "array.sl"serialize($this->array));
    }
    Remember not to store objects like Player or Level.
     
  3. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    no... just no

    https://forums.pmmp.io/threads/why-not-to-serialize-objects-for-storage.5614/
     
    Kenn Fatt likes this.
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    It is perfectly ok to use serialize()/unserialize() as an alternative of yaml_emit()/yaml_parse() as long as you don't try to store any objects.
     
  5. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Is uuid object?
     
  6. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    im storing the item ids of specific items
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    No. Not any objects from the PocketMine API. See @dktapps' post for why.
     
  8. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    ok, im not sure how to use serialize()/unserialize() or yaml_emit()/yaml_parse() to keep the array from being wiped on restart. can i have an example of how to do it?
     
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    serialize()/yaml_emit() produces a string. Use file_put_contents() to write it to a file, e.g. file_put_contents($this->getDataFolder() . "data.yml", yaml_emit($this->array));. Use file_get_contents() to read a file, and turn it into an array with unserialize()/yaml_parse(), e.g. $this->array = yaml_parse(file_get_contents($this->getDataFolder() . "data.yml"));.
     
  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.