[UE4] C + + implementation of dynamic loading problems:loadclass<t> () and loadobject<t> () and static loading problems: Constructorhelpers::fclassfinder () and Fobjectfinder ()

Source: Internet
Author: User

Transferred from: http://aigo.iteye.com/blog/2281558

Dynamically loaded Uobject and dynamically loaded Uclass are respectively loadobject<t> (), and loadclass<t> () , both in The UObjectGlobals.h .

Also note:loadclass<T> template name, can not directly write Ublueprint, for example:loadclass<ublueprint> is wrong, When you create a blueprint, you select the parent class, then write the corresponding parent class name , if it is actor, write:loadclass<aactor>, otherwise it cannot be loaded successfully.

The path name must also have a _c suffix (loadobject does not need to have a _c suffix), for example, the blueprint path is:Blueprint '/game/blueprints/mybp.mybp ',

After the suffix, it is:Blueprint '/game/blueprints/mybp.mybp_c',

Example:

uclass* Test = loadclass<aactor> (NULL, TEXT ("Blueprint '/game/blueprints/mappathbrush_bp. Mappathbrush_bp_c '"));  

The official does not have the document, can only read the code comment first:

// Load an object.   class T >   inline tconstconst tchar* filename=nullptr, UInt32 Loadflags=load_none, upackagemap* sandbox=nullptr)  {      return (t*) staticloadobject (T::staticclass ( ), Outer, Name, Filename, Loadflags, Sandbox);  }  
// Load a Class object.   class T >   inline uclassconstconst tchar* filename=nullptr, UInt32 Loadflags=load_none, upackagemap* sandbox=nullptr)  {      return  staticloadclass (T::staticclass (), Outer, Name, Filename, Loadflags, Sandbox);  }  
/** * Find or load an object by string name with optional outer and filename specifications.  * These is optional because the inname can contain all of the necessary information.  * * @param ObjectClass the class (or a superclass) of the object to be loaded.  * @param inouter An optional object to narrow where to find/load the object from * @param inname String Name of the object. If it ' s not fully qualified, inouter and/or filename would be needed * @param Filename a optional file to load from  (or find in the file ' s package object) * @param loadflags Flags Controlling how to handle loading from disk * @param  Sandbox A List of packages to restrict the search for the object * @param ballowobjectreconciliation Whether to Allow the object to being found via Findobject in the case of seek free loading * * @return The object is loaded or Found.  NULL for a failure. */Coreuobject_api Uobject* Staticloadobject (uclass* Class, uobject* Inouter,ConstTchar* Name,Consttchar* Filename = null, UInt32 loadflags = Load_none, upackagemap* Sandbox = null,BOOLBallowobjectreconciliation =true ); Coreuobject_api Uclass* Staticloadclass (uclass* baseclass, uobject* Inouter,ConstTchar* Name,Consttchar* Filename = null, UInt32 loadflags = Load_none, upackagemap* Sandbox = null);

Loadobject Loading example, do not need to add suffix:

utexture2d* Tex = loadobject<utexture2d> (NULL, TEXT ("texture2d '/game/textures/ui/tex_ test001.tex_test001 '"));  

Files that can be loaded with Loadobject include:

Texture, Material, Soundwave, Soundcue, Particlessystem, Animmontage, Blendspace (1d,2d,3d), Animsequence, Animblueprint, Skeletalmesh and so on. The parent classes of these files are uobject, so you can also load as uobject* and then strong to specific types, for example:

uobject* OBJ = loadobject<uobject> (NULL, TEXT ("skeletalmesh '/game/mymesh.mymesh '" ));  Uskeletalmesh* Mymesh = cast<uskeletalmesh*> (OBJ);  

Implement dynamic load Uobject:staticloadobject (); Take texture and material as an example

Example 1:

Tool method for dynamically loading object

