Hi guys! I have a problem ! I create a big map with some houses, but I want to enter house as another level,and come out to the main level again! just like the game The Elder Scrolls did ! I want the Character press “f” to get in the house (another level ) when he stand infront of the ouside door, And I want him press “f” to get back to the main level when he stand infront of the inside door,and when he get back to the main level , he will stand infront of the door whichhe just het in . I beg someone to help , because it is really important to me.
some error words… “he will stand infront of the door which he just get in…”…sorry for my English…
Hi! No problem for your english, the question is clear And the answer is pretty simply but a little tricky at the begin I’ll try to explain you both the problem and the anser
When you change level you lose any information about leaved level since it’s removed form the memory. So, when you leave the house the game don’t know where it have to spawn the char in the world map! Also if the house have multiple entrance the game don’t know where to spawn you char in the new map!
The question is: how can I store information between levels?
The answer is: GameInstance Class!
This class persist during all game and you can only have one, spawned at the start of your game and destroyed when it end. Obviosuly it don’t contains any variable you can use to store data since it can’t foresee all the possible variables for every possible game! How to fix it? You can create a custom game instance class (create new BP that inherit from game instance) and fill it with all the variable you need to store between levels, you can also put events or function you can call whenever you want.
Ok, make it more practical, I’ll use a summary (because I like it lol):
-
Create a new game instance class, is like create a new blueprint but you have to create it looking for game instance in the class list.
-
Set your new game instance into project settings: Go to Edit/Project Settings/ Maps and modes and replace the last parameter (Game Instance class) with your new Game instance.
-
Create a new transform variable in your game instance called something like “WorldMapLocationLastTravel” and set the default value as the transform (location, rotation and scale) of your playerstart actor. [in this way the first time you spawn you’ll spawn at that location]
-
In the event begin play of your world map level blueprint you have to CAST your game instance to get the right data. So create the “GetGameInstance” node that will return the game instance reference and connect it to "Cast to name of your game instance " node. Now you have access to the all the data you stored into the game instance. Get the transform you saved before (kinda WorldMapLocationLastTravel) and set your char transform to it. The order is:
Event begin play → Cast to NameOfYourGameInstance(GetGameInstance) → SetActorTransform(WorldMapLocationLastTravel). -
Every time you travel from your world map to an house instead of GET your WorldMapLocationLastTravel, SET it to your current transform before open level.
Now when you leave the house the level blueprint will get the variable of your last position and will move the character!
If you have houses with multiple entrances just add an integer variable into the game instance. Any entrance must have a different number you need to change before you travel. Then in the level blueprint of your house you can get that number to make your character spawn in different locations depending on the value of the variable.
If you have any question feel free to ask
Really thanks !!! I will try it and come back !!!
Is it like this ?? The Transform variable create in the Blueprint (Gameinstance class)??
I am really a fresh man in blueprint…I major in Composition and Visual art…
For the level blueprint I’d say you are close. In your level BP you still need to get a reference to the actor you want to set the transform for and tell the set transform node where to place the character (WorldMapLocationLastTravel).
Alright!!! I’m going to try!! thanks !!!