BeginPlay VS Constructor?

I have been trying to spawn a certain object when the game starts and have been using constructor to do so, but nothing is happening. DebugMessages I surrounded it with are neither firing. Is there some crucial difference between BeginPlay and contructor?

This is due to that when the constructor is called the Actor or world is potentaly not loaded.
You can use BeginPlay if the actor is placed in the world, or PostInitelize methods to do this.

Yea, the constructor seems to be used for the only what inside the “object” itself.
Dont try and spawn anything else outside the “object” own constructor.

Use BeginPlay for anything outside the “Object”.

Man, If that makes any sense. Bet your Glad I dont write Docs.

It makes a little sense. I will try and rephrase that for you.

Think of the constructor as when the object/actor is built. Use this for all the default settings and so on.

The BeginPlay function is called when the game actually starts or after the object/actor is spawned and in the game world while the game is running from my understanding and this is where you want to put in any spawning you need to do or anything game related. For example if you need to get a pointer to the player controller in your character class you would do that and not in the constructor. In the constructor it wouldn’t know anything about the player controller that is possessing the character/pawn as it isn’t possessed yet. Not really sure when PostInitialize gets called in comparison to the other 2 functions.

I hope this clears up some stuff about the difference between the 2 functions.I am still learning myself on how and when each function is called to know where to place the code I need to add and so on so if anyone else can explain it better or if I have something incorrect in my explanation, please let us know.

1 Like

LOL, yea, that’s a better description.

One thing, I think the BeginPlay is fired after ALL “Persistent objects”, I think that’s the official UE4 name, in the scene are loaded and ready to play.

Thus insuring everything is loaded, and ready to run.

The order goes:

Constructor
PostInitializeProperties
BeginPlay

It’s safe to gameplay relevant code in BeginPlay. Constructor/PostInitializeProperties might be called when there is no world and are called on the default object. You definitely do not want to be spawning things that the CDO owns (Class Default Object), since the CDO exists outside of any world.

Generally, I use PostInitializeProperties for things that need extra fixup: calculations based off of defaults, range validation, etc.

I have no seen a way I can get anything done that way. As it is, only things that fire up when starting the play are the gamemode, and the only thing spawned is the character. Which means I have to spawn it through another object (and I will need to do it anyway but from another object instead of a gamemode)

I am suggesting that you add your spawning of the class to your GameMode::BeginPlay(). That works for our games.

Don’t do any gameplay on constructor as it also called when you start the editor to load defaults :stuck_out_tongue: so yea constructor is good for default, component initiation and delegate binding

It is best not to do anything that might fail in a constructor. It can be confusing and hard to troubleshoot for beginners.