Announcement

Collapse
No announcement yet.

4.6 constructor changes, am confused

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

    4.6 constructor changes, am confused

    Hi guys,

    I am probably not the brightest person in the world...

    I just downloaded and compiled the 4.6 engine, and creating a c++ class from the editor creates files, but the cpp files has no constructor as before. Now i read here on the forum that the new thing is that now we can create "normal" constructors for our classes. But i just cant get it to work.

    If we are do create normal constructors, with no parameters req. So for a class AJWKing for example, constructor would be

    Code:
    AJWKingActor::AJWKingActor()
    {
    	/*MySphere = FObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("MySphere"));
    	MySphere->InitSphereRadius(250.0f);
    	RootComponent = MySphere;*/
    }
    I tried even the usual way constructor is defined in UE4 code, still not getting it to work.

    So who can be kind enough to show me what am i missing? How do you define a class constructor in 4.6?

    Regards,

    #2
    *EDIT* The information provided in this post will work, but is not the preferred method for adding a constructor to a code class in version 4.6. I have added a new post that provides an example of the new preferred method for accomplishing this. I am leaving this post here to avoid any confusion, but please see this post for the preferred method for accomplishing this task.


    Hi Tiskan,

    We made some changes to classes in 4.6. Up to 4.5.1, whenever you created a new class in your project, you were required to include a constructor even if you did not actually use it. That is no longer the case in 4.6. Constructors are now optional, and are not included by default in a newly-made code class. Fear not, however. It is a simple process to add one if you need it.

    When you create a new code class in 4.6, you will end up with a class formatted something like this:

    Code:
    // MyActor.h
    
    UCLASS()
    class TESTCONSTRUCTOR_API AMyActor : public AActor
    {
    	GENERATED_BODY()
    	
    	
    };
    
    /*********************************/
    
    // MyActor.cpp
    
    #include "MyActor.h"
    The first thing you will notice is the lack of any constructor in the .cpp file. In addition, those with sharp eyes will see that the macro in the .h file is also different. The GENERATED_BODY() macro allows the class to build without having a constructor defined. If you need a constructor, you can change that macro to the more familiar GENERATED_UCLASS_BODY(). If you do, you must provide a constructor for the class or you will not be able to build the project in Visual Studio. So your code will look something like this:

    Code:
    // MyActor.h
    
    UCLASS()
    class TESTCONSTRUCTOR_API AMyActor : public AActor
    {
    	GENERATED_UCLASS_BODY()
    	
    	
    };
    
    /*********************************/
    
    // MyActor.cpp
    
    #include "MyActor.h"
    
    AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer)
        : Super(ObjectInitializer)
    {
        // Class constructor/initialization code
    }
    This will get you started with creating custom code classes in 4.6. There will be more details about this change, as well as some other code-related changes, in the official release notes for version 4.6. As always, please let us know about any issues you may run into with the preview version of 4.6.

    Tim
    Last edited by Tim C; 11-19-2014, 03:27 PM. Reason: Adding note for where to find preferred method.
    Engine Support Technician

    Comment


      #3
      Thank you for fast replay Tim. I actually just tried just the way you wrote above. That's what i tried before also and called it the usual way of defining the constructor. well the usual from 4.6 anyway from what i read in another thread.

      this is my code, from the cpp file, the rest is what UE4 editor creates.
      Code:
      AMyTestActor::AMyTestActor(const FObjectInitializer& ObjectInitializer)
      	: Super(ObjectInitializer)
      {
      	// Class constructor/initialization code
      }
      but what i am getting is this error and compile failure:
      Code:
       MyTestActor.cpp
      1>E:\Game Works\Unreal Projects\RobberCenter\Source\RobberCenter\MyTestActor.cpp(8): error C2084: function 'AMyTestActor::AMyTestActor(const FObjectInitializer &)' already has a body
      1>          e:\game works\unreal projects\robbercenter\source\robbercenter\MyTestActor.h(14) : see previous definition of '{ctor}'
      Am i still missing something?

      Comment


        #4
        Did you change GENERATED_BODY() for GENERATED_UCLASS_BODY()? The compiler is complaining that that constructor already exists in your header, which I can only imagine is the one provided by GENERATED_BODY so you don't have to do it manually.

        Comment


          #5
          Thank you for that! That was the problem. I did realize that the constructor body was getting created by the macro but i just could not figure out how to modify it so it stopped doing that. Thank you.

          Comment


            #6
            Hi Tiskan,

            cmartel is correct. GENERATED_BODY() is the macro you want to have in your header file if you will not be including a class constructor in your code, whereas GENERATED_UCLASS_BODY() is the macro you need to use in your header file if you will be including a class constructor.

            Tim
            Engine Support Technician

            Comment


              #7
              Thanks for clearing that up for me guys.

              /tiskan

              Comment


                #8
                I really like the change because I don't always need to write code in the constructor. But sometimes I do. What are the possibilities of get an overridable Initializer that takes FPostConstructInitializeProperties as an argument?

                Comment


                  #9
                  Originally posted by MJLaukala` View Post
                  I really like the change because I don't always need to write code in the constructor. But sometimes I do. What are the possibilities of get an overridable Initializer that takes FPostConstructInitializeProperties as an argument?
                  FPostConstructInitializeProperties is being replaced with FObjectInitializer in 4.6. I have not yet dug too deeply into this change, though. Could you provide some clarification of what you are looking for, possibly a use-case scenario? I can try to find out if FObjectInitializer will meet that need.
                  Engine Support Technician

                  Comment


                    #10
                    I just realized that I had provided some information with my first reply to this thread that, while it will work (for now), is not the preferred method of adding a constructor to a new code class. My response was based on steps that I used on an early internal build of version 4.6, and the actual preferred method is actually slightly different. Using the example class I provided in my original response, this is the actual preferred method for including a constructor in your new code class:

                    Code:
                    // MyActor.h
                    
                    UCLASS()
                    class TESTCONSTRUCTOR_API AMyActor : public AActor
                    {
                    	GENERATED_BODY()
                    	
                            // Constructor declaration
                    	AMyActor(const FObjectInitializer& ObjectInitializer);
                    };
                    
                    /*********************************/
                    
                    // MyActor.cpp
                    
                    #include "MyActor.h"
                    
                    AMyActor::AMyActor(const FObjectInitializer& ObjectInitializer)
                        : Super(ObjectInitializer)
                    {
                        // Class constructor/initialization code
                    }
                    Note that you will still use the new GENERATED_BODY() macro in your header file (there is no longer a need to use GENERATED_UCLASS_BODY() at all). The preferred method of including a constructor in version 4.6 will be to explicitly declare the constructor in the header file. If you do not need a constructor for your class, do not include the declaration of the constructor in the header file.

                    I apologize for the incorrect information I had provided earlier.
                    Last edited by Tim C; 11-19-2014, 03:30 PM.
                    Engine Support Technician

                    Comment


                      #11
                      yeah! i like this, but why not simply put there when user create the class from wizard? after that, user can delete if dont need, much easy erase to write

                      Comment


                        #12
                        I am in agreement with ZkarmaKun, I had to do some digging to realize that it was needed and wasn't included in the create class wizard, an update would be awesome!

                        Comment


                          #13
                          This does not work in 4.10.4. wtf

                          Comment


                            #14
                            Hey Tira,

                            This thread was originally for setting up a custom class constructor in 4.6. Since there have been changes to the code base since then, some of the information here may no longer apply. If you are having problems creating your own custom classes/constructors, please create a post on the AnswerHub (https://answers.unrealengine.com/index.html) to help track and investigate the issue.

                            Cheers

                            Doug Wilson

                            Comment

                            Working...
                            X