Well, I got stuck again where I don't know how to get a variable defined in another function. In other words, how do you get "qwerty" in the function retrieve? PHP: public function generate(){$var = "qwerty";}public function retrieve(){???}
PHP: public function generate(){$this->var = "qwerty";}public function retrieve(){var_dump($this->var);}
PHP: public function generate(){ $var = 'qwerty'; // Read about using single and double quotes http://www.scriptingok.com/tutorial/Single-quotes-vs-double-quotes-in-PHP $this->retrieve($var);}public function retrieve($var) { echo $var; // qwerty} Study more about OOP (Object Oriented Programming) @ https://www.codecademy.com/courses/web-beginner-en-bH5s3/0/1
You can also make a public variable PHP: public $var = "qwerty";public function retrieve() { echo $this->var;}
Not quite. @Primus's method requires you to use the generate() function each time you want to use the variable. My method, creating a public variable allows you to use that variable throughout the entire script.
I think @minebuilder0110 meant to retrieve a variable back after getting it in a function. In that way, store that in a class property like @Hotshot_9930 showed. It is, however, common code style to let anything internal be private instead of public unless necessary. If you simply want that to be passed when you call another function, though, just pass the variable directly through function parameters instead of storing to class properties.
Ok, first, sorry for bumping an old thread, I think it is better than creating a new one. Sorry if I was inconsiderate. PHP: class Main extends PluginBase implements Listener{ public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getLogger()->info(TextFormat::GREEN."Loading was successful. Made by 0110 (7oD)."); @mkdir($this->getDataFolder()); $this->yml = new Config($this->getDataFolder().'health.yml', Config::YAML); } public function onLevelChange(EntityLevelChangeEvent $event) { $player = $event->getEntity(); if ($player instanceof Player) { $world = $event->getOrigin()->getName(); $health = $player->getHealth(); $name = $player->getName(); $targetWorld = $event->getTarget()->getName(); $this->yml->setNested($name . '.' . $world, $health); $loadHealth = $this->yml->getNested("$name.$targetWorld"); if(!isset($loadHealth)) $loadHealth = 20;// $player->sendMessage("Your health $health is now saved and now your health is $loadHealth."); $player->setHealth($loadHealth); } } This is the plugin I want to write, and $this->yml needs to be used in both onEnable (to define), and in onLevelChange. I don't know if I am doing this correctly, but if it is, how would I connect $this->yml together? I have tried all methods above, and none of them seemed to work. If I am doing wrong (which I am expecting), how would I make the plugin to save certain value in a different yml? (in this case, health.yml)