I’m new to c++ but this really can’t be normal. I’m trying to create a vector and ran into an issue when adding 3x100 as the vector values…
FVector add_vector;
add_vector.X = float(100);
add_vector.Y = float(100);
add_vector.Z = float(100);
// or
FVector add_vector = FVector(100.0f, 100.0f, 100.0f);
// none of them work! But if I change one 100 it does work!
// works:
add_vector.X = float(100);
add_vector.Y = float(100);
add_vector.Z = float(101);
// works as well:
add_vector.X = float(200);
add_vector.Y = float(200);
add_vector.Z = float(200);
// and so on... 3 x 100 in a vector just doesn't work and creates an empty vector... I tried any variation of adding a hundred, 100.0, 100.000, 100.0f and so on...
Please tell me it’s a bug and I’m not losing my mind :d, would greatly appreciate your help
This works for me.
Can you provide a little more code?
// Btw this is sufficient
add_vector.X = 100.f;
// or
add_vector.X = 100;
I’m trying to guess down below:
void AFoo::AddToVector(FVector add_vector)
{
// Do you have a class variable called add_vector? if so you need to do
this->add_vector.X...
// Instead you are interacting with the argument add_vector
// Like this
this->add_vector += add_vector;
}
// Or are you looking at add_vector in debug mode after it has been released
// example
...
if (true)
{
FVector add_vector;
add_vector.X = float(100);
add_vector.Y = float(100);
add_vector.Z = float(100);
}
bool bHere_Add_Vector_Will_Be_Invalid = true;
...
The bool check seems to work correctly though, since Newtiles is passed as true, it’s inside this if statement 3x100 does not work but any other combination does
float float_x = StartX;
float float_y = StartY;
float float_z = 100.0f;
FVector new_vector = FVector(float_x, float_y, float_z);
// just checking with a new TArray to see if it's a problem with a constant TArray, it's not though
TArray<FVector> BlockArrayTwo = BlockArray;
// BlockArrayTwo.Add(new_vector);
// if i uncomment the above, and change the if(!Contained) check to true, it fires correctly and 3x100 vector is passed
bool Contained = BlockArrayTwo.Contains(new_vector);
// changed Contains to Contained, since I though it might be an illegal variable name
if (!Contained ){
Newtiles = true;
FVector add_vector = FVector(100.0f, 100.0f, 100.0f);
StartArray.Add(add_vector);
}
Still nothing. Someting about the BlockArrayTwo.Contains(new_vector) seems to interact badly? Since if I do:
It does work… And I think I’m using .Contains right, since adding the vector to the array and checking gives a proper true bool. and still doesn’t explain why Contains is properly checked as false (since Newtiles is set correctly).
That fixed it! Moving the boolcheck to a new function and switching .Contains to your suggested:
for (auto Cmp : LocList)
{
if (FVector::Dist(Cmp, Location) < 2.0f)
return false;
}
return true;
got it working. So as you said, perhaps a memory leak due to bad code?
Anyways, thanks a lot for your extra suggestions (yeah it’s a tile based game), it got me thinking about changing some stuff and hopefully cleaning things up. Such a weird issue and would never have figured out the problem without this help.
{
FVector *TestVector = new FVector(0);
FVector *WatchTestVector = TestVector;
// Ok
TestVector->X = 20;
// BAD
TestVector += FMath::RandRange(50, 100);
// Now the TestVector points to an unwanted memory location
// Writing to unkown memory location, can cause crash
TestVector->X = 50;
// VERY BAD, deleting unkown memory
delete TestVector;
// Original TestVector was NOT deleted
}