Getting a copy of AGameState to modify freely

I’m working on a turn based game and using AGameState quite heavily both for the game state and multiplayer replication.

I’m working on a minimax-ish bot algorithm that tries a number of plays and selects one based on its calculated fitness. This requires me to modify the game state a lot since the bot simulates the play up to the game’s end.

I can’t seem to create a new instance of AGameState without the game crashing however.

NewObject fails with Cast of Package /Engine/Transient to Level failed during a tick.
SpawnActor actually replaces the previous AGameState which isn’t what I want.

Is there a way to get a temporary AGameState?

Short answer: nope. You’ll need to break out any state that needs copying/rollback into a separate struct (or heap-allocated class if you must, but a struct is prefereable, because allocating lots of UObjects in a loop every tick will be quite slow).

I do this in my own game (also for AI-related reasons) and it works fine. As an added bonus, when you don’t have a single monolithic game state, you can have multiple matches going on per server/game world.

I was afraid of that. Glad I’m not far into the project.

Thank you!