How do I spawn an actor at game start?

I don’t want to create the object in the Unreal Editor. I want to be able to just edit c++ code, and when I compile and run the game, it spawns an actor at a certain location. There doesn’t seem to be a main function where I can run a command at the start of the game where I could use the SpawnActor function. I know how to place an object in the Unreal Editor, and I know how to spawn an actor from another class, but that’s not what I want to do. I just want to be able to spawn the actor at runtime.

To spawn the actor during runtime you just call the function SpawnActor and that is it. I dont understand what the problem is.

If you want to spawn it at the beginning of the game you call the SpawnActor function in BeginPlay.

If not, please describe your problem with more details.

Yes, but the level is spawned automatically, so the code would have to go there. Inside the Map there is also a function called BeginPlay and it will be called at the beginning of the game no matter what. You have to spawn the actor in the class that lies on the level above it. It seems like it is the Map in your case.

Isn’t BeginPlay() only called when an object has already been created? In other words, if I have a class called Foo, won’t Foo.BeginPlay() only be called when a Foo object is created. So how could I spawn the Foo object, without already having already spawned a Foo object.

I’m not sure I follow you when you say “the level is spawned automatically”. Thanks for helping me with this though :slight_smile:

What c++ file refers to the umap code?

You must have a level. This can either be in Blueprint or in C++. I always load the level in blueprint and then spawn/load all my actors etc from there. That is my “Main”. Inside the level blueprint you can spawn the actor you want.

You set what level is to be spawned inside the game mode settings I believe.

Well, you can’t totally bypass the editor cause you have to set a minimal bunch of settings before being able to run your own C++ code.

Since you are talking about “GameStart” and not “LevelStart”, I suggest you use the virtual void YOURGAMEINSTANCE::Init() function which is called when the “game” starts. Then you can do whatever you want from there.
But, you will have to set in your project (using the editor or some .ini files) that you want to use YOURGAMEINSTANCE type instead of the default one.

Good luck, best regaards.