Having trouble subscribing to event

So I have an event tickMinute in my world logic class, and I’m trying to set it up so whenever that event broadcasts, functions in other actors execute:

DECLARE_EVENT(AWorld, FTimePasses)
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYGAME_API AWorld : public AActor{

	FTimePasses tickMinute;

//external actor component UConsumable:

void UConsumable::RegisterWithClock(){
	AWorld* world;
	for (TActorIterator<AWorld> ActorItr(GetWorld()); ActorItr; ++ActorItr)
		world = *ActorItr;
	if (world != nullptr){ //Get reference to the actor whose event we want to subscribe to
		world->tickMinute.Add(this, &UConsumable::procConsumable); //set procConsumable to fire every time tickMinute broadcasts

The last line is what I’m having trouble with- I’m trying to run Add with the memory address of the function I want to subscribe to the event, but it won’t compile, instead producing an error, “cannot convert from ‘void (__cdecl UConsumable::* )(void)’ to ‘const FName’”. I know that means my argument is wrong, but it seems weird that it would want an FName instead of a function reference, is .Add() the wrong thing to use when I’m subscribing to events?

If UConsumable is a UObject you should use AddUObject, and the first parameter should be the UObject instance on which to call the method given in the second parameter, so:

world->tickMinute.AddUObject(consumableComponent, &UConsumable::procConsumable);

Also, it’s probably not a great idea to call your class AWorld since there’s already a built-in UWorld class.

Ooh, that does it, thank you!! :slight_smile: The AWorld thing is easy enough to fix… does Remove use different syntax, though? Because I tried to write the unsubscribe function with the same syntax, to wit:
tickMinute.Remove(this, &UConsumable::procConsumable);

However, it wants something else, as the compiler claims “Constructor overload resolution was ambiguous”.

I think you’re supposed to do:


Ooh that does it, thank you again! :slight_smile: