boardgame.io icon indicating copy to clipboard operation
boardgame.io copied to clipboard

onBegin and onEnd for Stages

Open matthiasseemoo opened this issue 4 years ago • 4 comments

I would like to execute code when entering a new stage and when leaving it. To this end, I would like to use onBegin and onEnd in stages. However, those keywords so not seem to have any effect in the stages sections. Would it be possible to add onBegin and onEnd to stages?

Example:

const MyGame = {
  turn: {
    onBegin: BeginTurn,
    stages: {
      discover: {
        onBegin: BeginDiscoverStage,
        onEnd: EndDiscoverStage,
        moves: { DrawCard, GetCoins },
      },
    },
  },
};

boardgame.io version:

npm info boardgame.io version
0.39.4

matthiasseemoo avatar Apr 04 '20 23:04 matthiasseemoo

Please see #561. The current recommendation is to call your BeginDiscoverStage wherever you are currently doing setStage('discover') (or setActivePlayers) and call your EndDiscoverStage wherever you are currently calling endStage if possible.

delucis avatar Apr 05 '20 08:04 delucis

Thank you for your answer, next time, I should also search in the closed issues. I understand that under the circumstances described in #561 onBegin and onEnd can lead to bugs. However, in my case I have multiple stages which also define a "next" element to continue to the next stage when the endStage event is called. Rewriting the endStage function to manually call setStage seems like much more code than simply having the ability to hook onBegin and onEnd of a stage. In my game architecture, I only have a single active player, so there will be no bugs related to multiple players being active at once. At least for those simple use cases, it would be nice to have the two hooks available.

matthiasseemoo avatar Apr 05 '20 09:04 matthiasseemoo

I agree, that sounds like a good use case for these hooks. Maybe it’s something that can be added in the future.

delucis avatar Apr 05 '20 13:04 delucis

Just to mention it, as discussed in #985 it may be also useful for various things to add endIf and the hypothetical-and-not-yet-properly-discussed shouldEnd to stages as well. This would allow functionality similar to current moveLimit (or minMoves/maxMoves in the PR) for all parts of the flow, without being restricted to just use the number of moves players have made.

senritsu avatar Sep 04 '21 17:09 senritsu