This is a very simple problem however for whatever reason, I cannot get this to work. Whenever I pass an argument to my function it doesn’t get updated.
Num1 = 3.0f;
UE_LOG(LogTemp, Log, TEXT("C_HealthPickup: %f"), Num1);
void AC_HealthPickup::P(float Number)
Number += 5.0f;
I should also add that the output is always 3
Hi! The thing is that you pass param by Value. Roughly speaking, function copy argument in it’s stack frame and works with this local variable. What you need to do is - to pass param by Reference or by Pointer. To do so, just update you function declaration and implementation
to this (by Reference)
void AC_HealthPickup::P(float& Number)
or to this (by Pointer)
void AC_HealthPickup::P(float Number)*
For more details just take a look at
Ah, that makes sense. Thank you so much. But in which case should I use a pointer over a reference or the other way round. Can you also make this an answer so I can mark the question as answered. Thanks again.
For function parameters - use reference most time, when you can. You dont need to care about nullptr in this case, so reference are way more safe. And only if you cant use reference - use pointer. But, of course, there are other rules for simple and effective C++ code that are more important. For example, try to minimize header file , that is included in many other headers. This make total build size smaller. Another example - to avoid circular dependencies between headers you can use pointers and forward declarations.