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

Weird error. Illegal offset 'name'

Comments in 'Plugin Development' started by Hotshot_9930, Mar 15, 2016.

  1. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Well, this has been frustrating me for a few days now, so i finally decided to come here for some help. Ive never had this problem before, but for some reason i get this error when i try to save a player name into an array:
    Code:
    InvalidKeyException: "Illegal string offset 'HotshotHD'" (E_WARNING) in "/RealmCore/src/realmcore/game/Freezetag" at line 201
    Here is the code:
    PHP:
    if(!isset($gameFrozenFT[$player->getName()])) {
      
    $this->freeze($player$gameId);
      
    $hitter->sendMessage($this->getPlugin()->formatMessage("§aYou froze §7" $player->getName(), "game"));
      
    $player->sendMessage($this->getPlugin()->formatMessage("§aYou have been §cfrozen""game"));
      
    $this->getPlugin()->getStats()->setPoints($hitter$this->getPlugin()->getStats()->getPoints($hitter) + 2);
    freeze() function:
    PHP:
    public function freeze(Player $player$gameId) {
       
    $gameFrozenStr "gameFrozenFT" $gameId;
       
       
    $this->getPlugin()->$gameFrozenStr[$player->getName()] = $player// Line 201 that the error points out
        
    It's probably some silly mistake i made, but it cant seem to find out what i did wrong. Any help is appreciated.
    Last edited: Mar 16, 2016
  2. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Your silly mistake is calling $this->freeze($player->getName(), $gameId); where $player->getName() is supposed to be a player object and not a string.
    As you can see you are trying to extract something from a string in line 201.
    applqpak and Vaivez66 like this.
  3. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    That was just a typo here. The error still happens.
    applqpak likes this.
  4. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    You first have to set the entry in the array to null, then overwrite it.
    applqpak likes this.
  5. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Why are you putting the player's name and then call the name again? It's like if you do $player->getName()->getName();

    Just put:

    PHP:
    public function freeze($str$gameId) {
       
    $gameFrozenStr "gameFrozenFT" $gameId;
      
       
    $this->getPlugin()->$gameFrozenStr[$str] = $player;
        }

    $this->freeze($player->getName(), $id);//call function
    applqpak likes this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What do you mean? Why can't you directly overwrite?

    Illegal string offset, does that actually mean that you are trying to use a string as an array? try var_dump
    HotFireyDeath likes this.
  7. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    applqpak likes this.
  8. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    As far as I know, you have to set it to null before being able to set it to something else.
    applqpak likes this.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Setting what to null?
    applqpak and ImagicalGamer like this.
  10. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Argh, I'm bad at explaining. The Index in the array. Example:
    PHP:
    $this->array["whatever"] = null// If it isn't set yet - Could also check with isset()
    $this->array["whatever"] = "hey";
    applqpak likes this.
  11. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Doesnt work. The error still occurs.
  12. HotFireyDeath
    Offline

    HotFireyDeath Notable Member Plugin Developer

    Joined:
    Mar 19, 2015
    Posts:
    328
    Plugins:
    1
    Minecraft User:
    HotFireyDeath
    Can you check to make sure that $this->getPlugin()->$gameFrozenStr; is an array and not a string?
    applqpak likes this.
  13. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Use var_dump... It is your friend.
    applqpak likes this.
  14. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Why would you need to set it to null? You're still assigning a value to it even if that value is a void.
    applqpak likes this.
  15. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    PHP:
    // change
    $this->getPlugin()->$gameFrozenStr[$player->getName()] = $player;
    // to
    $this->getPlugin()->gameFrozenStr[$player->getName()] = $player;
    applqpak likes this.
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It appears that he really means $plugin->$gameFrozenStr not $plugin->gameFrozenStr.
    applqpak likes this.
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No, you don't need to. This is totally redundant.
    applqpak likes this.
  18. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Then he clearly doesn't understand how arrays and variables work.
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No. It seems that he is really using $gameFrozenStr as a class property name. In PHP, this is valid code.
    Hotshot_9930 likes this.
  20. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    $gameFrozenStr is a string, unless $this->getPlugin()->$stringVariable defines a new variable for the class, I fail to see how it would work.

Share This Page

Advertisement