function() inputs and outputs
Question posted by: TheServant
(Needs Regular Fix)
on
May 16th, 2008 01:02 PM
Hey guys,
I have an untraining script in my game which I am having a small problem with. There are 4 levels of soldiers: rec, reg, exp and vet. Now the number of soldier is recorded as the total, so when a user "untrains" some, it will take it from the total. I want to remove rec's first, reg's second, etc... Now I think the function works, but I just don't know how to do inputs and outputs. I want to automatically assign the outputs to variables, let me know if this is unclear!
- function untrainer ( $un, $rec, $reg, $exp, $vet, &$new_rec, &$new_reg, &$new_exp, &$new_vet ) {
-
$new_reg = $reg; $new_exp = $exp; $new_vet = $vet;
-
if ( $rec >= $un ) {
-
$new_rec = $rec - $un ;
-
} else {
-
$new_rec = $un - $rec;
-
$un = $un - $rec;
-
if ($reg >= $un ) {
-
$new_reg = $reg - $un ;
-
} else {
-
$new_reg = $un - $reg;
-
$un = $un - $reg;
-
if ($exp >= $un ) {
-
$new_exp = $exp - $un ;
-
} else {
-
$new_exp = $un - $exp;
-
$un = $un - $exp;
-
$new_vet = $un - $vet;
-
}
-
}
-
}
-
}
-
-
untrainer ( $un, $rec_soldier, $reg_soldier, $exp_soldier, $vet_soldier, &$new_soldier_rec, &$new_soldier_reg, &$new_soldier_exp, &$new_soldier_vet );
|
|
May 16th, 2008 11:18 PM
# 2
|
Re: function() inputs and outputs
I have no idea of the problem, but maybe that is because I am not a gamer.
Ronald
|
|
May 17th, 2008 03:18 AM
# 3
|
Re: function() inputs and outputs
I want to know how I can submit several variables to a function, and receive several variables back. Forget the game and what the function does. Just look at how I am calling the function (giving it 5 variables and receiving 4).
Thanks for your reply tho!
|
|
May 17th, 2008 07:39 AM
# 4
|
Re: function() inputs and outputs
That looks all right to me. If you change the values of the last four parameters in your function, the variables you assign to them when you call the function should change as well.
I assume you are attempting something like this:
-
function increment(&$value) {
-
$value++;
-
}
-
-
$number = 1;
-
increment($number);
-
echo $number; // Should be 2
|
|
May 17th, 2008 10:00 AM
# 5
|
Re: function() inputs and outputs
Quote:
I have no idea of the problem, but maybe that is because I am not a gamer.
Ronald
|
*clueless too*
Message too short.
|
|
May 17th, 2008 02:23 PM
# 6
|
Re: function() inputs and outputs
OK, forget the first post. I have 4 variables $a $b $c $d that I put through a function but I want the function to update them as each of their variables:
- function dothis(&$a, &$b, &$c, &$d) {
-
$a++;
-
$b++;
-
$c++;
-
$d++;
-
}
-
dothis($a, $b, $c, $d);
-
echo($a.$b.$c.$d);
Also do the names used in the function need to be the same as the calling? Or can it be like:
- function dothis(&$a, &$b, &$c, &$d) {
-
$a++;
-
$b++;
-
$c++;
-
$d++;
-
}
-
dothis($f, $g, $h, $i);
-
echo($f.$g.$h.$i);
|
|
May 17th, 2008 03:51 PM
# 7
|
Re: function() inputs and outputs
Problem solved. My post above was correct, so thanks to everyone for replying, but a special thanks for Atli whose solution I used!
|
|
May 17th, 2008 05:36 PM
# 8
|
Re: function() inputs and outputs
Quote:
Also do the names used in the function need to be the same as the calling? Or can it be like:
- function dothis(&$a, &$b, &$c, &$d) {
-
$a++;
-
$b++;
-
$c++;
-
$d++;
-
}
-
dothis($f, $g, $h, $i);
-
echo($f.$g.$h.$i);
|
Good you solved it. As for your last question: no the names that you pass to a function are 'local' to the routine that issues the call.
The names that you declare in the function are local to the function only, so you can name them whatever you like.
Ronald
Not the answer you were looking for? Post your question . . .
189,872 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|