No way to create a global reference of the player class?

Hi community, since in every game at some point we will have to access and use the player BP class we are using, i’ve tried to google a solution similar to a Singleton class which stores the variable of that BP so it will be persisted and reachable at any time and occasion. Seems like tho there isn’t a way to do it. It’s incredible that in a programming framework there is no chance to recreate a pattern like a singleton class. The main thing is that seems really bad to cast every time i need to access my player class.
I’ve tried to store the variable in the Game Mode but isn’t updated costantly and seems not persisted at all. I was thinking about the Library but seems that even there isn’t possible to reach that result due the lack of the possibility to store variables outside of a function. Do you know any good way to achieve such a result? Maybe with Python scripting?

1 Like

Casting is not so bad, how often are you really going to need it?

Hey thanks for the reply. Quite a lot anyway. Mostly in combat situations to set some details depending on the particular case. Can’t be handled with collisions and overlapping all the times. I feel the need to have something more light than a cast.

But if you just get it on begin play, you’re done, no?

Also, technically, nothing else should be accessing the variable internal to the player. If you want to keep modularity :wink:

I understand, thank you. Immagine i have a “AreaController” class which takes care to notice if the player is inside that particular area using a Sphere Collision Component and tries to read it’s statistics and level to generate enemies according to it. The strategy i was thinking was to store a BP_Player variables in a thread safe mode like we usually do in the AnimationBlueprint so the AreaController could have fresh info to work on. That way i’ve could separate the moment of info gathering and enemy generating because i’ve noticed that creating Actors seems like a heavy process at run time. If you say the normal flow of casting isn’t so bad i’ll stick to it.

There’s nothing wrong with casting, just don’t do it on tick :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.