How can I put this code in a function?

to clean up

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1368 Views - Last Post: 26 May 2009 - 08:38 AM Rate Topic: -----

#1 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

How can I put this code in a function?

Posted 24 May 2009 - 01:26 PM

Hi guys,

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!**

Is This A Good Question/Topic? 0
  • +

Replies To: How can I put this code in a function?

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How can I put this code in a function?

Posted 24 May 2009 - 02:14 PM

You have to look up how you send variables to functions by reference! Example:
function DoSomething(&$param1, &$param2) { }

the "&" tells PHP to send the variable reference to the function instead of the variable value, therefore the change will affect the variable even if it is outside of the functions scope! And you don't have to return anything, unless you want to!

A more complete example:
<?php
$x = 5;
function Add(&$var)
{
	$var += 5;
}
Add($x);
echo $x;
?>
This should output 10!

Hope it helps! :)

This post has been edited by Wimpy: 24 May 2009 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#3 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: How can I put this code in a function?

Posted 24 May 2009 - 02:16 PM

!!!! wow!!!!

wow is that it? So using "&" will allow me to work with that variable outside the scope of the function? ((meaning the changes will affect the variable even when we exit the function?))

This post has been edited by Xioshin: 24 May 2009 - 02:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How can I put this code in a function?

Posted 24 May 2009 - 02:18 PM

Exactly, I think I edited my post while you were replying, check the "more complete example" and try it out! :)

View PostXioshin, on 24 May, 2009 - 11:16 PM, said:

holy ****

wow is that it? So using "&" will allow me to work with that variable outside the scope of the function? ((meaning the changes will affect the variable even when we exit the function?))

This post has been edited by Wimpy: 24 May 2009 - 02:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: How can I put this code in a function?

Posted 24 May 2009 - 02:23 PM

Oh wimpy that's awesome, thanks a lot!

I have a question tho. See the variables $target_x, $target_y, and I think a few others.

These variables have no values outside of the function (before being created the first time the code runs). How do I handle that?

I think the values for those variables will be lost when we exit the function, no?
Was This Post Helpful? 0
  • +
  • -

#6 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How can I put this code in a function?

Posted 24 May 2009 - 02:25 PM

The problem you have is that there are so many variables that you need inside the function, I think you should try to make it simpler in some way, if it's possible! :)

View PostXioshin, on 24 May, 2009 - 11:23 PM, said:

Oh wimpy that's awesome, thanks a lot!

I have a question tho. See the variables $target_x, $target_y, and I think a few others.

These variables have no values outside of the function (before being created the first time the code runs). How do I handle that?

I think the values for those variables will be lost when we exit the function, no?

Was This Post Helpful? 0
  • +
  • -

#7 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: How can I put this code in a function?

Posted 24 May 2009 - 02:31 PM

This is a snippet of hundreds of lines of code. The entire project is indeed starting to get complex :)

Functions will make it not look so complicated, but those variables are very necessary. The issue you don't know about is that there is an undefined number of units that may need to be handled, which is why I am working with a dynamic number of variables.

I will do my best to simplify everything of course!. I appreciate your help.

Could you help me out with my last question about the $target_x and $target_y? I can't really pass it as a reference if I don't have a value for it yet..
Was This Post Helpful? 0
  • +
  • -

#8 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How can I put this code in a function?

Posted 24 May 2009 - 02:33 PM

Looking at your code, I think that you could put this outside of the for-loop:
	$var = "player_unit".$i."_xloc";
	$my_x = $$var;
	$var = "player_unit".$i."_yloc";
	$my_y = $$var;
	$var = "player_unit".$i."_MOV";
	$my_mov = $$var;
since it doesn't use the $k variable in any way!

Then you would only have to bother with this:
for ($k=1;$k<=$enemy_unit_count;$k++) {
	$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; 
	}
}



--- EDIT ---

Create a dummy value for each? :)
$target_x = $target_y = null;


This post has been edited by Wimpy: 24 May 2009 - 02:34 PM

Was This Post Helpful? 1
  • +
  • -

#9 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: How can I put this code in a function?

Posted 24 May 2009 - 02:58 PM

You have been Thanked!

Ah, yes. That piece of code that does not use $k is supposed to be outside the loop - little oversight.


I appreciate your help!
Was This Post Helpful? 0
  • +
  • -

#10 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How can I put this code in a function?

Posted 24 May 2009 - 03:17 PM

No problem, happy to help! How is the AJAX/PHP-game doing, btw? :)

View PostXioshin, on 24 May, 2009 - 11:58 PM, said:

You have been Thanked!

Ah, yes. That piece of code that does not use $k is supposed to be outside the loop - little oversight.


I appreciate your help!

Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3037
  • View blog
  • Posts: 10,613
  • Joined: 08-August 08

Re: How can I put this code in a function?

Posted 24 May 2009 - 03:18 PM

I believe that you could shorten this:
for ($k=1;$k<=$enemy_unit_count;$k++) {
	$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; 
	}
}


to this:
for ($k=1;$k<=$enemy_unit_count;$k++) {
	$target_x = "enemy_unit".$k."_xloc";
	$target_y = "enemy_unit".$k."_yloc";
	$new_distance = find_distance($my_x, $my_y, $$target_x, $$target_y);
	if ($new_distance < $distance) { 
		$target = $k;
		$distance = $new_distance; 
	}
}


This post has been edited by CTphpnwb: 24 May 2009 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#12 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: How can I put this code in a function?

Posted 24 May 2009 - 03:24 PM

Woops I read what you said wrong. I'll take a closer look at it.

This post has been edited by Xioshin: 24 May 2009 - 03:26 PM

Was This Post Helpful? 0
  • +
  • -

#13 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How can I put this code in a function?

Posted 24 May 2009 - 03:26 PM

Tralala, was reading with the wrong glasses too! :)

This post has been edited by Wimpy: 24 May 2009 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#14 Xioshin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 264
  • Joined: 05-November 08

Re: How can I put this code in a function?

Posted 24 May 2009 - 03:26 PM

Hahaha I know what it means, I've used that in my original code :-P

I misread what he posted. I believe he made a mistake and edited it out.

This post has been edited by Xioshin: 24 May 2009 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#15 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: How can I put this code in a function?

Posted 24 May 2009 - 03:29 PM

He's playing with our minds! :crazy:

-- EDIT --

I'm tired, so excuse my somewhat weird post!

View PostXioshin, on 25 May, 2009 - 12:26 AM, said:

Hahaha I know what it means, I've used that in my original code :-P

I misread what he posted. I believe he made a mistake and edited it out.

This post has been edited by Wimpy: 24 May 2009 - 03:36 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2