How to cast and return pure reference of MyGameInstance?

How can I pure cast to my game Instance and return it to have a reference of MyGameInstance?

I am doing is I think on a non efficient way.

.h

UFUNCTION(BlueprintCallable, BlueprintPure)
void MyGameInstance_Cast_Pure(UMyGameInstance& RefReturn);

.cpp

void AMyClass::BeginPlay()
{
	Super::BeginPlay();
    UMyGameInstance* MyGameInstanceRef;
    MyGameInstance_Cast_Pure(MyGameInstanceRef);
}
void AMyClass::MyGameInstance_Cast_Pure(UMyGameInstance& RefReturn)
{
	UGameInstance* GameInstance_Ret = UGameplayStatics::GetGameInstance(this);
	if(UMyGameInstance* DyNamic_PureCast_Ret = Cast<UMyGameInstance>(GameInstance_Ret))
	{
		RefReturn = DyNamic_PureCast_Ret;
	}
}

Use pointer type ref when you are global casting, and don’t cast under conditional, try this

void MyGameInstance_Cast_Pure(UMyGameInstance* RefReturn);
UMyGameInstance* MyGameInstanceRef;
void AMyClass::MyGameInstance_Cast_Pure(UMyGameInstance* RefReturn)
{

	UMyGameInstance* DyNamic_PureCast_Ret{};
	UGameInstance* GameInstance_Ret = UGameplayStatics::GetGameInstance(this);
	DyNamic_PureCast_Ret = Cast<UMyGameInstance>(GameInstance_Ret);

	if (DyNamic_PureCast_Ret != nullptr)
	{
		RefReturn = DyNamic_PureCast_Ret;
		//print success
		return;
	}else
	{
		//print failed
		return;
	}
}

hope it helps
cheers!

1 Like

Thank You sir very much , and in begunplay I have to set this global variable ?

MyGameInstance_Cast_Pure(MyGameInstanceRef);

yes you can set it and then use it to access yourgameinstance

hope it helps
cheers!

1 Like

A pure function must have no side effects – this means it should only operate on data that is passed into it as a parameter, should not depend on the status of class or static variables outside the function, it should not alter any data externally to it (such as changing data in an incoming object), it should not print to screen, it should not log, it should not affect anything outside of taking in parameters, performing operations and returning new values.

void AMyClass::MyGameInstance_Cast_Pure(UMyGameInstance* RefReturn)
{
    return Cast<UMyGameInstance>(RefReturn);
}

should be sufficient.

1 Like

Accepted answer is incorrect, beacuse functions operate on copies, not original values, so

static int someVal = 10;
static int otherVal = 20;

void DoPointerThingsWrong(int* SomePtr)
{
  SomePtr = &otherVal;  // no effect outside
}

void DoPointerThingsGood(int** SomePtrAddr)
{
  *SomePtrAddr = &otherVal; // good
}

int main()
{
  int* MyPtr = &someVal;   // MyPtr point to 10
  DoPointerThingsWrong(MyPtr);
  cout << "MyPtr dereference -> " << *MyPtr << '\n';  // still points to 10
  DoPointerThingsGood(&MyPtr);
  cout << "MyPtr dereference -> " << *MyPtr << '\n'; // points to 20
}

@eblade give correct answer (only forgot change return type)

1 Like

Thank You @Emaer @eblade for replying , i accepted the answer only if it works for me and the new information/methods about pointers you explained is amazing and it explain more in details, Thank you very very much for taking time to assist me and helping me in my studies :slightly_smiling_face:

oops yeah i just copied the function signature my bad. Also, I love pure functions. :slight_smile:

1 Like