Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to use a variable throughout all functions

Comments in 'Plugin Development' started by minebuilder0110, Oct 30, 2015.

  1. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    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(){
    ???
    }
  2. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    public function generate(){
    $this->var "qwerty";
    }
    public function 
    retrieve(){
    var_dump($this->var);
    }
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    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
  4. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    You can also make a public variable
    PHP:
    public $var "qwerty";

    public function 
    retrieve() {
      echo 
    $this->var;
    }
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    It is actually the same.
  6. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    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.
  7. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    But isn't $this->xxx a public var too? By default, I think it is public :)
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
    minebuilder0110 likes this.
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
  10. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    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)
  11. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    This should work. Have you tested it?
  12. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    setHealth has bugs. It does not work.

Share This Page

Advertisement