Alexa.Ki
(Alendromeda.ki)
August 29, 2022, 5:15am
1
Hi, How to cast, I mean to get a reference of the class MyGameInstance
?
this is how I cast to my gamestate:
if (AMyGameState* GS = GetWorld()->GetGameState<AMyGameState>())
{
GS->DoSomething();
}
i want something same to cast to mygameinstance…
Alexa.Ki
(Alendromeda.ki)
August 29, 2022, 5:31am
2
is this coorect?
UMyGameInstance * MyGameInstance_Ref;
UGameInstance* GameInstanceRef = UGameplayStatics::GetGameInstance(this);
MyGameInstance_Ref= Cast<UMyGameInstance >(GameInstanceRef );
if(MyGameInstance_Ref)
{
GameInstanceRef->DoSomething();
}
Tuerer
(Tuerer)
August 29, 2022, 8:59am
4
GetWorld()->GetGameInstance();
Look into the GameplayStatics.cpp
to see what the function does, I’ve told it to you several times already. It takes you just 10 seconds instead of posting here and waiting for an answer for several hours.
2 Likes
Alexa.Ki
(Alendromeda.ki)
August 29, 2022, 5:26pm
5
for example like this? UMyGameInstance * MyGameInstanceRef = GetWorld()->GetGameInstance<UMyGameInstance >();
and I can use MyGameInstanceRef
to access the MyGameInstance
members?
1 Like
zos
(Andrew Tomazos)
August 29, 2022, 9:27pm
6
By the way, what do you actually need a custom game instance for? (Its use cases are somewhat uncommon.)
Alexa.Ki
(Alendromeda.ki)
August 30, 2022, 1:09am
7
I have some member functions in game instance and want to access them.
just doing UMyGameInstance * MyGameInstanceRef;
returns null pointer
Alexa.Ki
(Alendromeda.ki)
August 30, 2022, 1:12am
8
This returns a null pointer.
UMyGameInstance * GS = GetWorld()->GetGameInstance<UMyGameInstance>();
Tuerer
(Tuerer)
August 30, 2022, 7:42am
9
IDK what this syntax GetGameInstance<UMyGameInstance>()
, is it an old-style cast of some sort?
Make sure GetWorld() return something. It can only work when game has started, so no constructor or OnConstruction;
Make sure GetGameInstance() returns something.
Only then Cast<UMyGameInstance>(GetGameInstance())
to see if it casts right.
If p.1-2 work but p.3 does not, maybe you haven’t selected your custom game instance in the project settings?..
1 Like
zos
(Andrew Tomazos)
August 30, 2022, 10:19am
10
It’s called a member function template call:
template< class T >
T* GetGameInstance() const
{
return Cast<T>(GetGameInstance());
}
1 Like
zos
(Andrew Tomazos)
August 30, 2022, 10:20am
11
Where are you calling this function from?
zos
(Andrew Tomazos)
August 30, 2022, 10:22am
12
Why did you put the member functions in game instance and not in another class like game mode or another kind of actor or uobject? What do the functions do?
Alexa.Ki
(Alendromeda.ki)
August 30, 2022, 2:05pm
13
the function is returning some default random values
Hi Alexa,
when you create your game instance you have to make sure that in your project settings, you set the project game instance to the one you just created that way you don’t get a null value each time you call the get game instance.
by default, this will be what your project settings will look like
1 Like