Selacius Posted June 7, 2010 Share Posted June 7, 2010 Hello, I used to frequent these forums a while back while creating my Web-Based PHP Game. I've recently got back into it but upon looking it seems I have deleted all of my files, therefore I have to start fresh. I have run into a problem with Functions and cannot for the life of me remember how to get past it. I am calling a function with the intent that the function will fill in two values of the same array (a value in 0 and a value in 1), and then update a third array. However, no matter what I do, it seems that during the calling of the function the variables get updated, but checking after the function is called, the variables revert to 0. Here is the function: function playerstart ($map, $id, $playerdata) { $startx = rand(1,24); $starty = rand(1,24); if ($map[$startx][$starty] == " ") { $playerdata[$id][1] = $startx; //X Starting Location $playerdata[$id][2] = $starty; //Y Starting Location $map[$startx][$starty] = "*"; } else { playerstart ($map, $id, $playerdata); } echo $playerdata[$id][1]; } Link to comment Share on other sites More sharing options...
justsomeguy Posted June 8, 2010 Share Posted June 8, 2010 Passing variables like that will only pass a copy of the variable, where the changes are only visible to that one function. If you want to pass a reference to the variable, where after the function ends the changes to those variables are visible to everything else, you can put a & before the parameter names to indicate that they are being referenced instead of copied.function playerstart (&$map, $id, &$playerdata) { Link to comment Share on other sites More sharing options...
Selacius Posted June 8, 2010 Author Share Posted June 8, 2010 Sweet, that is exactly what I needed:)Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.