Announcement

Collapse
No announcement yet.

BeginPlay VS Constructor?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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?

    #2
    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.

    Comment


      #3
      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.

      Comment


        #4
        Originally posted by Xdust View Post
        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 here 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.
        Last edited by warlord57; 08-08-2014, 03:03 PM.

        Comment


          #5
          Originally posted by warlord57 View Post
          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 here 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.
          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.

          Comment


            #6
            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.

            Comment


              #7
              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)

              Comment


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

                Comment


                  #9
                  Don't do any gameplay on constructor as it also called when you start the editor to load defaults so yea constructor is good for default, component initiation and delegate binding
                  =========
                  My Tutorials:
                  Basic knowledge about Classes and UObject environment and stuff like that

                  Comment


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

                    Comment

                    Working...
                    X