Download

Losing mind: Can't seem to define FVectors in Pawn::Tick scope

Please see attached screenshot. There really isn’t much more to it than what you see. I’m trying to define local FVectors so I can use them to hold some temp data during the tick, in order to compute the rootcomponent’s current vector.
But I can only seem to define the first FVector and the rest remain undefined…thanks for any insight you might have. I’m assuming it’s something really stupid I’ve overlooked :wink:

just in case
032a931e69ee00629b52a11b8797f3c732033717.jpeg

Since I don’t know anything about your setup, I can only give general advice of course :slight_smile:

But to me this feels like compiler optimization or a VS quirk.
Try wrapping your function in:



#pragma optimize("", off)

void ATwinStickPawn::Tick(float DeltaTime)
{
//Code! ...
}

#pragma optimize("", on)


and then recompiling and doing the same test. You can achieve the same effect project wide by changing your solution configuration to debug.

Let me know if this didn’t help with some more info on your setup and I might be able to help further :slight_smile:

Good luck and best regards,
Temaran

OMG what an awesome trick! Yeah I should have known to trust the compiler and then remember that sometimes the watch window lies in non-debug.

Thanks!

I’m glad it worked out :slight_smile:

Best regards,
Temaran

Just a suggestion, if you’re going to be using Tick, don’t DEFINE a local variable. It’s not very efficient. A tick is ran every Frame, every Frame you’re creating a brand-new variable, that gets destroyed, and then made again next Frame. So if you have 30 Frames Per Second, you’re creating that variable 30 times per second for absolutely no reason. Define it as a global, and then just modify it every Tick. It’d save on some over-head.

Also. FVector Variable = FVector(0,0,0) is kind of silly to me. Why declare that the Variable is nothing if you’re creating it?

FVector Variable; should do exactly the same thing.

You could also compile with debug settings - that should prevent these optimisations and allow all of your code to be traced.

@IrishKilter you guarantee declaring FVector Variable sets everything to 0? I am paranoid about declaring variable without formally initializing them.
Yep, totally understand the performance thing, I’m just setting some new code up so it’s kind of dirty atm.

@AusPaco: that was my key issue, the funny thing is I expected the watch window to say it was optimized away, as it does with a alot of values in unreal engine stepthroughs. But again, bad assumption to make and I always forget the project default is set to DevelopmentEditor

Thanks again everyone!

I’ve never seen a variable created that wasn’t set to 0.

0 = NULL/Nothing/No/False, especially in the computer world. So when you create any variable, it’s always defaults to 0 because there is nothing there.

Integers will be 0, bools will be 0 (which is the same as false), FVector’s, which is just a combo of floats, will have three floats set to 0,etc, etc. Unless told otherwise, the game doesn’t know what your variable is, so just starts off as 0 until modified.

If you really want to be safe, you can set it to 0, but I’d do it in the Constructor. But again, unless modified it should always be 0.

The general take on it from what I understand is you should always set it to zero and initialize it manually. You shouldn’t ever trust the compiler or Constructor to do it for you. VS’s compiler is probably fine for things like this.

AFAIK declaring a variable and not initializing it is considered ‘unsafe’, since you can never guarantee what will be there - even when it’s created. This is especially true for things like Pointers etc.

@IrishKilter

In C++ you need to initialize your variables, they are only set to an equivalent of 0 under certain conditions.

For instance writing int i; only reserves space for an int, that’s why almost every for loop begins with int i = 0; There is something there, because it’s actually a memory address whatever was last there is still there. It does turn out that it’s a bunch of zero’s sometimes.

Because of class constructors and other conditions you may not notice it, but you’ll run into bugs if you keep programming C++ and don’t initialize your variables. It’s very different than C# in this regard. There is no default value for plain data types.

For initing a FVector there is a shortcut.

FVector variable = FVector::ZeroVector;

because an FVector is not a plain data type I’m not certain. A quick search didn’t bring me to something that specified. When I want an FVector to start as zero I initialize it. I don’t bother initializing it to zero when I don’t care what’s it’s value is, as often It’s set right before I use it, but I treat it as dirty before that.

In the case of DanaFo’s code, those FVector’s do not need to be initialized to zero. They are set prior to being read so it makes no difference, they can be garbage prior.

I stand corrected then! Learn something new every day.

Only globals and static variables are automatically initialized with 0 ( ofc if you not initialize it manually ) :slight_smile:

Thanks again for contributing all!