Spawning an Actor when using a plugin

I feel like I am missing some fundamental knowledge on how plugins are designed to work in unreal. My ultimate intent is to have a plugin that responds when a level has started. On start it will spawn a few actors and clicking on some will cause effects on the other.

With that in mind my plan for going forward with this was to create a plugin that will manage these interactions between the actors and have direct knowledge of them. This would mean that upon receiving a click event for actor A, the plugin toggles a flag in actor B.

So my short question is, how can I spawn an actor in the IModuleInterface that unreal provides when creating a plugin. I’ve tried directly accessing the engine to do so during the StartupModule and it is null there. I’ve attempted to leverage the PostLoadCallback but that function never gets called.

My longer question is, this seems way more difficult than it needs to be so I suspect there is some fundamental that I am missing about plugins. If anyone can either explain or point me to good documentation that explains the intended use of plugins that would be great.

Thanks in advance.

First of all, a plugin is no different from code placed in the source folder of the project, or code contained in the engine (4.25 in this case) folder. All code in UE is contained within modules (you have a file for each module). This means that what you are asking for can be accomplished in any of the three different locations. Modules Documentation

Now, to the actual question, it is a broad question and it has a long and complex answer to it. I can’t give you a complete answer here, but I’ll try to lead you in the right direction.

First question you need to ask yourself is, how generic should this be? is Actor A and B predefined classes or should this work on all actors? Do you want this to be tightly integrated with the editor, or can this “Click on A, send information to B” functionality go via an actor you place in the level, acting as a middleman?

You could simply create an actor, have a delegate called when actor A is clicked, have a reference to actor B and set some variable. For picking up when an actor has been clicked there is a delegate on Actors called FActorOnClickedSignature. Use this to get a generic way of picking up clicked actors.

If this is not good enough, you can overload the FEdMode class and basically create your own editor (with custom functionality) within UE4, cool huh? FEdMode Documentation. This needs to reside within an editor module.

This can further be expanded by adding ComponentVisualizers and other cool stuff. This way you can add menus and visualizers, to make sending data to actor B more interactive.

Basically what I’m trying to show is that this question can be expanded to infinity, the engine is truly open and generic and you can do as you choose. From your question I get the impression that you are new to the engine, so I suggest learning the basics first and then progressively start implementing more advanced functionality,

Hope this at least gave some ways forward!

Hey thanks for this answer. So you are right I am definitely new to unreal and right now I’m trying to prove a basic and simple concept for this project I am working on to see if this engine is right for me. I am however very familiar with c++.
I’m looking to make two classes that represent two types of objects where actor A sends information to B. It sounds like this FActorOnClickedSignature reflects a lot of what I am trying to do. Having a new actor that operates as a middle man seems like a weird solution for me.

Right now I’d like a solution where I’ve created two actor classes; A and B. On scenario start or mission load the plugin will spawn in actors at select locations and the plugin knows when these actors are clicked and can pass information between them. It sounds like how unreal works though is that you have to program through actors? Meaning I would have some invisible actor that is the middle man for handling these events?

Oh and one more thing, it isn’t exactly clear to me how to register for these events. I know Actors have access to the OnClicked event. But I’m not sure how to make my IModuleInterface register for the OnClick event

I noticed that I didn’t consider that this may not be editor functionality you’re looking for? Is this intended to be Editor behavior or Runtime behavior?

This is intended to be a runtime behavior. So the order of events I’m looking to do is something like

  1. Level loads
  2. Plugin’s IModuleInterface or some other management system spawns in actors
  3. Plugin’s IModuleInterface or some other management system registers for on actor click events
  4. On receiving click from Actor A, the management system lets Actor B know that A was clicked

Ok, gotcha! Well, if you can live with that your plugin requires the game to use a GameInstance that the plugin comes with, I think this is a nice way of “Spawning actors on level load”. Since the GameInstance is global for the project, this will take place in all levels loaded.

  1. Create a custom GameInstance class in the plugin. Override this function.
  2. In this function you can spawn the actors.
  3. Regarding clicking on actors in runtime: see this link. In addition, I believe this is also required.
  4. Regarding how to achieve the notification, there are many ways this can be achieved. What comes to mind first is event dispatchers (basically delegates in c++). You might want to use an interface aswell to make the notification a bit more generic?