Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How do I access a variable from another class without using blueprints?

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.

Product Version: Not Selected
more ▼

asked Jan 20 '15 at 07:21 PM in C++ Programming

avatar image

1 1 1 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Several ways: 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:

  1. Make the variable public (eg: public float MyVar;)

  2. 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.

  3. 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 http://www.cplusplus.com/doc/tutorial/inheritance/)

In your case I would suggest go with method 2 and write a getter function

more ▼

answered Jan 21 '15 at 02:58 AM

avatar image

3.4k 154 51 271

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question