This is a documentation for Board Game Arena: play board games online !

User talk:SwHawk/Create Modular Code

From Board Game Arena
Revision as of 18:45, 14 June 2022 by SwHawk (talk | contribs)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Please sign your comments using this syntax -- ~~~~ so that I may credit your additions to the article.

User names

"Tisaac Human Expert" -> Tisaac

"VictoriaLia" -> VictoriaLa

-- Mogri (talk) 18:08, 14 June 2022 (CEST)

Thanks for the corrections -- SwHawk (talk) 19:45, 14 June 2022 (CEST)

A non-dojo JS approach

Dojo is old and kinda bad. This approach lets you avoid Dojo and this and allows more modern JS.

[X.js]
window.X = {}; // X = your game, or you can use something more generic

...

setup: function (gameData) { // this cannot be an arrow function due to dojo
   X = Object.assign(this, X);
   // the rest of your setup goes here
},
[modules/whatever.js]
X = Object.assign(X, {
  MY_CONSTANT: 1,

  myFunction: (arg1, arg2) => {
    X.someFunction(X.MY_CONSTANT);
    return arg1 + arg2;
  },

  someFunction: (x) => _('The answer is: ') + x,
}

Notes on this approach:

  • Aside from setup and any framework-override functions, you should be using arrow functions
  • Always use X instead of this
  • As usual, you can use any number of modules this way

-- Mogri (talk) 18:08, 14 June 2022 (CEST)

While I agree that most of the components of the framework (be it PHP or JS) are old, there are some advantages to using its loader: dependencies management for instance. Also I'm not sure that changing every function definition to an arrow function offers better readability but that's a personal opinion, as I'm more used to languages where function keywords exists. -- SwHawk (talk) 19:45, 14 June 2022 (CEST)

Highlight lines added in code blocks

The code blocks are rather long right now with all of the comments, however I do think it is worth keeping them so they look like the starter files.

You could consider coloring or emphasising the lines added in some way.

e.g.

[[X_game.php]]
<?php
/**
 *------
 * BGA framework: © Gregory Isabelli <gisabelli@boardgamearena.com> & Emmanuel Colin <ecolin@boardgamearena.com>
 * X implementation : © <Your name here> <Your email address here>
 * 
 * This code has been produced on the BGA studio platform for use on http://boardgamearena.com.
 * See http://en.boardgamearena.com/#!doc/Studio for more information.
 * -----
 * 
 * X.game.php
 *
 * This is the main file for your game logic.
 *
 * In this PHP file, you are going to defines the rules of the game.
 *
 */

require_once 'modules/UtilityFunctionsTrait.php'; //Here we require the trait to be loaded when the class is loaded

use YourProjectNamespace\Modules\UtilityFunctionsTrait; //We import the trait to be used directly as UtilityFunctionsTraits, otherwise we would have to use the Fully qualified  class name (YourProjectNamespace\Modules\UtilityFunctionsTrait)

class YourProjectName extends Table
{
//...

//////////////////////////////////////////////////////////////////////////////
//////////// Utility functions
////////////    

   /*
       In this space, you can put any utility methods useful for your game logic
   */

   use UtilityFunctionsTrait; //Here we actually include the trait inside the class, making all the trait's methods available as class methods, keeping their visibility scope (so a private method is still private)
}
Well thanks for that, I looked into the formatting guide, but I must have overlooked that part, thanks for showing me how to do it! -- SwHawk (talk) 19:45, 14 June 2022 (CEST)