タイトル: Blueprintで現在開いているレベルが特定のレベルであるか調べる方法
SEOタイトル: UE Blueprint 現在のレベル判定完全ガイド
| この記事の要点 |
|
現在のレベル名を取得する
Blueprint で「今どのレベルが開いているか」を判定する基本は Get Current Level Name ノードです。これは C++ の UGameplayStatics::GetCurrentLevelName のラッパで、永続レベル (Persistent Level) の名前を返します。
最もシンプルな実装
[Event BeginPlay]
──→ [Get Current Level Name]
Remove Prefix String = true (推奨)
──→ String の出力
──→ [Equal (String)] と比較
A = (level name)
B = "MainLevel"
Return → Branch
True → Print "MainLevel です"
False → Print "別のレベル"
Get Current Level Name のオプション
- Remove Prefix String (デフォルト true): PIE 時の
UEDPIE_0_プレフィックスを自動で除去。これを有効にしないと PIE と本番で名前が変わる - 戻り値の型は
String。Name型ではない
PIE vs パッケージング後の違い
UE では Play In Editor 中、レベル名に自動でプレフィックスが付きます:
| 実行モード | 取得される名前 |
|---|---|
| PIE (Server) | UEDPIE_0_MainLevel |
| PIE (Client) | UEDPIE_1_MainLevel |
| Standalone | MainLevel |
| Packaged | MainLevel |
Remove Prefix String = true でこの違いを吸収できます。
Soft Object Reference で型安全に
文字列比較はタイプミスに弱い。Soft Object Reference を使うと、エディタでレベルを選択でき、リネームにも追従できます:
[Variable]
Name = TargetLevel
Type = Soft Object Reference → World
[Branch]
Condition:
Get → Get Current Level Soft Object Path (UE 5.x)
Equal? TargetLevel
# もしくは Soft Reference を文字列に変換して比較
[Get Path Name (Soft Object Reference)]
→ "/Game/Maps/MainLevel.MainLevel"
[Get Asset Name]
→ "MainLevel"
複数のレベル判定 (Switch on String)
[Get Current Level Name (Remove Prefix=true)]
──→ [Switch on String]
Selection = (level name)
Pins:
"MainLevel" → 処理 A
"BattleLevel" → 処理 B
"TitleLevel" → 処理 C
Default → エラー処理
Blueprint Function Library にまとめる
「現在のレベルが X か?」を頻繁に判定するなら、Blueprint Function Library に純粋関数として切り出すと再利用しやすくなります。
- Content Browser → 右クリック → Blueprints → Blueprint Function Library
- 名前を
BFL_LevelHelper等に - 関数
IsCurrentLevel(LevelName: Name) → boolを作成 - 内部で Get Current Level Name → 比較 → 真偽を返す
- BeginPlay 等から
IsCurrentLevel("MainLevel")で呼び出せる
[Function: IsCurrentLevel]
Input: LevelName (Name)
[Get Current Level Name (Remove Prefix=true)]
──→ [Name → String]
──→ [Equal (String, Case Insensitive)]
A = currentName
B = LevelName (String 変換)
──→ Return (bool)
ストリーミングレベルの場合
Level Streaming (永続レベルにサブレベルをロード) を使っている場合、Get Current Level Name は永続レベル名を返します。ロード済みのサブレベルを判定したい場合は別の方法が必要です:
[Get Streaming Level (by Object Reference)]
Level = (Streaming Level Reference)
[Is Level Loaded] / [Is Level Visible]
→ bool
# サブレベルをすべて列挙
[Get World] → [Get Streaming Levels]
→ Array of Level Streaming
ループで .GetLevelName() を比較
Level Transition でロード完了を検知
レベル切替直後に処理したいなら、OnLevelLoaded イベントが便利です:
[Get All Actors of Class] (GameInstance or GameMode で)
--- Open Level (by Name) して切替 ---
# GameInstance の Event Graph で
[Event Init]
Bind to → OnLevelLoaded
[Event OnLevelLoaded]
──→ [Get Current Level Name]
──→ 判定処理
C++ から呼ぶ場合の対応
#include "Kernel/GameplayStatics.h"
void AMyActor::BeginPlay()
{
Super::BeginPlay();
FString CurrentLevel = UGameplayStatics::GetCurrentLevelName(this, true);
// 第二引数 RemovePIEPrefix = true
if (CurrentLevel == TEXT("MainLevel"))
{
UE_LOG(LogTemp, Log, TEXT("MainLevel detected"));
}
}
// または Level Soft Reference で
TSoftObjectPtr TargetLevel;
FString CurrentPath = GetWorld()->GetMapName();
CurrentPath.RemoveFromStart(TEXT("UEDPIE_0_"));
FAQ
Q: PIE では一致するのに、パッケージング後だけ一致しない
A: Remove Prefix String が false になっている可能性。または比較対象の文字列が PIE 用にプレフィックス付きでハードコードされていないか確認。大文字小文字や末尾スペースも疑う。
Q: World Composition / World Partition を使っているとき
A: World Partition では永続レベルの概念が変わるため、レベル全体名の概念が薄れます。代わりに UWorldPartitionSubsystem や Data Layer 単位の判定を使用。
Q: GetCurrentLevelName と GetMapName の違い
A: GetCurrentLevelName はレベル名 (ファイル名相当)、GetMapName は World 全体のパス的な名前。PIE プレフィックス除去の有無や、サブレベル時の挙動が異なります。多くの場面では GetCurrentLevelName で十分。