silverglade Posted August 2, 2010 Share Posted August 2, 2010 Hi, when I run my script I get the following error message. These are array variables I am passing into the playerAttack function. I can't figure out what is wrong it looks fine to me. Any help greatly appreciated. Thanks. Derek attack posted Fatal error: Cannot use string offset as an array in C:\wamp\www\SUN_DRAGON_GAME\gamestart.php on line 202 $currentMonster='octalisk'; ///////////////////////////////////////////////////////////battle//////////////////////////// //function monsterEncounter($echoMonster,$monsterImage) if(!empty($_POST['attack'])) { echo "attack posted"; playerAttack($currentMonster,$player['stats']['playerHitRollLow'],$player['stats']['playerHitRollHigh'], $player['stats']['playerDmgLow'],$player['stats']['playerDmgHigh'],$monsters['Ocean']['octalisk']['defense'],$monster['Ocean']['octalisk']['hp'],$player['faction']['Teardrop_Ocean_Protectors']); $doesItAttack=encounter($monsters['ocean']['octalisk']['seeInvis'],$monsters['ocean']['octalisk']['aggro'],$player['factions']['Teardrop_Ocean_Protectors']); //insert octalisk stats into monsterattack function. if($doesItAttack =='yes') { monsterAttack($currentMonster,$monsters['Ocean']['octalisk']['hitRollLow'],$monsters['Ocean']['octalisk']['hitRollHigh'] , $monsters['Ocean']['octalisk']['dmgLow'],$monsters['Ocean']['octalisk']['dmgHigh'], $player['stats']['playerDefense'],$player['stats']['playerHp'],$monsters['Ocean']['octalisk']['hp']); } else { echo "do nothing"; } } here is the attack function function playerAttack($currentMonster,$hitRollLow,$hitRollHigh,$dmgLow,$dmgHigh,$monsterDefense, $monsterHp,$playerFaction) { echo "in player attack"; $playerRoll= rand($playerHitRollLow, $playerHitRollHigh); if($playerHitRoll>=$monsterDefense) { global $theMessage; //shit had to make it global $playerDamage= rand($playerDmgLow,$playerDmgHigh); $theMessage = "You have hit a ". $currentMonster." for "."<strong>". $playerDamage."</strong> points of damage!"; $monsterHp= $monsterHp- $playerDamage; $theMessage.=" The ".$currentMonster."'s hp is now ".$monsterHp; if($monsterHp<=0) { if($playerFaction<=0) { $theMessage="Your faction standing with ".$playerFaction. "could not possibly get any worse."; } } $theMessage.=" You have killed the ".$currentMonster. "Your faction standing with ".$playerFaction." is now ".$playerFaction-1; } else { global $theMessage; //WTF i dont know what im doing. $theMessage= " Whoa! You missed the ".$currentMonster; } } Quote Link to comment https://forums.phpfreaks.com/topic/209598-fatal-error-cannot-use-string-offset-as-an-array-in-cwampwwws/ Share on other sites More sharing options...
Skewled Posted August 2, 2010 Share Posted August 2, 2010 http://informationideas.com/news/2006/06/14/fatal-error-cannot-use-string-offset-as-an-array-in/ That should answer your question, you'll have to redo your arrays. Quote Link to comment https://forums.phpfreaks.com/topic/209598-fatal-error-cannot-use-string-offset-as-an-array-in-cwampwwws/#findComment-1094225 Share on other sites More sharing options...
silverglade Posted August 2, 2010 Author Share Posted August 2, 2010 Thank you. I don't see any duplicate values. Here are my arrays, do you see anything I need to change please? I can't figure it out. Thanks. Derek. $monster ['name']=''; $Teardrop_Ocean_Protectors=''; $random=''; $error=''; $monsterMessage=''; $echoMonster=''; $name=''; $ocean=''; $monster=''; $monsters=''; $monsters['ocean']=''; $player['factions']['Teardrop Ocean Protectors']=''; $monsters['ocean']['octalisk']['seeInvis']=''; $spells = array ( "red" => "red", "green"=>"green", "blue" =>"blue" ); ////////////////////////////////////////////////////////////// $player = array ( 'inventory'=>'', 'spells'=>'', 'stats'=> array ( 'int'=>16, 'playerHp'=>100, 'playerDmgLow'=>1, 'playerDmgHigh'=>10, 'playerHitRollLow'=>1, 'playerHitRollHigh'=>20, 'playerDefense'=>5 ), 'armor'=> array ( 'shield'=>'gold' ), 'weapons' =>array ( 'sword'=>'silver' ) , 'faction'=> array ( 'Teardrop_Ocean_Protectors'=>3 //zero means the worst faction you can have, 10 being best , 5 being indifferent. ) ); /////////////////////////////////////////////////////////////// $monsters = array ( 'Ocean'=>array ( 'octalisk'=>array ( 'name'=>'octalisk', 'hp'=>100, 'exp'=>10, 'mana'=>100, 'dmgLow'=>1, 'dmgHigh'=>10, 'seeInvis'=>true, 'aggro'=>true, 'defense'=>5, 'faction'=>'Teardrop_Ocean_Protectors', 'hitRollLow'=>1, 'hitRollHigh'=>20), 'shark'=>array ( ), 'eel'=>array ( ) ), 'Desert'=>array ( 'sand_snake' ), 'Forest'=>array ( 'frog', 'lizard', 'spider' ) ); Quote Link to comment https://forums.phpfreaks.com/topic/209598-fatal-error-cannot-use-string-offset-as-an-array-in-cwampwwws/#findComment-1094234 Share on other sites More sharing options...
AbraCadaver Posted August 2, 2010 Share Posted August 2, 2010 Where the hell is line 202? Quote Link to comment https://forums.phpfreaks.com/topic/209598-fatal-error-cannot-use-string-offset-as-an-array-in-cwampwwws/#findComment-1094267 Share on other sites More sharing options...
silverglade Posted August 2, 2010 Author Share Posted August 2, 2010 oops sorry //I NEED THE FOLLOWING CODE BUT IT GAVE AN ERROR SO I COMMENTED IT OUT // echo "attack posted"; // playerAttack($currentMonster,$player['stats']['playerHitRollLow'],$player['stats']['playerHitRollHigh'], // line 202 $player['stats']['playerDmgLow'],$player['stats']['playerDmgHigh'],$monsters['Ocean']['octalisk']['defense'],$monster['Ocean']['octalisk']['hp'],$player['faction']['Teardrop_Ocean_Protectors']); $doesItAttack=encounter($monsters['ocean']['octalisk']['seeInvis'],$monsters['ocean']['octalisk']['aggro'],$player['factions']['Teardrop_Ocean_Protectors']); Quote Link to comment https://forums.phpfreaks.com/topic/209598-fatal-error-cannot-use-string-offset-as-an-array-in-cwampwwws/#findComment-1094275 Share on other sites More sharing options...
jcbones Posted August 3, 2010 Share Posted August 3, 2010 Somewhere above line 202: you have declared one of your variables as a string, therefore you cannot re-declare it as an array, unless done explicitly. Quote Link to comment https://forums.phpfreaks.com/topic/209598-fatal-error-cannot-use-string-offset-as-an-array-in-cwampwwws/#findComment-1094386 Share on other sites More sharing options...
kenrbnsn Posted August 3, 2010 Share Posted August 3, 2010 You have problems in this chunk of code. I indicate them with "// <<------" on the lines <?php $monster ['name']=''; $Teardrop_Ocean_Protectors=''; $random=''; $error=''; $monsterMessage=''; $echoMonster=''; $name=''; $ocean=''; $monster=''; // <<----- Already used $monster as an array above $monsters=''; $monsters['ocean']=''; // <<---- Already used $monsters as a string $player['factions']['Teardrop Ocean Protectors']=''; $monsters['ocean']['octalisk']['seeInvis']=''; $spells = array ( "red" => "red", "green"=>"green", "blue" =>"blue" ); ////////////////////////////////////////////////////////////// $player = array ( 'inventory'=>'', 'spells'=>'', 'stats'=> array ( 'int'=>16, 'playerHp'=>100, 'playerDmgLow'=>1, 'playerDmgHigh'=>10, 'playerHitRollLow'=>1, 'playerHitRollHigh'=>20, 'playerDefense'=>5 ), 'armor'=> array ( 'shield'=>'gold' ), 'weapons' =>array ( 'sword'=>'silver' ) , 'faction'=> array ( 'Teardrop_Ocean_Protectors'=>3 //zero means the worst faction you can have, 10 being best , 5 being indifferent. ) ); /////////////////////////////////////////////////////////////// $monsters = array ( 'Ocean'=>array ( 'octalisk'=>array ( 'name'=>'octalisk', 'hp'=>100, 'exp'=>10, 'mana'=>100, 'dmgLow'=>1, 'dmgHigh'=>10, 'seeInvis'=>true, 'aggro'=>true, 'defense'=>5, 'faction'=>'Teardrop_Ocean_Protectors', 'hitRollLow'=>1, 'hitRollHigh'=>20), 'shark'=>array ( ), 'eel'=>array ( ) ), 'Desert'=>array ( 'sand_snake' ), 'Forest'=>array ( 'frog', 'lizard', 'spider' ) ); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/209598-fatal-error-cannot-use-string-offset-as-an-array-in-cwampwwws/#findComment-1094420 Share on other sites More sharing options...
silverglade Posted August 3, 2010 Author Share Posted August 3, 2010 awesome thanks everyone, it works now, not right, but it works. THANKS. Derek :D Quote Link to comment https://forums.phpfreaks.com/topic/209598-fatal-error-cannot-use-string-offset-as-an-array-in-cwampwwws/#findComment-1094486 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.