That because you passing to SpawnStones function a vector pointer to local varable of other function. If you don’t know pointer (varable type with *
in you case FVector*
, note that references FVector&
are also work like pointers) contains memory address to real varable and you can read it from pointer. Local varable depending on how compiler compiles the code is either stored in RAM (which also may be CPU Cache) or CPU register (if compiler optimize it that way).
Once function containing local variable is done the local varable is either deallocated or replaced with something else. If it get deallocated you will get crash trying to access it or like in your case if it’s not deallocated you will get a trash data as point where that varable was stored is now used by something else and you reading that data, that data don’t need to be float data that why you get those crazy values.
In other words you accessing so called invalid pointer, most dangerus thing in C/C++ because you can’t verify if set pointer points to something existent. As all your code is turned in to CPU machine code and all variables are turn in to nameless memory addresses, your program or OS can’t determent if pointer is valid or else you track that information, that why you should always null pointers when object storing the pointing variables is destroyed (For example UE4 tracks existence status of all UObjects and all UPROPERTYs referencing to it get nulled, it also possible to use check validity using IsValidLowLevel() function in any UObject). But what you doing is not even unsafe (as it can can crash) it won’t work at all because reasons mentioned above.
You should make private varable inside AStoneEmitterVolume that will hold that VolumeLocation so it will be persistent and exist as long as actor exists.
Design wise, i assume you want SpawnStones to be callable elsewhere thats why you use FVector argument insted of reading vector direcly form component. In such case normaly you should use function overload, one with FVector argument and one without any argument that will read VolumeComponent->GetComponentLocation() and execute SpawnStones(FVector& Location). But function overloads are not supported by reflection system, you can’t have 2 UFUNCTIONs with same name (thats why it rare to find function overloads in UE4 APIs), so alternatively you can make 2 functions with different names if both need to be UFUNCTIONs, effect will be the same. This will allow you to avoid using pointers at all.