Yes, you can save variables by using serialize function in PHP, but why would you want to save variables?
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
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())
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.
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.
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 valueecho $entity->namedtag["YourData"]; // echoes hi
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.
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?
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.
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.