Download

Get Blueprint GameMode Reference in C++?

Hi!

I’ve very basic knowledge of working with the blueprints and code together. I am having trouble with making a reference of my Blueprint Game Mode in a C++ class(not a gamemode class).

The question is that i have a Game Mode Blueprint (BP_GameMode) and i need to make a reference of it so that I can use certain functionalities defined in BP, something like this

BP_GameMode* GameModeRef = Cast<BP_GameMode>(GetWorld()->GetAuthGameMode());
GameModeRef->GetSomeValue();

I know that during compile time, compiler won’t know what is BP_GameMode and hence will throw an exception, so, is there a way to bypass this? Or any other way to get a reference to the blueprint gamemode and it’s members?

One other way I can think of is to create a C++ GameMode class, reparent BP_GameMode’s parent to the new C++ GameMode class and use this reference. But, I don’t want to do this :confused: Any suggestions would be great!

Cheers!

TLDR You cant. What you can do is create a C++ GameMode class, and reparent your blueprint as you’ve said. Declare the functions you need in the C++ class. Then you can either code it in C++ right away, or mark it as UFUNCTION(BlueprintImplementableEvent) and code it in blueprint.

1 Like

@STRiFE.x , thanks for your reply!
I was hoping there would be a way :frowning_face:

You can define an interface in c++ and implement it on your BP class, if you don’t want to reparent it.

Anything can be done from C++, you can access any field and call any BP function using reflection system, but it’s much easier to simply use a C++ base class for your game mode.

@RotemS , true, I guess I didn’t mention the reason for not reparenting. I didn’t want to reparent so as to keep C++ minimal for my project.

@BrUnO_XaVIeR , in this case I would actually be interested in knowing how to do it, if it’s possible as you mention. Would it be possible for you to give an example or point me in the direction on what I should do in my case? Thanks!

If you are new to Unreal, don’t fight the engine.

Use the tools they give you until you learn all the rules… Only then you start breaking them and setting your own rules.

This is why Epic usually do not explain this kind of stuff.

@BrUnO_XaVIeR , thanks for the advice! But, I’m really interested in knowing how this can be done! I’d really appreciate any leads :slight_smile:

for (TFieldIterator<UProperty> PropIt(GetClass()); PropIt; ++PropIt)
{
	UProperty* Property = *PropIt;
	// Do something with the property
}

Unreal Property System (Reflection) (unrealengine.com)

Thanks @BrUnO_XaVIeR! :smiley: , I’ll take a look at it.