Under Project Settings->Maps and Modes, I set my game mode to a custom blueprint. I did this, so I could have persistent variables. However, I need to access these variables not only in Blueprint, but with C++. What is the best solution? Do I want to create a C++ game mode instead? Can I access those variables via Blueprint then?
Yes. Create a C++ base class for your game mode blueprint, and define your variables on there as editable UProperties instead of in blueprint. Then you can rebase your game mode blueprint to derive from that base class instead and both native and blueprint will be able to access members on the game mode.
class AMyGameMode : public AGameMode
/** Ratio of game time over real time. */
UPROPERTY( Category=World, EditDefaultsOnly, BlueprintReadOnly )
Thank you, that was very helpful. I had a feeling it had something to do with UPROPS.
I’ve set my game mode, and setup a variable as such:
class MYGAMEINSTANCE_API UMyGameInstance : public UGameInstance
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Custom)
After a recompile, I still can not find these variables in my BP. Am I doing something wrong? (These are in my Game Instance header.)
That’s not a game mode, that’s a game instance. In any case, did you reparent your blueprint to the newly created C++ class?
Oops, got my terminology mixed up there. I meant game instance. No, I didn’t reparent my blueprint to a game instance, because some of thing things that need to access the game instance variables are not game instances blueprints.
I don’t see how this changes anything really.
You need to reparent the blueprint anyhow, it’s a simple matter of fact. Without it, the blueprint doesn’t have the information you’re adding in the base class. You instead have two different types of Game Instance containing different information:
UGameInstance <- UMyGameInstance (contains added C++ properties)
UGameInstance <- UBPGameInstance (unrelated, cannot see added C++ properties)
When what you need is:
UGameInstance <- UMyGameInstance (contains added C++ properties) <- UBPGameInstance (child blueprint, can use added C++ properties)
If you need to access these properties outside of the game instance blueprint, then it’s just a matter of access control like any other class. Make public what you want to access, make protected what you only want child classes to access, etc.
That makes sense. I figured I could just directely access them from C++, but reparenting into the blueprint API makes more sense.