Download

How would I store variables in the game instance for local multiplayer?

I am looking to store unique variables (if said player has an item or not) between levels in a local multiplayer game, and was wondering if there was a more efficient way of doing it than making 2 separate booleans for each item in the game instance i.e. Item 1 for Player 1, and Item 2 for Player 2, and setting them each time a level starts by checking for the player controller’s id.

Doesn’t seem very efficient to me, what better ways would there be around this?

Thanks :slight_smile: !

There are many ways to skin this cat.

The simplest is likely to store an array or set of actors (or objects, or ints) in each PlayerState. If a thing is in that array/set for that player, then that player has that thing.

Slightly fancier would be to have a map from player to array of objects, and store that in game state. This centralizes inventory, but actually makes it harder to figure out who has what. Also, map-of-array is not a thing in blueprint, that’s C++ only.

A third option is to use a bit mask, where bit-set means player-has-thing, and then store the bit mask per player in player state, or store bitmask-per-player in something like an array in the game state.

Note that PlayerState is destroyed and re-created for each level load, BUT you have a hook (CopyProperties()) that lets you copy the properties from old state to new state.

I’m sure there’s more ways to do this, but those are the obvious ones that come to mind

Thank you, will try this in a bit! Is there a way to copy properties between player states without any C++ and just in Blueprints?

Yes:

Not entirely sure what your requirements are but I’m pretty sure a save/load system is an option. You can simply save your data when you finish a level, and then load it when you start a new level.

How this will work with your local multiplayer game depends as you may have to store all the player data within a single game save, then distribute the data accordingly. Alternatively (more complex), you can use the OnlineSubsystem so each player has their own game file (this requires each local player to sign in with some account).

If you don’t want the data to be saved between game sessions, then you can delete the game saves every time the game starts (I.e. reach the main menu). In this case, think of it as a ‘temporary’ save. I have definitely seen games implement this exact behaviour.