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.
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.
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
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
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";
Why would you need to set it to null? You're still assigning a value to it even if that value is a void.
PHP: // change$this->getPlugin()->$gameFrozenStr[$player->getName()] = $player;// to$this->getPlugin()->gameFrozenStr[$player->getName()] = $player;
No. It seems that he is really using $gameFrozenStr as a class property name. In PHP, this is valid code.
$gameFrozenStr is a string, unless $this->getPlugin()->$stringVariable defines a new variable for the class, I fail to see how it would work.