I'm writing an NPC generator for use with D&D 3.5, and I've gotten the kinds of results I want by "randomly" sampling various PHP arrays using
print_r(array_rand($a,1));
The whole bit which generates an NPC is of that style, with arrays labeled $a through $e. A result from my code comes out like this (array labels added for a little clarification)
($a)Half-Orc ($b)male: ($c)Young, ($d)with a relaxed, peaceful disposition
($e)and some items that need to be fetched/located.
My problem is that I can't figure out exactly how to make a block of these statements into a function which can be called. I've tried using
function Generator(){
print_r(array_rand($a,1));
print_r(array_rand($b,1));
print_r(array_rand($c,1));
print_r(array_rand($d,1));
print_r(array_rand($e,1));
}
But it's gotten me nowhere when I try to call it using
Generator();
Am I just being stupid, or is there something I'm seriously missing?
A function/method consists of the statements inside the braces (in this case none) What you probably mean to do is put the print_r
statements inside the function;
function Generator(){
print_r(array_rand($a,1));
print_r(array_rand($b,1));
print_r(array_rand($c,1));
print_r(array_rand($d,1));
print_r(array_rand($e,1));
}
Also, unless $a
- $e
are global variables, you'll want to pass them in as parameters;
function Generator($a, $b, $c, $d, $e){
print_r(array_rand($a,1));
print_r(array_rand($b,1));
print_r(array_rand($c,1));
print_r(array_rand($d,1));
print_r(array_rand($e,1));
}
...and call it using;
Generator($a, $b, $c, $d, $e);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.