I’m creating a game in Unreal Engine 4 and I am only using C++, not the blueprints that are given. I want to be able to access a variable from my ProjectCharacter class in my projectHUD class, however, the only tutorials or help I can find is with blueprints. The syntax between C++ and UE4 seems to be a little off for accessing variables between classes. Could someone explain in code how I can do this?
Just to clarify, in the character class we give the player a certain thruster variable that controls the level of the thruster. I want to access this variable in my HUD class so I can display a bar that uses the thruster variable and a scale to display the level of the thruster on the screen. Please let me know if I need to clear up my question more.
Lets assume you have a class A and class B
To make a variable MyVar inside class A available in calss B, you have the following methods:
Make the variable public (eg: public float MyVar;)
Write getter and setter methods inside classA. Ie you will write two public functions named getMyVar and setMyVar. getMyVar will return the myVar varibale while setMyVar will accept a value as a parameter and set it to myVar. You can keep variable myVar as protected or private.
Define classB as a friend of classA. Once you do that all functions in classB will be able to access the variables of classA. Use this only if your require it. (here is more on it Friendship and inheritance - C++ Tutorials)
In your case I would suggest go with method 2 and write a getter function