There is two type of instances :
– Statics are created in archive settings (override Instance) and don’t change when you change instance.
– Dynamics are created in Instance Setting (Pre-mad instances) and are generated in game.
In this tutorial you’ll see how to use instances in your game. Do not copy what is done in this tutorial because if you need this, you’ll mostly always want persistant instances, that is part of the tutorial about InstanceObjects.
We added an event “on Instance change” because we want to handle case where the door’s level is not loaded and the lever’s level is, at time where instance change.
Currently it is the only main event that is dispatch by the master, we’ll add what you need.
Nothing is changed from previous tutorial if i’m right.
Is a button with some vars we want to store and save, it is the object that represent a savegame. It have :
– The playerName
– A bool storing his brighting state (active saveGame).
– The dateTime at which the saveGame was created first.
It is ISerialisable for visual initialisation (event postDeserialization).
Look at in Pre-Made Instances, we made a special instance called “defaultgame_b5” that “Is ReadOnly” and “Is Content“.
This is an instance that will be exported to our game (see tooltip on “Is Content”) and cannot be modified while in play mode.
Click on serialize all for generate it in editor.
We are using this instance when creating player (DuplicateInstance).
Doing this, we won’t have the problem where :
– We try to access an object with a GetObject node.
– The object isn’t loaded
– The object have no saved state.
The instance “Is StartGame” is set to false because instances are managed in WBP_PlayerList.
In this blueprint we will manage savegames and instances.
First we check if the name is not empty and the instance is not already existing (this node check if instance contain archives).
We duplicate our “defaultgame” instance in the new “nameOfThePlayer” instance and set this player the current player.
We generate all buttons in our map.
We call this function in the event “BeforeSubDeserialization” because at this moment our PlayerList widget is already deserialized and our map (AllPlayers) is at his saved state but with null objects. We need to create objects in this map before to deserialize these.
For handle mobile platforms, and because there is no endPlay event that we are sure will fire, we listen
ApplicationWillEnterBackground event of an Application lifecycle component in BP_ArchiveMaster_B5, this won’t work when launching the game from the editor to your mobile but will work if you build your game.
EndGame is handle in BP_ArchiveMaster_B5 :
– By ApplicationWillEnterBackground for mobile platforms.
– By the event EndPlay for other platforms.
This is a special instance that have a hash of 0, it cannot be save/serialize neither load/deserialize. It is usefull because when you set this instance, you know nothing will be saved or load.
We are using this feature
– At the start of the game (no “Start Game Instance” in Instance Setting)
– When we delete a player, we change to this instance, like this, when the changeCurrentPlayer function is called (right after) and will try to save the current instance, it will just do nothing in the serialize/save part, we don’t need to add a bool to this function for know if player must be saved or not.
– When the current instance is changed, all archives that doesn’t overrideInstance are unloaded.
– Do not include instance inside instance or you’ll have problem (an instance “ins0” and an instance “ins0/sub” is not yet supported).
– If you try to change the current instance to the same instance, nothing will happen, neither the event on instance changed.
– In Pre-Made instances, if you try to delete instance directly from the array (using trash button for exemple), only instances that does not contain archives are deleted. If you need to delete lots of instances, delete these in explorer and then click on the trash.
– A read only instance is not read only in editor, even through Blueprints.
– “SerializeAllArchives” is not the same than “SerializeInstance” because it will serialize archive that override instance too (used in WBP_PlayerList->endPlay).