How to listen to delegate

For you delegate setup:

RegisterActorDelegate.AddDynamic(ObjectToCallTo,&AClassToCallTo::FunctionToCallTo);

Argument names tells what you need to do ;] There also other binding functions depending on what you trying to bind, you can also bind delegate to delegate:

https://api.unrealengine.com/INT/API/Runtime/Core/Delegates/TBaseDelegate/index.html

In case of multicast Bind functions are called Add

I want that when a actor gets added to the level, that it tells a class in the level that it just entered the level, so that that class can then add the actor to a array. I’m a bit confused on how I go about having that class listen to the delegate. I have everything setup correctly (I think) but I’m lost on how to proceed further.

280985-81.png

what if I don’t know the object? I only know the class. the setup I want, is that the level has a MasterActor that sits there and listens to the delegate. the building enters the level and just yells “I’m here, and I’m a building”. the MasterActor hears that and adds the building to an array. So the building wouldn’t know the actor. And the masterclass wouldn’t know the actor until it hears the call.

In that case you should find the master actor from your building. There is no way to listen to a delegate without subscribing to it, and for that you need to know the object that will broadcast it.
I would do it something like this:

void InteractableActor::RegisterBuilding()
{
        for(TActorIterator<AYourMasterActorClass> Itr(()); Itr; ++Itr)
        {
            AYourMasterActorClass* MasterActor = *Itr;
            if(MasterActor != nullptr)
            {
                  //Create a function that can be called by the buildings being created
                  MasterActor->RegisterBuilding(this);
                  return; //end the execution of the function since we only needed this.
            }
        }
        //Probably shoot out an error if the master actor was not found when attempting to register.
}

Do I understand that correctly that you ditch the delegate and directly cast to the MasterActor? Is there a more elegant way? because the entire reason of me doing this was to not have to iterate through the actors in the level.

what if I instead use the playercharacter, since that one will always be in the level. However it doesn’t seem to like the AddDynamic. I guess I’m missing a include file, but I haven’t been able to figure out which one

280999-86.png

Yes, I proposed that solution since delegates are not universally listened to, they need to be referenced. Using an intermediate actor like the player character, like you propose, might be a good compromise.

AddDynamic is a macro so visual studio will try to make you use the function instead. Simply write RegisterActorDelegate.AddDynamic(character, ATopDownCharacter::Register);

Hope this helps.

still throws the same error even if I type AddDynamic. is there something I’m missing? the error is as followed.

no instance of function "FRegisterActorDelegate::_Internal_AddDynamic" matches the argument list.

Sorry. I made a mistake when responding with the macro thing. The arguments for the macro should be: the object that will listen to the delegate and a reference to the function that will use it, so it should be like this:

RegisterActorDelegate.AddDynamic(character, &ATopDownCharacter::Register);

Notice the & symbol before the function name.

It was just not working. no matter how many different variations I tried. I ended up casting to the player instead, which I don’t wanna do. I now slipped into the problem that at the start I cast to the player 100+ times. which is just bad performance wise, but I don’t know how else to do it right now.