Using namespaces in games projects

Game development with Board Game Arena Studio
Post Reply
User avatar
thoun
Posts: 1620
Joined: 10 December 2020, 22:25

Using namespaces in games projects

Post by thoun »

You can now write your game class in a new way, using namespces and taking benefit of PSR-4!
From now on, the new project will generate a Game.php file in modules/php, with a namespaced class, and will be able to load your other classes in this directory using PSR-4 (without needing to manually write `require_once`).

The new Game.php files looks like this:

Code: Select all

declare(strict_types=1);

namespace Bga\Games\YourGameName;

require_once( APP_GAMEMODULE_PATH.'module/table/table.game.php' );

class Game extends \Table {
  ...    
To take advantage of the autoload for your other classes, just follow PSR-4. For those not familiar with it, I've added some examples here : https://en.doc.boardgamearena.com/Main_ ... er_classes

BGA will try to load a `Bga\Games\YourGameName\Game` class in `modules/php/Game.php`.
If it isn't found, it will fallback to the "legacy way" of loading `YourGameName` class in `yourgamename.game.php`, so your already started projects and older games still work (but PSR-4 autoload won't be activated in this case).
For your new projects, please use it (especially if you copy one project to the other instead of starting from the blank template for each new project).

(If you convert a "legacy" project to the new way, you will need to add `\` before exception classes so `BgaUserException` becomes `\BgaUserException`)

The Reversi project & tutorial has been updated in that way, so you can use it as an example
User avatar
zetaLeonis
Posts: 14
Joined: 08 September 2024, 05:42

Re: Using namespaces in games projects

Post by zetaLeonis »

Newbie question: Is the BGA Type Safe Template (https://en.doc.boardgamearena.com/BGA_T ... e_Template) unaffected by this change? or it will be updated too?

I'm asking because I started coding a project around ~15 days ago and I'm a place I can switch to a new project and adapt the code, but I was starting to feel confortable with the BGA Type Safe Template and I don't want to lose those IDE-friendly features.
User avatar
thoun
Posts: 1620
Joined: 10 December 2020, 22:25

Re: Using namespaces in games projects

Post by thoun »

This wiki page was not done by bga and is not maintained by bga, so I can't answer you. You can ask the writer of this page, and I hope they can make it compliant with it soon.
User avatar
zetaLeonis
Posts: 14
Joined: 08 September 2024, 05:42

Re: Using namespaces in games projects

Post by zetaLeonis »

Oh, right. So sorry!. Newbie mistake.
User avatar
thoun
Posts: 1620
Joined: 10 December 2020, 22:25

Re: Using namespaces in games projects

Post by thoun »

Don't worry, it's almost impossible to know if wiki pages were done by BGA admins or contributed by other game devs ;)
Post Reply

Return to “Developers”