0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

あるクラスから別クラスを呼び出す方法-忘備録-【UnrealC++】

Posted at

概要

あるクラスから別のクラスを呼び出す方法を忘れてしまうので、知っている範囲でまとめていきます。

・この記事のクラス名は全てSample + クラス名とします。
・引数からキャストする方法は除外することにします。


Characterクラスから別クラスを呼び出す

SampleCharacter.cpp
// GameMode
ASampleGameMode* SampleGameMode = SampleGameMode == nullptr ? GetWorld()->GetAuthGameMode<ASampleGameMode>() : SampleGameMode;

// GameState
ASampleGameState* SampleGameState = Cast<ASampleGameState>(UGameplayStatics::GetGameState(this));

// PlayerState
ASamplePlayerState* SamplePlayerState = SamplePlayerState == nullptr ? GetPlayerState<ASamplePlayerState>() : SamplePlayerState;

// PlayerController
ASamplePlayerController* SamplePlayerController = SamplePlayerController == nullptr ? Cast<ASamplePlayerController>(Controller) : SamplePlayerController;

GameModeクラスから別クラスを呼び出す

SampleGameMode.cpp
// GameState
ASampleGameState* SampleGameState = GetGameState<ASampleGameState>();

// PlayerController (全てのコントローラーを取得しています)
for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; ++It)
	{
		ABSamplePlayerController* SamplePlayer = Cast<ASamplePlayerController>(*It);
	}

GameStateクラスから別クラスを呼び出す

SampleGameState.cpp
// PlayerController
ASamplePlayerController* SamplePlayer = Cast<ASamplePlayerController>(GetWorld()->GetFirstPlayerController());

PlayerStateクラスから別クラスを呼び出す

SamplePlayerState.cpp
// Character
ASampleCharacter* SampleCharacter = SampleCharacter == nullptr ? Cast<ASampleCharacter>(GetPawn()) : SampleCharacter;

// PlayerController
ASampleController* = SampleController == nullptr ? Cast<ASamplePlayerController>(SampleCharacter->Controller) : SampleController;

PlayerControllerクラスから別クラスを呼び出す

SamplePlayerController.cpp
// Character
ASampleCharacter* SampleCharacter = Cast<ASampleCharacter>(GetPawn());

// GameMode
ASampleGameMode* SampleGameMode = SampleGameMode == nullptr ? Cast<ASampleGameMode>(UGameplayStatics::GetGameMode(this)) : SampleGameMode;

// GameState
ASampleGameState* SampleGameState = Cast<ASampleGameState>(UGameplayStatics::GetGameState(this));

// PlayerState
ASamplePlayerState* SamplePlayerState = SamplePlayerState == nullptr ? GetPlayerState<ASamplePlayerState>() : SamplePlayerState;

ほかにも

.cpp
// その他
UGameplayStatics::GetAllActorsOfClass(this, 呼び出したい任意のクラス::StaticClass(), クラスを格納する任意の変数);

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?