Hi team,
I'm having some trouble with spawning child actors.
I have the following code:
export const gameMachine = setup({
actions: {
...
playerConnect: enqueueActions(({ context, enqueue, event, self }) => {
assertEvent(event, EventTypesSchema.enum.PlayerConnect);
if (!self.getSnapshot().children[event.data.playerId]) {
enqueue.spawnChild(playerMachine, {
id: event.data.playerId as never,
input: PlayerSchema.parse({
id: event.data.playerId,
}),
});
}
enqueue.sendTo(event.data.playerId, event);
}),
...
}
})
and I attempt to persist the parent actor like so:
const persistedSnapshot = this.stateMachine.getPersistedSnapshot();
However, I see an error:
Error: An inline child actor cannot be persisted.
I followed this suggestion to convert it from an inlined actor to a referenced actor like so:
export const gameMachine = setup({
actors: {
player: playerMachine
},
actions: {
...
playerConnect: enqueueActions(({ context, enqueue, event, self }) => {
...
enqueue.spawnChild("player", {
...
}),
});
}
}
})
however, now I see a new error:
Actor type 'player' not found in machine 'x:13'.
State machine error Error: Unable to send event to actor '7c8e05d4-9d85-4d33-a0aa-8895f29dd157' from machine 'game'.
it seems the parent machine cannot find "player" even though it is referenced in actors in the setup function. I assume this happens on enqueue.sendTo
Any suggestions?
Thank you