Question about unreal tutorial ObstacleAssault 3.13

Sorry about the bad title. This honestly isn’t really a question specific to the lecture, rather something that was shown that isn’t the way I usually do things. So essentially I already know c++ for the most part, but didn’t know unreal so I bought this tutorial. However, in the specific lecture he says we have to use a constructor to declare a FVector and then does so like FVector = FVector(values). However, since I already know C++ I kind of was working ahead and declared it the usual c++ way FVector = { values };
I had no problems compiling and my values are editable and look the same as the lecturer’s. So my question is there a difference in the ways we declared this? Is there something different that does indeed happen, or will happen when working with the data later that won’t work the same way? Thanks for your time!

Side note – It is also still allowing me to access the x, y, and z. Meaning it is recognizing it as a struct.
So I tried creating another variable, a float, and then = MyVector.x; And this works fine. Normal syntax also works such as MyFloat = MyVector[0]; This really has me in a stump because I feel like theres a reason he didn’t use regular syntax. But I am much more inclined to use regular syntax if there are no consequences simply because I don’t want to confuse myself when writing in c++ outside of unreal.

There isn’t exactly a “usual” or “normal” way to initialise an object in C++.

Initialization in C++ is Seriously Bonkers

int x = 5;
int x{5}; // added in C++11
int x(5);

are all different forms of initialisation. They are not all equal

std::vector<int> v1(10, 42);
std::vector<int> v2{10, 42};

v1 is a vector of 10 ints with the value of 42.
v2 is a vector of 2 ints with the value of 10 and 42.

demo

A lot of people prefer the traditional way using ()s some prefer the other, there are pros and cons to both. Personally I use {} for when it’s directly initialising the members/arrays, otherwise () for class types, and = for the built-in types (a basic for loop with {}just looks too odd for me).

The problem is that you can’t use () for in-class member initialisers i.e. this code won’t compile

class Foo
{
    std::vector<int> Bar(2);
};

because it looks like the start of a member function declaration, it’s expecting a parameter not an argument after it parses the ( - demo
but you can write it in terms of copy initialisation (=).

Hey, thanks so much for the reply! However, I think you are misunderstanding my question a bit. I know there are different ways to declare things in c++, my question is more about the unreal api he uses in this scenario. FVector isn’t the vector name, rather an unreal api keyword. So technically it is FVector Myvector(this is the variable name) = values. So he uses a what I assume is a class constructer since he says “we have to use a constructor” in the lecture and FVector is a keyword that I’m assuming is just a class or function in unreal api. This is why I’m confused. It seems if he is using a class constructor to declare a vector, or specifically a struct, by calling a constructor. This to me seems like an extra step. So I am just curious to if this constructor FVector() does something outside of just declaration because if not I don’t understand why you wouldn’t just declare the values of your struct like I did (However you’d like out of the examples you gave) instead of specifically calling the FVector constructor. Like I said in my reply below the post, it seems to be recognizing it as a struct and allowing me to do the same things with the data, but I would just hate to get into a habit that hurts me down the road if there are some differences. The reason I am not sure about this is because its Unreal that I don’t know, not C++ (Not that I am some expert, I am most certainly not) but I don’t know the unreal A.P.I. at all. Not to mention before following the previous section of the lecture did I even know how to use the unreal engine at all lol. Thanks again for your help !

So FVector (To my knowledge this isn’t normal c++ syntax, rather a keyword within unreal) so FVector VectorName = FVector(values) ; I guess my confusion is I’m just not sure why this exsist if it doesn’t do something specific outside of decleration since normal c++ syntax already provides ways to declare it.

I think you misunderstood my point. I was using std::vector as an example of a a type where a different constructor gets called based on different syntax that is used; I’m aware they are not the same. Also FVector is not a keyword, just like std::vector isn’t. Unreal adds 0 keywords, it’s all just C++, you aren’t using a custom Unreal specific compiler. You’re using a normal C++ compiler that compiles C++ code.

It is calling a constructor. But he’s using parens and as explained you can’t use for in-class member initialisers.

They both do that. Why he chose to use direct + copy initialisation instead of list initialisation syntax beats me but I explained some reasonings above.


Demo of what’s done - https://godbolt.org/z/6xza3x9nn

1 Like

Ah, I did misunderstand. Okay I get it now, thank you. And sorry for so many questions, but if you happen to not mind one more, you said FVector isn’t a keyword, so what would this be called? Things like UPROPERTY as well. Things specific to custom api library’s. Would these just be considered functions? I imagine when using these there’s somewhere unreal puts code in one of the project files that tells the compiler what these api specific (functions?) do. Same with standard library, as you mention std::vector. I assume these have a name? Words that do something that have code behind them to make it easier to write so we don’t have to write a ton of code just to create a vector. Thanks again for your help. And I apologize for my mistake on misunderstanding what you were saying.

A type or more specifically a user-defined type. Or class/struct (same thing).

That’s a macro and it actually expands to nothing i.e.

UPROPERTY(TheCompilerWontSeeThis)
int32 Bob;

will expand to (as in what the compiler will end up compiling) is just

int32 Bob;

before compilation happens the Unreal Header Tool will run which will parse these macros and generate code in a separate file to also be compiled. You can see them in the Intermediate folder with .gen.cpp and .gen.h file extensions. There’s even a comment in the code saying this
image

No, only functions would be called functions.
https://www.learncpp.com/cpp-tutorial/introduction-to-functions/

May have misunderstood this question; it’s the same with all code including yours.

What in particular? std::vector and FVector are both types; not all things are types. You have types, functions, templates, macros, variables, etc. there’s no name that encompasses them all other than “code” etc…

1 Like

No you answered perfectly, thank you! The header tool info was exactly what I was asking about. I did not know this existed but thought something like it must have. Thanks so much for your help!

1 Like

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

Privacy & Terms