# PlayerState persistence across map change

**URL:** <https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366>\
**Category:** World Creation\
**Tags:** playerstate, unreal-engine, CPP, question\
**Created:** [May 15, 2014, 12:34am UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366 "2014-05-15T00:34:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReqPro\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ReqPro\_1](https://forums.unrealengine.com/u/ReqPro_1)\
**Post date:** [May 15, 2014, 12:34am UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/1 "2014-05-15T00:34:32Z")

</div>

So I had some questions about the persistence across map change of PlayerState and talked to JMarkiewicz about it on IRC.  
I’m posting this for posterity in case someone else is looking for the same answers.

Hopefully this quote can help the guys that, like me, were looking for some answers on how to preserve a player’s state across a map change.

---

<div class="post-metadata">

**Author:** ![Crzyhomer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crzyhomer](https://forums.unrealengine.com/u/Crzyhomer)\
**Post date:** [May 27, 2014, 6:10pm UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/2 "2014-05-27T18:10:16Z")

</div>

[20:40] anyone available to answer a few questions about the PlayerState class?  
[20:46] I’m curious as to it’s actual purpose… The way I understand it, it’s meant to hold stuff such as score, health, money, lives, etc  
[20:46] But my question is, does it persist across map changes?  
[20:46] or are we supposed to serialize it to disk and load it again after changing level?  
[20:47] \<+Epic|jmarkiewicz\> good question… promise to post this to answerhub 🙂  
[20:47] \<+Epic|jmarkiewicz\> so the playerstate is the lightweight sibling of the playercontroller. Servers have all the player controllers and all the playerstates… clients have only their player controller but all playerstates  
[20:47] \<+Epic|jmarkiewicz\> helps replication  
[20:47] \<+Epic|jmarkiewicz\> now  
[20:47] \<+Epic|jmarkiewicz\> servertravel comes in two flavors… hard / seamless (as I refer to them)  
[20:48] \<+Epic|jmarkiewicz\> seamless travel will preserve the playerstate but does copy it to a new one on the other side for you.  
[20:48] \<+Epic|jmarkiewicz\> hard travel loses everything.  
[20:48] \<+Epic|jmarkiewicz\> look at HandleSeamlessTravelPlayer  
[20:48] \<+Epic|jmarkiewicz\> so you don’t have to worry about, but you do need to leverage/override CopyProperties  
[20:48] is this the process you’d do in a strictly singleplayer game, too?  
[20:49] \<+Epic|jmarkiewicz\> ::CopyProperties(APlayerState\* PlayerState)  
[20:49] \<+Epic|jmarkiewicz\> _blinks_ single player? hadn’t thought about that in a long time  
[20:49] lol  
[20:49] multiplayer is overrated 😛  
[20:49] \<+Epic|jmarkiewicz\> I think traditionally we change maps by LoadMap (which is internal to both hard/seamless travel processes)  
[20:50] \<+Epic|jmarkiewicz\> in that case you would have to serialize your data  
[20:50] \<+Epic|jmarkiewicz\> but…  
[20:50] \<+Epic|jmarkiewicz\> you can always make a UObject (not an AActor) and AddToRoot before travel, then TObjectIterator find the object on the other side and RemoveFromRoot after restoring what you want from it  
[20:50] \<+Epic|jmarkiewicz\> we are looking at adding a new class that persists across all map travel to make this easier  
[20:51] so AddToRoot on character?  
[20:51] \<+Epic|jmarkiewicz\> AddToRoot is on any UObject, but don’t do this for AActors  
[20:52] okay, thank you  
[20:52] \<+Epic|jmarkiewicz\> there is “streaming levels” which you could theoretically never leave/travel/load in a way that would preserve the PlayerState for the duration  
[20:53] \<+Epic|jmarkiewicz\> I’m not as familiar with that, there is editor setup and map creation stuff I’m not familiar with  
[20:53] yeah but that’d be no good for, let’s say a shoot’em up or a platformer  
[20:53] \<+Epic|jmarkiewicz\> you basically would be loading/unloading pieces within one “map”  
[20:53] \<+Epic|jmarkiewicz\> sure, I guess it depends on your gameplay, level scope, etc  
[20:53] \<+Epic|jmarkiewicz\> if you want load screens, etc  
[20:53] yeah definitely  
[20:54] \<+Epic|jmarkiewicz\> but playerstate was a “network optimization” basically  
[20:54] I think serializing is the way to go  
[20:54] \<+Epic|jmarkiewicz\> and an “organizational” thing  
[20:54] there is no SP only Coop 😛  
[20:55] sp in the case of a “simple” platformer or shoot’em up or the like, you’d recommend putting the player attribues (score, health, etc) in the player controller?  
[20:55] \<+Epic|jmarkiewicz\> Mons is right in a way, you could be “networked, but not advertised” and then travel around as you would networked but never accepting connections  
[20:55] \<+Epic|jmarkiewicz\> you could also change a few things to ensure you reject anyone even trying to connect to your ip :  
[20:55] \<+Epic|jmarkiewicz\> 🙂  
[20:56] \<+Epic|jmarkiewicz\> I would always put all of that in the PlayerState regardless… its just part of the framework and I don’t think going against it here would buy you anything  
[20:56] that was my reasoning as well  
[20:58] thank you for the reply 🙂

---

<div class="post-metadata">

**Author:** ![MiKom](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MiKom](https://forums.unrealengine.com/u/MiKom)\
**Post date:** [July 16, 2014, 9:41am UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/3 "2014-07-16T09:41:37Z")

</div>

Hey,

I’d suggest putting information that you need to override CopyProperties if you want to preserve your data between travels somewhere in the documentation of GameFramework:

[https://docs.unrealengine.com/latest/INT/Gameplay/Framework/QuickReference/index.html](https://docs.unrealengine.com/latest/INT/Gameplay/Framework/QuickReference/index.html)

Also, as I understand, if you want to preserve information for players that reconnect after losing the connection (which is handled by inactive player states) you should also make sure that your properties are copied.

Right now, none of these things is mentioned in the docs.

---

<div class="post-metadata">

**Author:** ![Crzyhomer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crzyhomer](https://forums.unrealengine.com/u/Crzyhomer)\
**Post date:** [July 16, 2014, 3:43pm UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/4 "2014-07-16T15:43:43Z")

</div>

Thanks for identifying that oversight. I’ll see what I can do to get that added.

---

<div class="post-metadata">

**Author:** ![Crzyhomer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crzyhomer](https://forums.unrealengine.com/u/Crzyhomer)\
**Post date:** [July 17, 2014, 6:36pm UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/5 "2014-07-17T18:36:04Z")

</div>

To followup, I think that page is too high level to put your suggestions. I’ll be looking to add it a deeper conversation about travel and object persistence. Either way, this is good feedback and I’ll make sure it’s incorporated.

---

<div class="post-metadata">

**Author:** ![illYay](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@illYay](https://forums.unrealengine.com/u/illYay)\
**Post date:** [August 17, 2014, 6:07pm UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/6 "2014-08-17T18:07:54Z")

</div>

So for persisting inventory is he saying I should create like an InventoryManager class that subclasses from UObject and add that to root?

Or should I just use PlayerState and do seamless travel?

I may not necessarily want PlayerState to contain the inventory though since that’s a bunch of extra info that travels between players.

---

<div class="post-metadata">

**Author:** ![Crzyhomer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crzyhomer](https://forums.unrealengine.com/u/Crzyhomer)\
**Post date:** [September 11, 2014, 3:04pm UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/7 "2014-09-11T15:04:15Z")

</div>

You might want to check out the new GameInstance class. It is designed to persist across level transitions and might be a good place for you to hang data that doesn’t necessarily need replication but does need to last longer than a single map.

Adding to root is always kind of a last resort and a little heavy handed.

---

<div class="post-metadata">

**Author:** ![joessu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@joessu](https://forums.unrealengine.com/u/joessu)\
**Post date:** [February 10, 2016, 5:50am UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/8 "2016-02-10T05:50:41Z")

</div>

is there a doc somewhere on this subject? I am really curious about storing a “player name” as well as weapons and stuff.

---

<div class="post-metadata">

**Author:** ![Crzyhomer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crzyhomer](https://forums.unrealengine.com/u/Crzyhomer)\
**Post date:** [February 11, 2016, 3:59pm UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/9 "2016-02-11T15:59:10Z")

</div>

It looks like there isn’t much documentation specifically on `APlayerState`, but you would do well to read up on [networking and multiplayer](https://docs.unrealengine.com/latest/INT/Gameplay/Networking/index.html).

The player name is already stored on the `APlayerState` class. It is the representation of each player given to every client in the game (along with their `APawn`/`ACharacter`). You would store things like health/score/name/id on this object so everyone can have read only access to it. Only the server can make changes to the object _that transfer to all clients_. If a client (including the owning client) makes changes to the variables on this class it will only exist locally and probably be overwritten the next time it actually changes on the server.

---

<div class="post-metadata">

**Author:** ![Mingah](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mingah](https://forums.unrealengine.com/u/Mingah)\
**Post date:** [July 6, 2019, 9:39am UTC](https://forums.unrealengine.com/t/playerstate-persistence-across-map-change/283366/10 "2019-07-06T09:39:39Z")

</div>

\*\ ***Important to note you cannot use it in the editor because seamless travel does not work in the editor. Your variable cannot be “BlueprintReadWrite”.** And you also need to enable seamless travel and that only works in standalone or packaged build.

```
UPROPERTY(BlueprintReadOnly, Replicated, Category = "Preplan")
Test = "Original Value";  

void AShooterPlayerState::CopyProperties(class APlayerState* PlayerState)
{
	 Super::CopyProperties(PlayerState);

	 if (IsValid(PlayerState))
	 {
		this->Test = "New Value";
		
		 AShooterPlayerState* ShooterPlayerState = Cast<AShooterPlayerState>(PlayerState);        
		 if (ShooterPlayerState)
		 {            
			 ShooterPlayerState->Test = this->Test;            
		 }
	 }
}
```
