growing yourgamename.game.php

Game development with Board Game Arena Studio
User avatar
Sydnique
Posts: 11
Joined: 26 July 2020, 21:38

growing yourgamename.game.php

Post by Sydnique »

Hey everybody,

as I'm working on a first (example) game, I keep adding more and more code to the "Main game logic: yourgamename.game.php" file.

Is there a recommended way to move some of that code out to another File and or Class?

There's a guys project on Github who seems to have figured it out: https://github.com/danielholmes/battle- ... ForHill218
But so far I had no luck trying to copy that approach, and I don't want to complicate things too much.

How are you guys doing it, are you just putting everything in one class or splitting it up somehow?
User avatar
Draasill
Posts: 197
Joined: 26 April 2020, 00:00

Re: growing yourgamename.game.php

Post by Draasill »

It depends a lot on the game's complexity, IMHO.

If you like OOP, it's good to have classes representing your board, your players, your cards...

So you can do things like $card->applyEffectOnPlayer($player).

The "modules" directory exists for this.
User avatar
Sydnique
Posts: 11
Joined: 26 July 2020, 21:38

Re: growing yourgamename.game.php

Post by Sydnique »

got it to work

Things to remember:
  • add new class file under src/Yourgamename/YourNewClass.php
  • reference your new class in yourgamename.game.php:

    Code: Select all

    use Yourgamename\YourNewClass;
    (put that directly below the require statement near the top of the file)
Then, to make phpunit work:
  • configure autoload in composer.json

    Code: Select all

        "autoload": {
            "psr-4": {
                "Yourgamename\\": "src/Yourgamename"
            }
        }
  • don't forget to run composer install after adding or changing the autoload config
And to get it to work in production:
  • add src path to extraSrc in bgaconfig.yml

    Code: Select all

    extraSrc:
      - src/Yourgamename
    
    (Note: This will include the contents YourNewClass.php in the deployed version of Yourgamename.game.php)
User avatar
Sydnique
Posts: 11
Joined: 26 July 2020, 21:38

Re: growing yourgamename.game.php

Post by Sydnique »

Draasill wrote: 13 August 2020, 11:03 It depends a lot on the game's complexity, IMHO.

If you like OOP, it's good to have classes representing your board, your players, your cards...

So you can do things like $card->applyEffectOnPlayer($player).

The "modules" directory exists for this.
Thanks for the hint, I think if one wouldn't use composer/phpunit, your approach would definetely be more elegant.
User avatar
RicardoRix
Posts: 2541
Joined: 29 April 2012, 23:43

Re: growing yourgamename.game.php

Post by RicardoRix »

BGA may suggest you use the modules directory exclusively, and prepend all filenames with a unique identifier. For the example below, it's EU :

You can do something like this:

Code: Select all

require_once('modules/EU_DBTrains.php');
....
$this->trains = new EU_Trains($this);		


You may like to extend from APP_GameClass, and pass the 'game' variable in the constructor.

Code: Select all

require_once('EU_DBTables.php');
  
class EU_Trains extends APP_GameClass {

    function __construct($game) 
	{
		$this->game = $game;
	}
User avatar
Sydnique
Posts: 11
Joined: 26 July 2020, 21:38

Re: growing yourgamename.game.php

Post by Sydnique »

@RicardoRix

Thanks, thats good to know.

Might be useful in the BGA Studio doc :-)
User avatar
Draasill
Posts: 197
Joined: 26 April 2020, 00:00

Re: growing yourgamename.game.php

Post by Draasill »

Sydnique wrote: 13 August 2020, 11:16 Thanks for the hint, I think if one wouldn't use composer/phpunit, your approach would definetely be more elegant.
Yeah, the autoload + namespaces is better, too, I think !
User avatar
Tisaac
Posts: 2736
Joined: 26 August 2014, 21:28

Re: growing yourgamename.game.php

Post by Tisaac »

Draasill wrote: 13 August 2020, 14:36
Sydnique wrote: 13 August 2020, 11:16 Thanks for the hint, I think if one wouldn't use composer/phpunit, your approach would definetely be more elegant.
Yeah, the autoload + namespaces is better, too, I think !
Are you sure it works in production though ?
I already run in some troubles making sub-folder of modules/ dir working when deploying, I'm not sure if that would works flawlessly. Let us know if you have some advices on it.

I personnaly use lot of subclasses in modules with some generic ones : Utils (for thing that fits nowhere), playerManager, player, logManager, and more recently the very useful NotificationManager. I also use a lot of inheritance to handle the game's rule and make the code easier to read/maintain : one class for the Card, and then one children for each "type" of card that can overwrite the function (for instance in Santorini, almost every powers' abilities are encoded in its own class, that also stores the info about the power (name/desc/...) instead of having a huge material.inc.php file).
User avatar
Draasill
Posts: 197
Joined: 26 April 2020, 00:00

Re: growing yourgamename.game.php

Post by Draasill »

Some games are using it, yeah.

Remember there are some inspiration on https://github.com/topics/boardgamearena !
User avatar
Sydnique
Posts: 11
Joined: 26 July 2020, 21:38

Re: growing yourgamename.game.php

Post by Sydnique »

Draasill wrote: 13 August 2020, 17:12 Some games are using it, yeah.

Remember there are some inspiration on https://github.com/topics/boardgamearena !
awesome - I didn't know about this list
Post Reply

Return to “Developers”