Pointer as parameter for FTimerDelegate

Hi, I’m trying to pass a pointer parameter in a delegate but the engine crash when I try to execute it.
Class name: ABaseCharacter
Here is the code:

/*Declarations in .h file*/
FTimerHandle EnergyRegenerationTimerHandle;
FTimerDelegate EnergyRegenerationDelegate;
/////////////////////////////////////////////////////////////////////
/*Init in BeginPlay()*/
EnergyRegenerationDelegate.BindUFunction(this, FName("UpdateRegenerationTimer"), &(State->CurrentEnergy), &(State->Energy));
	GetWorldTimerManager().SetTimer(EnergyRegenerationTimerHandle, EnergyRegenerationDelegate, 1.0f, true);
///////////////////////////////////////////////
/*Function*/
void ABaseCharacter::UpdateRegenerationTimer(float* current, float* max)
{
	if ((*current) >= (*max)) {
		GetWorldTimerManager().PauseTimer(EnergyRegenerationTimerHandle);
	} else {
		(*current) = FMath::Clamp((*current) + 10.0f, 0.0f, (*max));
	}
	GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, FString::Printf(TEXT("Updated Energy: %f/%f"), (*current), (*max)));
}

The problem is that I can’t use a pointer “*” in the function but the values are going to change and if I pass by reference then only use the initial value (value when I call SetTimer…). Any solution?
Thanks in advance!

You should follow your debug trace to figure what is going on. My guess is that you forgot to mark UpdateRegenerationTimer as UFUNCTION.

Certainly I didn’t mark it. I’ll prove as fast as I can, but before add the parameter I test it without any parameter on the function and it worked properly. I post when I prove the macro. Thanks so much!

Ok, I prove it. This don’t solve the problem, but it give me more info, I can’t use “*” type. I’m going to find other option like TSharedPtr or something else. Thanks a lot!

Can’t you just pass them by the reference and not by the pointer?

I can, but if I pass by reference, when execute only have the initial value, if later the value change, it ignore. The value of these variables are going to change (I need it). I can’t find a way to do it. :S
I edit the question.

First of all check the logs in Saved/Logs of your project directory, if engine crash because it don’t like you doing it should be printed there. If log is cut that means engine crash unexpectedly and probably due to invalid pointers, keep in mind whatever is in State pointer should be alive or else pointers you sending are invalid and definitely can cause a crash. You could also try using direct function pointer insted of using BindUFunction use BindUObject with function pointer:

BindUObject(this, &ABaseCharacter::UpdateRegenerationTimer, &(State->CurrentEnergy), &(State->Energy))

BindUFunction is mainly for binding blueprint functions as you can’t directly point to them in C++, if you do wrong pointing with thing above you will get informed during compilation instead of crashes and errors in engine. Not to mention this way you direcly pointing to function while BindUFunction needs to interpate string you giving in then go to reflection system and search for function with that name.

Now i think you can do without pointers, first of all why you passing pointers in first place when you can use State in the function? What is State? Why you not using it directly? If it’s UObject why you not just pass State all together? It should be a lot safer then just passing float pointers and you can always check validity of object pointer with IsValidLowLevel()

You also can also just use tick, or else you really need 1 sec intervals, you can use delta time to change values over time, it also give you better control over flow of it.

I know state has valid data because I initialize that just before (and print it).
The reason I use pointer and don’t use State directly is because I need 3 FTimerHandle and the 3 call the same method (I really hate repeat code and I consider it a really bad practise), with different parameters. The time is just for test, in the future, the time would be a variable.
I don’t understand well the 3º paragraph(BindUFunction is mainly for binding blueprint functions as you can’t directly point to them in C++, if you do wrong pointing with thing above you will get informed…) could you explain me please??
Thanks for your help!

Well State is in ABaseCharacter right? or else you want to use different states you can already access state in actor and function will be called on correct object (thats why you using “this” in first argument of bind, it a object that function will be called on), if not send State (which already is the pointer) and modify variables from state, it will definitely be more safer then using float pointers. If you mean you want to use it elsewhere with different variables out side of State, then maybe try using interface? Or maybe use common base class for State.

C++ allows you to create function pointer variable which points to function in the code, same as standard pointer it has memory address to function code (in CPU machine code) which you can later call on. UE4 use those to bind functions in to delegates, which i showed you example with BindUObject and this is what you should use in C++. If you make mistake with function name this way compiler will catch the error.

You can’t do that with blueprint functions, because they exist only in virtual machine not CPU machine code and can be only point via reflection system, in order to use function pointer you need to have function in native code (C++). So to make delegates work with blueprints (which is needed for event dispatchers to work, that plugs blueprint functions and events in to delegates in C++) you need to point in to reflection system and thats what BindUFunction is for. BindUFunction will work with C++ functions too because they also exist in reflection system if put UFUNCTION() above them. Since you have your function inside C++, using BindUFunction on C++ function is just wasteful, since it takes more instruction for engine to find function first by name and then call.

Remember ot check logs, we still don’t really know what is the cause of crash

I saw the log and put here, but I solve it, UFUNCTION can’t have pointer “*” and before I put the macro the code compile but crash, probably because of what you say, BindUFunction use blueprint, and the log show me that can’t find the function “UpdateRegenerationTimer”. So, to use pointer, I must use delegate instead BindUFunction is what you say??
State is my PlayerState variable (I have a custom PlayerState) and it has a lot of variables like health, energy, mana, etc. This function is going to be the regeneration, so, with the same function I will regenerate the different attributes (that’s the idea). And I have one FtimerHandle and one FTimerDelegate per attribute. That’s because I need a function that receive some pointers, because the State variables is going to change with time.