I am making a game, and came to the part to implement character abilities on the server-side.
So I have an array to hold all character abilities:
$abils = array();
And a class for them:
class Abil{
private $timing;
private $func;
public function __construct($time, $func){
global $abils;
$this->timing = $time;
$this->func = $func;
array_push($abils,$this);
}
public function trigger($time,$params){
if(preg_match('/^'.$this->timing.'$/',$time)) $this->func($params);
}
}
$time is the “timing” of the ability, as RegEx. For example, \d\d-0 would be the start of anyone’s turn.
$func is a function to run when the ability is used.
When a certain “timing” is reached (for example, the start of someone’s turn), a global triggerAll() function would be used to trigger all abilities of that timing:
function triggerAll($time,$params = NULL){
global $abils;
foreach($abils as $abil) $abil->trigger($time,$params);
}
But then, when triggerAll is called, a fatal error is generated:
Fatal error: Call to undefined method Abil::func()
I suspect this is due to $func not being a METHOD but a PROPERTY which holds a function. Any workarounds?
Found a workaround:
call_user_funcinstead of