Reliability of callback functions

Game development with Board Game Arena Studio
Post Reply
User avatar
galehar
Posts: 136
Joined: 29 March 2015, 00:12

Reliability of callback functions

Post by galehar »

Hello,
I have a bug in RFTG which I have a hard time reproducing. Here is what's going on.
There is a special action in the game that can be done only once (prestige action). When the player click the prestige action button (called action_phasebonus in the code), there is a confirmation window to warn about "can be done only once!". The callback function of the confirmation dialog hides the button (among other things) with this line of code:

Code: Select all

dojo.style('action_phasebonus', 'display', 'none');
When the player select their action, I check the existence and visibility of the button to decide whether it's a normal action or a prestige action:

Code: Select all

if ($('action_phasebonus')) {
    if (dojo.style('action_phasebonus', 'display') == 'none') {
        bCardBonus = true;
    }
}
Sometimes it doesn't work. The player clicks the prestige action button, confirms, then selects their action and it's a normal action, not a prestige one. I suspect that the callback function is too slow and the player chooses their action before it has been executed. Or maybe it hasn't been executed at all.

So, is there a more reliable way to do this? Is the hiding of the button too slow and I should set a variable instead? Would that changes anything? Or is the timing of callback function generally unreliable? Or am I doing this the wrong way?

I'm thinking of replacing the confirmation dialog by a simple warning message. There's an undo button anyway.
User avatar
joezg
Posts: 70
Joined: 16 June 2011, 17:17

Re: Reliability of callback functions

Post by joezg »

Hi,
First of all, I would never mix the presentation with the logic, so I would never check visibility of the element as a condition for game logic. It is much better to set some state variable on the gamegui class of your game.

The problem shouldn't be with the callback, nor with the slowness of the button hiding. Javascript doesn't have real concurrency so two things in code cannot happen concurrently. JS is one-threaded with event loop. When something happens in browser asynchronously, browser would put new event on event-loop and it would run as soon as JS thread is available. Therefore, first click handler would always completely run before a click handler that happens later. Also I'm pretty sure that this DOM change is synchronous.

I think that the problem could be indeed with checking the dispay style of the button. Maybe there is some state change happening on a server side due to other player action so onUpdateActionButtons is triggered again between a player clicks on prestige action and clicking on action itself. Or maybe some other effect changes. I would try to provoke this bug by clicking on a prestige button with first player and then do a lot of things with the other to see if something changes.

Using state variable could fix the bug, because only this handler changes it. The button in DOM can be changed in a lot of ways.
User avatar
galehar
Posts: 136
Joined: 29 March 2015, 00:12

Re: Reliability of callback functions

Post by galehar »

Thanks for your answer. I've pushed a change to use a property, we'll see if it fixes the bug.
Post Reply

Return to “Developers”