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

Save data on entity forever

Discussion in 'Development' started by imYannic, Nov 19, 2016.

Thread Status:
Not open for further replies.
  1. imYannic

    imYannic Baby Zombie

    Messages:
    113
    How can I add data (strings) to an entity which is saved, and callable even after reboot?
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    I guess everything on namedTag (NBT Compound Tag) is written on entity data file.
     
    imYannic likes this.
  3. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Does it save the custom variables like $ent->namedtag->customvar = "123"; too?
     
  4. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Yes, you can save variables by using serialize function in PHP, but why would you want to save variables?
     
    imYannic likes this.
  5. imYannic

    imYannic Baby Zombie

    Messages:
    113
    I have a shop project at the moment, I want to save the category of the offer/kit in the object, so I can call the category everytime, even after reboot
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You can't serialize a Closure. But if it is static method/function name, it is valid.
    Using nonstatic methods won't work because the object reference would be different.

    It is actually best if you just store the parameters needed, as well as an identifier for your handler such that your plugin can identify which method to call from the identifier. I recommend not to store method names directly, as it may expose security leak. (That's as dangerous as calling eval())
     
    Primus likes this.
  7. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    There's multiple ways of doing this but the quickest way is to add data to an entity is through NBT, you can do this by adding your new NBT fields to the the entities existing compound tag/named tag:
    PHP:
    Entity::$namedtag->YourNewField = new pocketmine\nbt\StringTag("YourNewField""value");
    # Just so people don't complain about it looking like a static property
    $entity->namedtag->YourNewField = new pocketmine\nbt\StringTag("YourNewField""value");
    This will store the string with the entity until it is killed. You will have to perform your own checks to make sure the field exists or you'll end up with some errors trying to get or set a value that doesn't exist.
     
  8. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Downvote, that doesn't save the value either. Only for the uptime. After reboot it's deleted.
     
  9. Primus

    Primus Zombie Pigman

    Messages:
    749
    You sure entity got saved correctly?
     
  10. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Yes, I set it on 20 test villagers, checked it (it returned the value), then rebooted it and everyone of the 20 villagers threw the error "Undefined property "test"". So it is deleted after reboot.
     
  11. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Downvote, it works fine. Make sure you're accessing the property in the correct manner ;)
    PHP:
    // Assume $entity is an instance of pocketmine\entity\Entity
    $entity->namedtag->YourData = new StringTag("YourData""boo"); // add a new StringTag
    $entity->namedtag->YourData->setValue("hi"); // sets the existing StringTag's value
    echo $entity->namedtag["YourData"]; // echoes hi
     
  12. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Try it after rebooting! :D
     
  13. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    If it were to reset after being saved then you're doing something very wrong. PM uses NBT to save all properties of entities, items and chunks so if it reset then your server would more or less reset after every restart.
     
  14. Aviv

    Aviv Baby Zombie

    Messages:
    156
    sorry for off topic, but if i do serialize($this->factions) will it save this variable forever? could i use it few days later of year later?

    EDIT: and is it an efficient way of saving data?
     
  15. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    please make a new thread since it is unrelated to this thread in anyway
    thanks
     
  16. Aviv

    Aviv Baby Zombie

    Messages:
    156
    you could just help me, why instead you tell me to add more and more threads to this forum, i dont think i should open a new thread for that little question.
     
  17. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Because this is irrelevant to this thread. Opening more threads is not a problem if it helps future readers to find your question more easily.
    Data saving is quite a big topic, and it not only deserves another thread, but also even another tag or even a whole article introducing different popular types of data saving in PocketMine plugins.
     
Thread Status:
Not open for further replies.
  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.