Page 1 of 1
[Solved] Use class properties
Posted: 23 April 2020, 10:08
by 0BuRner
Hello,
I'm wondering if it's possible to use class properties on the main class of the game, like
$slappingPlayers here ?
It seems not working (variable is reset at every call). Don't know if it's an error in my code or simply not working in BGA.
Code: Select all
class EgyptianRatscrew extends Table
{
private $slappingPlayers = array();
function __construct()
{
Re: Use class properties
Posted: 23 April 2020, 16:14
by apollo1001
I might be misunderstanding you, but it sounds like you just need to put it in the __construct():
Re: Use class properties
Posted: 23 April 2020, 16:20
by 0BuRner
apollo1001 wrote: ↑23 April 2020, 16:14
I might be misunderstanding you, but it sounds like you just need to put it in the __construct():
I already tried it. But whenever I use this variable in different method, it's empty... That's why I'm asking if it's expected or not.
Code: Select all
I have a methods like:
function playCard() {
array_push($this->slappingPlayers, $player_id);
// here with debug I see $this->slappingPlayers has a value
}
and
Code: Select all
function someMethod() {
if (in_array($player_id, $this->slappingPlayers)) {
// always returns false even if debug saw me its in...
}
}
Re: Use class properties
Posted: 23 April 2020, 16:23
by apollo1001
your issue here might be that you've called the method 'checkAction()' which is part of the framework.
Re: Use class properties
Posted: 23 April 2020, 18:05
by RicardoRix
I just checked my code, and all I did was declare the variable in the constructor. I think either way should be possible.
In both function examples it's not clear where local variable $player_id is being set. Did you miss:
$player_id = self::getCurrentPlayerId();
And rather than array_push, just use.
$this->slappingPlayers[] = $player_id;
to add a new element.
https://www.php.net/manual/en/function.array-push.php
Note: If you use array_push() to add one element to the array, it's better to use $array[] = because in that way there is no overhead of calling a function.
Re: Use class properties
Posted: 23 April 2020, 19:13
by 0BuRner
RicardoRix wrote: ↑23 April 2020, 18:05
I just checked my code, and all I did was declare the variable in the constructor. I think either way should be possible.
In both function examples it's not clear where local variable $player_id is being set. Did you miss:
$player_id = self::getCurrentPlayerId();
And rather than array_push, just use.
$this->slappingPlayers[] = $player_id;
to add a new element.
https://www.php.net/manual/en/function.array-push.php
Note: If you use array_push() to add one element to the array, it's better to use $array[] = because in that way there is no overhead of calling a function.
Thanks, I'm not used to PHP, it's probably the solution
