Game constructor being called twice

Game development with Board Game Arena Studio
Post Reply
User avatar
junibegood
Posts: 49
Joined: 25 November 2016, 21:12

Game constructor being called twice

Post by junibegood »

For testing purposes, I echoed some variables in the main game constructor (in {gamename}.game.php) and was quite surprised to see my variables displayed twice. At first, I thought I had some duplicate code, but I didn't and I could reproduce this behaviour with a very basic constructor :

Code: Select all

function __construct( )
    {
        echo 'Constructor';
        parent::__construct();
        self::initGameStateLabels([]);
    }
As a result I get : 'ConstructorConstructor';

Is this an expected behaviour or is it a sign that there is something wrong in my project (which I barely started) ?
User avatar
Victoria_La
Posts: 665
Joined: 28 December 2015, 20:55

Re: Game constructor being called twice

Post by Victoria_La »

I don't think there are any gurantee on how many times it will be called. It may create your php class every time there is an action.
I am not sure why it matter? You only can have persisted data in database, you cannot really on anything else.
User avatar
junibegood
Posts: 49
Joined: 25 November 2016, 21:12

Re: Game constructor being called twice

Post by junibegood »

Both calls happen on page load, they are not the results of Ajax queries.

It does not seem to cause any issue directly. But it means at least that whatever is done inside this controller is done twice (which means CPU wasted) and that you get 2 copies of this object and whatever is instanciated inside this constructor (which means RAM wasted). If that is not expected to happen and is not the result of a mistake in my project, it may be a bug in the studio.
User avatar
Victoria_La
Posts: 665
Joined: 28 December 2015, 20:55

Re: Game constructor being called twice

Post by Victoria_La »

You should not be doing anything in your php constructor, so don't worry about it, we not programming for calculator with 2K of RAM running on 2mHz cpu.
Its will not be any sort of visible impact on memory or performance. Just assume object can be created a lot of times.
User avatar
shapeshifter999
Posts: 27
Joined: 18 January 2018, 02:23

Re: Game constructor being called twice

Post by shapeshifter999 »

Ho Ho Junibegood!

Curiosity, I was wondering about the life cycle of the BGA platform: when what is called.

This surprise me. Did you found more information?

Do you know if it occurs twice at the beginning then it never called again?

Thanks!
User avatar
junibegood
Posts: 49
Joined: 25 November 2016, 21:12

Re: Game constructor being called twice

Post by junibegood »

The constructor is also called every time you perform an AJAX call.

And I discovered that it's not only the constructor that is called twice, but whole queries performed entirely multiple times.

I'm stuck with this in the game I'm working on. When a player does something (like moving a piece), I do some server side controls to check it that action is allowed (eg. check if the the target tile is empty) and throw an exception if it's not (which means a red message client side)

I notice that each action is performed a variable amount of times. Sometimes it's only one, but sometimes it's two, three, or even more ! The result is :
  • The first action is performed correctly (I get all the expected behaviours, both server-side and client-side)
  • All following identical actions fail, because whatever the player did is not a legal move anymore (eg. the target tile is no longer empty because the piece already moved). This results in one or more messages like "This move is not allowed" or "It's not your turn"
The exceptions stacks prove that the exact same query is performed multiple times : method calls in the stack are always identical and match whatever action the player performed. At first, I believed my JS code was wrong and I was sending AJAX calls multiple times, but I'm not. I added console logs before every AJAX call and I'm 100% certain I never send any of them more than once.

I have no idea what to do now. I don't want to decrease code quality by removing server side controls (this would probably allow some cheating by sending AJAX calls with a third-party HTTP client instead of using the game GUI), but the game is unplayable right now because you get walls of messages almost everytime you perform an action. Is this something specific to the studio that wouldn't happen on the main server ? How do you guys handle this (if you ever had to) ?
User avatar
fafa-fr
Posts: 383
Joined: 22 December 2013, 21:58

Re: Game constructor being called twice

Post by fafa-fr »

All games do server side control to avoid cheating. I never had any problem like this, all my red error messages appear only once (if there's an actual error), and the PHP/SQL logs display requests only once. And I'm sure it's the same for other devs. So there's probably something wrong in your project, have you done something "exotic"?
User avatar
junibegood
Posts: 49
Joined: 25 November 2016, 21:12

Re: Game constructor being called twice

Post by junibegood »

Thanks for your answer.

No, I don't think i did anything exotic, especially server-side, I much more experienced with PHP than I am with Javascript.

The only thing I can think of that maybe not everyone does is enforcing some constraints directly in the databse model. In the example I used above, the coordinates of a piece on the board are UNIQUE so that it's not possible to have two pieces on the same tile, for example. I do PHP controls before I send the query, though. But all error messages seem I get related to SQL queries, so maybe that's a lead I should follow.
User avatar
Tisaac
Posts: 2743
Joined: 26 August 2014, 21:28

Re: Game constructor being called twice

Post by Tisaac »

Are you sure you didnt bind click event mutliple times ?
What does the network tab show in term of requests ? Only one ?
User avatar
junibegood
Posts: 49
Joined: 25 November 2016, 21:12

Re: Game constructor being called twice

Post by junibegood »

You're right, I do get multiple XHR queries, and I also get multiple instances (the same number) of a query that happens before. Since the notification sent as a result of this previous query is used to trigger event listeners, I suppose the actual problem happens way before it can be seen.

I also discovered that when multiple instances of a same console message happen in a row, Firefox groups them, and displays the number of instances at the other end of the line. It's like 1800 px away and I never noticed it :lol: The whole testing method I used was wrong because of that... It's only when I ran my game on Chrome that I realised it does the same thing, but displays the number next to the message, which is much more convenient !

Thanks for the help, now I know where to look. ;)
Post Reply

Return to “Developers”