Announcement

Collapse
No announcement yet.

Creating a class wtihout FPostConstructInitializeProperties& PCIP?

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

    Creating a class wtihout FPostConstructInitializeProperties& PCIP?

    I have this class that I want to create within my AI

    Code:
    void ABaseAIController::moveToTarget()
    {
    	SetFocus(target);
    	MoveToActor(target);
    	Bot->currentAction =  new UMoveAction();
    }
    Here are the other classes:

    Code:
    UCLASS()
    class UMoveAction : public UAction
    {
    	GENERATED_UCLASS_BODY()
    	
    	
    };
    
    UCLASS()
    class AEntity : public ACharacter
    {
    	GENERATED_UCLASS_BODY()
    
    	UPROPERTY(VisibleAnywhere, Category = EntityType)
    	TEnumAsByte<EntityEnums::EntityType> CurrentEntityType;
    	UAction* currentAction;
    	
    };
    This issue is that the parameters require FPostConstructInitializeProperties& PCIP, which I have no idea what it is or why I need one.

    Is it possible to not have this in the parameters or if I do need it where do I get PCIP from?

    #2
    new isn't going to work for UE4 classes, have you tried replacing new UMoveAction(); with GetWorld()->SpawnActor(...)?

    edit: if it's an UObject you should use something similar to TestObject = NewObject<UPWNTestObject>(this, UPWNTestObject::StaticClass()); instead

    edit2: The answer hub has a bunch of good answers to lots of very specific problems. More info here.
    Last edited by AO; 04-05-2014, 08:28 AM.

    Comment


      #3
      Im not an expert of UE4 yet, but i think you can use this Bot->currentAction = UMoveAction::StaticClass(); ??

      Comment


        #4
        So I now have this:

        Code:
        Bot->CurrentAction = NewObject<UMoveAction>(this, UMoveAction::StaticClass());
        but how would I pass in parameters then?

        Comment


          #5
          This issue is that the parameters require FPostConstructInitializeProperties& PCIP, which I have no idea what it is or why I need one.
          Is it possible to not have this in the parameters or if I do need it where do I get PCIP from?

          This required line for UObject declarations...

          Code:
          GENERATED_UCLASS_BODY()
          ... effectively creates some glue that makes your class work with the UObject sub-system. Part of that glue is the declaration of a class constructor, and it is required that you implement it in your module. If your class has a lot of methods, you probably already have a separate .cpp file just for that class, and you can add the constructor definition to that file. If your class doesn't have any code, or all code is inlined, then you can just throw it into any other .cpp file. Typically I use the MyModule.cpp for that, or if I have many classes with no code I add MyModuleClasses.cpp and put the constructors in there (for example, take a look at SettingsClasses.cpp).

          The format for the constructor definition is as follows:

          Code:
          UMyCoolClass::UMyCoolClass( const class FPostConstructInitializeProperties& PCIP )
          	: Super(PCIP)
          {
              /* do object initialization in here, if needed, otherwise leave empty */
          }

          For the creation of UObjects see: UObject Instance Creation
          For the creation of AActors see: Spawning Actors

          Note that UObjects (and therefore AActors as well) have their properties initialized from the INI file, or set to 0/nullptr if no settings are specified. You cannot pass any additional arguments into the constructors but will have to defer all run-time initialization to subsequent method calls or property assignments after creating the object. It seems like a limitation at first, but it opens up some interesting possibilities. For example, all UObjects and actors have the same constructor API, which means that they can be easily created automatically by other means, i.e. from the Blueprint system or when they're replicated over the network. The only thing you have to provide is a class name.

          If custom constructor arguments were allowed then we would have to provide a mechanism to generate constructor calls and populate them with valid arguments automatically at run-time. Probably the only elegant way to do this would be a dependency injection framework, which is quite difficult to create and use in C++. In other similar languages, such as C# this may be a little bit easier, but note that even there most frameworks do not allow custom constructor arguments for serializable objects.
          Last edited by gmpreussner; 04-06-2014, 08:56 PM. Reason: added docs links
          Gerke Max Preussner | UE4 Programming Consultant

          Logo
          Follow me on Github | Goodreads | Linkedin | Pinterest | SlideShare | Twitter.
          Chat with me as gmpreussner on Discord.

          Comment


            #6
            Is is possible to make a normal c++ class instead, I only need something that extends base object anyway?

            Comment


              #7
              You can just create your own plain old C++ classes in Visual Studio.

              Code:
              class FMyCoolClass { .. };
              It's not possible yet to create C++ classes from the Editor yet.
              Gerke Max Preussner | UE4 Programming Consultant

              Logo
              Follow me on Github | Goodreads | Linkedin | Pinterest | SlideShare | Twitter.
              Chat with me as gmpreussner on Discord.

              Comment


                #8
                Thanks, I'll give it ago

                Comment


                  #9
                  Originally posted by AO View Post
                  new isn't going to work for UE4 classes, have you tried replacing new UMoveAction(); with GetWorld()->SpawnActor(...)?

                  edit: if it's an UObject you should use something similar to TestObject = NewObject<UPWNTestObject>(this, UPWNTestObject::StaticClass()); instead

                  edit2: The answer hub has a bunch of good answers to lots of very specific problems. More info here.
                  I've added this to the wiki a while back:
                  https://wiki.unrealengine.com/UE3_to...n_Guide#Coding

                  Comment


                    #10
                    Do I still need a .h and .cpp for normal c++ classes?

                    I've got this atm as my .h:

                    Code:
                    namespace ActionEnums
                    {
                    	enum ActionType
                    	{
                    		Movement,
                    		Combat,
                    		Other
                    	};
                    }
                    
                    
                    class Action 
                    {
                    	ActionEnums::ActionType CurrentActionType;	
                    };
                    and this as my .cpp:

                    Code:
                    #include "The_Battle_of_Maldon.h"
                    #include "Action.h"
                    
                    Action::Action()
                    {
                    
                    }
                    doesn't like me doing Action::Action()?

                    Comment


                      #11
                      What happens if you put the constructor declaration in the header?
                      Action();

                      Comment


                        #12
                        Ah ha, I forgot about constructors, didn't realize that it went in here:

                        Code:
                        class Action 
                        {
                                 Action()
                                 {
                        
                                 }
                        	ActionEnums::ActionType CurrentActionType;	
                        };
                        You've fixed my problem, thanks

                        Comment


                          #13
                          New issue I have this within my Bots:

                          Action* CurrentAction;

                          And I try and assign it within my AIController like so:


                          Bot->CurrentAction = new MoveAction();

                          Here is MoveAction

                          Code:
                          #pragma once
                          
                          #include "Action.h"
                          
                          class MoveAction : public Action
                          {
                          public:
                          	MoveAction()
                          	{
                          		CurrentActionType = ActionEnums::Movement;
                          	}
                          };
                          It's giving me this as an error:

                          error C2440: '=' : cannot convert from 'MoveAction *' to 'Action *'

                          Any Ideas?

                          Comment


                            #14
                            Your Action() constructor is private, the default access type for a class. Make it public.

                            Comment


                              #15
                              I wish I had a second pair of eyes around, almost all problems are stupid mistakes, Thanks so much

                              Comment

                              Working...
                              X