[Solved] Use class properties

Game development with Board Game Arena Studio
Post Reply
0BuRner
Posts: 12
Joined: 13 April 2020, 17:00

[Solved] Use class properties

Post 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()
    {
Last edited by 0BuRner on 23 April 2020, 19:13, edited 1 time in total.
User avatar
apollo1001
Posts: 191
Joined: 21 July 2015, 10:41

Re: Use class properties

Post by apollo1001 »

I might be misunderstanding you, but it sounds like you just need to put it in the __construct():

Code: Select all

$this->slappingPlayers = array();
0BuRner
Posts: 12
Joined: 13 April 2020, 17:00

Re: Use class properties

Post 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():

Code: Select all

$this->slappingPlayers = array();
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...
  }
}
Last edited by 0BuRner on 23 April 2020, 16:47, edited 1 time in total.
User avatar
apollo1001
Posts: 191
Joined: 21 July 2015, 10:41

Re: Use class properties

Post by apollo1001 »

your issue here might be that you've called the method 'checkAction()' which is part of the framework.
User avatar
RicardoRix
Posts: 2541
Joined: 29 April 2012, 23:43

Re: Use class properties

Post 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.
0BuRner
Posts: 12
Joined: 13 April 2020, 17:00

Re: Use class properties

Post 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 :)
Post Reply

Return to “Developers”