boardgameio / boardgame.io

State Management and Multiplayer Networking for Turn-Based Games
https://boardgame.io
MIT License
9.93k stars 707 forks source link

onBegin and onEnd for Stages #608

Open matthiasseemoo opened 4 years ago

matthiasseemoo commented 4 years ago

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
delucis commented 4 years ago

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.

matthiasseemoo commented 4 years ago

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.

delucis commented 4 years ago

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

senritsu commented 2 years ago

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.