Alexa.Ki
(Alendromeda.ki)
August 19, 2022, 4:36pm
1
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;
}
}
c0r37py
(c0r37py::)
August 19, 2022, 5:47pm
2
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
Alexa.Ki
(Alendromeda.ki)
August 19, 2022, 5:50pm
3
Thank You sir very much , and in begunplay I have to set this global variable ?
MyGameInstance_Cast_Pure(MyGameInstanceRef);
c0r37py
(c0r37py::)
August 19, 2022, 5:52pm
4
yes you can set it and then use it to access yourgameinstance
hope it helps
cheers!
1 Like
eblade
(eblade)
August 20, 2022, 1:37pm
5
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
Emaer
(Emaer)
August 20, 2022, 3:10pm
6
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
Alexa.Ki
(Alendromeda.ki)
August 20, 2022, 4:49pm
7
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
eblade
(eblade)
August 21, 2022, 4:37am
8
oops yeah i just copied the function signature my bad. Also, I love pure functions.
1 Like