Hello. I want to travel between levels without level streaming. I tried Open Level node but I couldn’t figure out Options. I saw some posts say I need to put GameMode URL there but it doesn’t work. Can you help me?
Thank you.
Hello. I want to travel between levels without level streaming. I tried Open Level node but I couldn’t figure out Options. I saw some posts say I need to put GameMode URL there but it doesn’t work. Can you help me?
Thank you.
Thank you for your answer but I am doing this. I want to know how to pass some variables with the character. When I do this, everything gets resetted like you just started to play the game. I want to know what should I do with the Options node to pass all variables on.
You can created a box collision actor to trigger a level change. Do “OnComponentBeginOverlap(Box)” to an “Open Level” node. The level name inside the node is going to be the name of the level you want to open. When you walk into the box it will automatically open the level up.
is there a way to store existing TopDownCharacter’s variables on GameInstance now? Game is kind of close to be finished.
to pass variables between levels, store them inside a GameInstance, or save them in a SaveGame, and load them on the other side.
As long as you create a variable inside the GameInstance and set it from your character’s blueprint, you just have to cast back to it to retrieve the value of it when needed. If you do not have a GameInstance then just create a new one (I called mine GameInstanceX) and go to the Edit>Project Settings>Maps & Modes>GameInstanceClass and change the default to the one you created. These will stay stored here until you shut down the game. If you want them kept longer then you will need to save them the the SaveGame feature.
Do I need to set all variables or is there an easy way?
Sorry for the late reply, you have to set these variables in the game instance from where you control them from. From your event begin play, you need to cast the the game instance from whichever blueprint you need to communicate with it and then create a reference for it. Use the reference to get the variable and set the variable. On the second image I am setting the variable from the blueprint communicating with the game instance (shown on the left). This stores the variable in the game instance. When you need the variable data later you just drag out the game instance reference and “get” the variable you want, and then use it to set a variable in the blueprint communicating with the game instance.
Okay thank you, I will do that but what should I write to ‘Options’ tab in ‘Open Level’ node?
From all the research I did on the Options pin, it is pretty much useless.
So, how I am suppose to transfer variables when level is changed then? lol
First make your variable in the game instance that you want to transfer. Basically inside the Level1 blueprint for example you are setting the variables inside the game instance with the same info as what is inside the level1 variables. Once level2 is loaded it will take the game instance variable and set the variable inside level 2 blueprint. I naved the variables so you can see where they are stored at.
You could use options string that can be passesd while loading levels.
Passing “Value” under variable named “Variable”:
Reading data in new level (must be performed in blueprint, that derives from GameMode class):
This method is pretty limited as passing around complex structures involves serializing and deserializing it from string. Also, passing object references isn’t possible I think. For such cases, storing data temporary seems like better choice, tho I find it pretty ugly.
You would have to set each variable separately.
I am doing it with GameInstance and SaveGame but I can’t do it right. It doesn’t work. I am saving the game with trigger and it prints correct, then I load it, prints correct again but the character spawns with default values not with saved ones.
Still doesn’t work, its driving me crazy.
Are these nodes correct? Can you help me if you know about this?
Spawning will always load default values. That’s normal. You need to fill in the Variables values after spawning. Or you expose them (There is a boolean in the Variables options window) and directly fill them in the Spawn Actor node.
But technically, that’s the same. So filling in the data needs to be handled AFTER you spawn the Character.
Can anyone help?
If you are storing your info in the GameInstance then you need to load them to the GameInstance and then set the TopDownCharacter variable with it. This is the way I would do it In a FPS Example.
I’m going to necro this topic (with the solution) because I find it rather disappointing Unreal doesn’t support this out of the box. So here is a work-around to achieve what you want to do without storing the data and caching it inside the GameInstance but rather send them from one level to another. This is a very basic and simple example, more complex structures are also possible, you can store multiple structs for instance. (C++ Code below)
The whole idea is to:
USTRUCT()
struct FExampleStruct
{
GENERATED_USTRUCT_BODY()
public:
FExampleStruct() : FExampleStruct("Lol", -1) { }
private:
UPROPERTY() FString SomeString;
UPROPERTY() int32 SomeInt;
public:
FORCEINLINE FName GetSomeString() const { return SomeString; }
FORCEINLINE int32 GetSomeInt() const { return SomeInt; }
};
Then somewhere in your code:
FString OutJSONString;
const FExampleStruct ExampleStruct("YourDesiredStringValue", 1337);
FJsonObjectConverter::UStructToJsonObjectString(ExampleStruct, OutJSONString);
const FString Options = FString::Printf(TEXT("ExampleStructKey=%s"), *OutJSONString);
UGameplayStatics::OpenLevel(this, TargetLevelName, true, Options);
Then inside your AGameModeBase put the following code. GameMode’s BeginPlay is the very first thing that gets fired once loaded into a new map, so this is the perfect place to conver the string back into a struct value:
void ACoreGameModeBase::BeginPlay()
{
Super::BeginPlay();
const FString Key_ExampleStruct = "ExampleStructKey";
// OptionsString is a native GameModeBase value, you dont have to define it.
if (UGameplayStatics::HasOption(OptionsString, Key_ExampleStruct ))
{
const FString Value_ExampleStruct = UGameplayStatics::ParseOption(OptionsString, Key_ExampleStruct );
FExampleStruct ExampleStruct;
if (FJsonObjectConverter::JsonObjectStringToUStruct(Value_ExampleStruct, &ExampleStruct))
{
// Do stuff with your values here, cache them for later use for instance.
}
}
}