boardgame.io
boardgame.io copied to clipboard
onBegin and onEnd for Stages
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
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.
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.
I agree, that sounds like a good use case for these hooks. Maybe it’s something that can be added in the future.
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.