-7

So I'm looking at the Unreal Docs here: https://docs.unrealengine.com/latest/INT/Programming/QuickStart/3/index.html

in step2, they create a float RunningTime in the .h file and use it in the cpp file in step3, which I understand. What I don't understand is in the cpp file, they created FVector NewLocation and float DeltaHeight, but they didn't add those to the .h.

Why is RunningTime in the .h and the rest aren't? What is special about data in the .h files?

thanks

EDIT:

I understand the scope aspect. My apologies for not being clear. My confusion is that RunningTime is not being set anywhere. It's being used in the cpp, in the Tick function but the value is..what? I ran this in the Unreal Engine, and it works, but what is (theValueOf) RunningTime?

に か
  • 119
  • 1
  • 2
  • 10
  • If I interpret that guide correctly, they need `RunningTime` to persist between calls to the function so they added a global variable. They do not need `NewLocation` to persist between calls so they added a local variable. – François Andrieux Feb 21 '17 at 22:51

1 Answers1

0

The purpose of this tutorial you pointed out is to create the class AFloatingActor, which can then be instantiated. The RunningTimevariable is part of the interface of the class and since it is defined as public

public:
    float RunningTime;

it can be accessed outside the class (look at public/private class members and methods).

Opposed to that, the FVector NewLocation and float DeltaHeight are local variables used only in the method AFloatingActor::Tick. Because they are used only in this member (function) and nowhere else in the class neither they are part of the class interface, there is no need for them to be class variables.

If they would be in the .h file (therefore a class variable) then they would be created when the class is instantiated and kept alive as long as the class is. But there is no need for that, therefore they are created when AFloatingActor::Tick is called, they do their part and then get destroyed since they are local variables of that method.

This is also the answer for your second question "What is special about data in the .h files?".

To sum it up: In the .h file of a class you write the class interface. It consist of

  • public variables/methods: this are the variables/methods of this class that are accessible outside the class
  • private variables/methods: this variables/methods are used only in this class, they can not be accessed from outside and are shared with all the methods in this class
  • protected variables/methods: look for example here SO: private vs. protected variables

In the .cpp file of a class the class implementation is written (implementation of each method).

For more information look for example here.

Community
  • 1
  • 1
maetulj
  • 1,002
  • 1
  • 9
  • 15
  • I understood the scope aspect. My apologies for not being clear. My confusion is that RunningTime is not being set anywhere. It's being used in the cpp, in the Tick function, but the value is..what? I ran this in the Unreal Engine, and it works, but what is (theValueOf) RunningTime? – に か Feb 23 '17 at 03:16
  • sorry, I understood it completely different. But the answer for your question is "hidden" in my response. Since the RunningTime is a public member (and can therefore be set from outside the class), it is probably (not sure since I see only the class definition) set in the code that uses this class. Probably in a loop and in each iteration it's value is probably changed. Probably, because I don't know how it is used, but it would be logical in this way. But it needs to be set when initialising the class, since class member default value is undefined and you certainly don't want that... – maetulj Feb 23 '17 at 14:23
  • Thats what I thought afterwards. That RunningTime is being set by another Class somewhere and then is being used in this Tick class. And the way its being used means that the naming would have to be something specific so that it can be found and used by other classes. So, as a test, I tried renaming RunningTime to BlaBloopTime, and...the program still ran correctly. So, i am still confused on how its working. Maybe a fault in the docs? My conclusion is RunningTime's value doesn't matter. Tonight I'll try removing RunningTime everywhere it shows up and see if the cone still bobs. thanks – に か Feb 23 '17 at 19:01