I'm running this snippet of code inside another for loop repeatedly, so I thought I would be able to make my code really clean by calling a function a few times, instead of having this huge for loop within another for loop.
Can you take a look at it please? I appreciate your time. The function would be called find_target().
The problem I seem to be having is trying to figure out how to make the variable changes stick (You'll see the variables inside this snippet are changed, so outside of the function's scope the variables will be back to their original value (which is wrong)))
for ($k=1;$k<=$enemy_unit_count;$k++) {
$var = "player_unit".$i."_xloc";
$my_x = $$var;
$var = "player_unit".$i."_yloc";
$my_y = $$var;
$var = "player_unit".$i."_MOV";
$my_mov = $$var;
$var = "enemy_unit".$k."_xloc";
$enemy_x = $$var;
$var = "enemy_unit".$k."_yloc";
$enemy_y = $$var;
$new_distance = find_distance($my_x, $my_y, $enemy_x, $enemy_y);
if ($new_distance < $distance) {
$target = $k;
$target_x = $enemy_x;
$target_y = $enemy_y;
$distance = $new_distance;
}
}
**again, the code works perfect, just want to clean it up!**

New Topic/Question
Reply




MultiQuote





|