utexture2d* Mytextureloader::loadtexturefrompath (const fstring& Path)  {      if return NULL;         return Cast<utexture2d> (Staticloadobject (Utexture2d::staticclass (), NULL, *(Path)));  
Call:
" /game/textures/yourstructurehere " ;  Utexture2d* tmptexture = Loadtexturefrompath (pathtoload);  

Example 2:
Loading Material and Texture

structFconstructorstatics {constructorhelpers::fobjectfinderoptional<UTexture>Texturefinder; Constructorhelpers::fobjectfinderoptional<UMaterial>Materialfinder; Fconstructorstatics (): Texturefinder (TEXT ("texture2d '/game/textures/2dbackground.2dbackground '")), Materialfinder (TEXT ("Material '/game/materials/dynamictexturematerial.dynamictexturematerial '"))       {       }   }; StaticFconstructorstatics Constructorstatics; Texture=ConstructorStatics.TextureFinder.Get (); Umaterial* Material =ConstructorStatics.MaterialFinder.Get (); Dynamicmaterial= Umaterialinstancedynamic::create (Material, This);

The settings call the loaded material and texture:

Dynamicmaterial->settextureparametervalue (FName ("dynamictexture"), Texture);  Mesh->setmaterial (0, dynamicmaterial);  

If the resource is never used and you want to destroy the resource object, the code is as follows:

// This assumes that Mytex is  legally valid .   Mytex, Conditionalbegindestroy ();   = NULL;  Getworld ()->forcegarbagecollection (true);  

Dynamic Asset Loading with C + +

Https://www.youtube.com/watch?v=pJIAmSGxfmQ

Dynamic Load Object

Https://wiki.unrealengine.com/Dynamic_Load_Object

[UE4] C + + static load Problem: Constructorhelpers::fclassfinder () and Fobjectfinder ()

The static load here refers to the loading method that must be done in the constructor, the dynamic loading is worth the way that can be loaded during runtime, UE4 Source, the former is actually a layer of the latter package, that is, Fobjectfinder () is the Loadobject () The package. But,fclassfinder () is not an encapsulation of loadclass (),Fclassfinder () is called Loadobject () internally.

If you want to get the type class of a blueprint BP, you can get it through Constructorhelpers::fclassfinder (), for example:

Static Constructorhelpers::fclassfinder<aactor> Unitselector (TEXT ("Blueprint '/game/myproject/ Myblueprint.myblueprint '"));  Tsubclassof<AActor> unitselectorclass = Unitselector.class;  

However, when you start the game, you will get an error indicating that you cannot find the file, for example:

Default property Warnings and errors:

Error:cod Constructor (Mygamemode): Failed to Find/game/myproject/myblueprint.myblueprint

There are two kinds of solutions (this is a pit of UE4, a waste of my long time ...). ):

A, add _cto the file path after copy reference , for example:Blueprint '/game/blueprints/myblueprint.myblueprint_c '

Static Constructorhelpers::fclassfinder<aactor> Unitselector (TEXT ("Blueprint '/game/blueprints/ Myblueprint.myblueprint_c '"));  Tsubclassof<AActor> unitselectorclass = Unitselector.class;  

B, remove the path prefix:/game/blueprints/myblueprint

Static Constructorhelpers::fclassfinder<aactor> Unitselector (TEXT ("/game/blueprints/myblueprint "));  Tsubclassof<AActor> unitselectorclass = Unitselector.class;  

Also note the template name of:fclassfinder<t>, cannot write ublueprint directly, for example:fclassfinder<ublueprint> is wrong. When you create a blueprint, you select the parent class, then write the corresponding parent class name, if it is an actor, it should be written as: fclassfinder<aactor> otherwise it cannot be loaded successfully.

Template names must be the same when using tsubclassof<t>

In addition, the template name in the fclassfinder<t> () function must be the same as the template name of the tsubclassof<t> variable , for example, the above is aactor, and if not, the above error will occur.
Give another example:

Static Constructorhelpers::fclassfinder<uuserwidget> TESTBP (TEXT ("/game/blueprints/mywidget_bp " ));  Tsubclassof<UUserWidget> mywidgetclass = Testbp.class;  

You can also replace tsubclassof<t> with uclass*

For example:

Static Constructorhelpers::fclassfinder<uuserwidget> TESTBP (TEXT ("/game/blueprints/mywidget_bp "));  Uclass* Mywidgetclass = Testbp.class;  

Before seeing a lot of examples is through fobjectfinder () to get class, now think about feeling is helpless, UE4 document comparison Pit, not only the blueprint of the document update is not synchronized, C + + documentation is even less pitiful.

Static Constructorhelpers::fobjectfinder<ublueprint> Unitselector (TEXT ("Blueprint '/game/myproject/ Myblueprint.myblueprint '"));  Tsubclassof<AActor> unitselectorclass = (uclass*) unitselector.object->generatedclass;  

Other references:

CDO constructor:failed to find Blueprint

Https://answers.unrealengine.com/questions/84880/cdo-constructor-failed-to-find-blueprint-ue-44.html

[UE4] C + + implementation of dynamic loading problems:loadclass<t> () and loadobject<t> () and static loading problems: Constructorhelpers::fclassfinder () and Fobjectfinder ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.