Announcement

Collapse
No announcement yet.

Change StaticMesh runtime via C++

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

    Change StaticMesh runtime via C++

    Immediately apologize for my english.
    I have some interesting problem.
    I have task to change static meshes in runtime via C++.
    And I found 2 ways to implement this task:

    1. Create direct object references via the method "ConstructorHelpers::FObjectFinder" in Constructor of Class:
    Code:
    UStaticMesh *MeshForLoad;
    auto Mesh = ConstructorHelpers::FObjectFinder<UStaticMesh>( TEXT( "StaticMesh' PathToObject'"));
    if ( Mesh.Object )
      {
           MeshForLoad = Mesh.Object;
      }
    Then in Component start method SetStaticMesh( MeshForLoad);

    That way is hardway to designers - I created code, but designers cannot have access for reference change without editing code.

    2. Create DataTable via struct, and via TAssetPtr load objects. So. It way very cool and easy for architecture, but I have problem.
    - I create struct MeshList with Property TAssetPtr <UStaticMesh> MeshPtr;
    - I create DataTable from struct MeshList.
    - Designer setup some meshes (like 2-3 references).
    - Via Code I find references, try to SetStaticMesh( MeshPtr.Get() ). But. Mesh does not want to load. Method "Get()" return null. As far as I understand - TAssetPtr stores a link to the location of the object (in my case - location of the Static Mesh).

    Okey, I found information about AssetManager (and StreamableManager). That objects can help me to load my assets.
    BUT.

    I Cannot find informantion about how create that objects (how initialize instance for work with it). In Documentation all examples are based on the fact that the object of AM (or SM) has already been created.
    Or, in some case we have singleton, which (like witch) not work in 4.22.1 =(

    Please help me =(((
    Last edited by DenKuzn; 05-12-2019, 05:56 AM.

    #2
    https://wiki.unrealengine.com/TAsset..._Asset_Loading i think u should read this.

    Comment


      #3
      Originally posted by hien27021990 View Post
      @hien27021990 On this page just talking about how use a AssetManager, but not have any information, how create that object, or where it can find.

      Comment


        #4
        https://api.unrealengine.com/INT/API...ath/index.html

        You can do something like this:

        Code:
        #include "Runtime/CoreUObject/Public/UObject/SoftObjectPtr.h"
        Code:
        UPROPERTY( Category="MyCategory", EditAnywhere, BlueprintReadWrite, meta=( DisplayName="My Custom Mesh", AllowedClasses="StaticMesh" ) )
        FSoftObjectPath CustomMesh;
        Code:
        MeshForLoad = Cast<UStaticMesh>( CustomMesh.ResolveObject() );
        
        if ( MeshForLoad == nullptr )
        {
            MeshForLoad = Cast<UStaticMesh>( CustomMesh.TryLoad() );  // Load asset if not already loaded, this is slow...
        }
        _____

        That way people using the editor can just set the value of CustomMesh and in code you can use whatever mesh they inserted there.
        Note: This is Unreal 4.20 ~ 4.22 API, in older versions this might not work at all.
        | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

        Comment


          #5
          Originally posted by BrUnO XaVIeR View Post
          You can do something like this:
          BrUnO XaVIeR

          Yes! Finally! =) That was my headache for a week! =)
          Thanks a lot! =)

          This way is much easier than all the others that I try to use.
          Do I understand correctly that in this way I can load materials, textures, etc.?

          Comment


            #6
            Yes, I do that to load lots of different things.
            Including textures for loading screens, images for Slate UI and etc.
            | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

            Comment


              #7
              Originally posted by BrUnO XaVIeR View Post
              Yes, I do that to load lots of different things.
              Including textures for loading screens, images for Slate UI and etc.
              Cool! =) Thanks again =) You super =)

              Comment


                #8
                Originally posted by BrUnO XaVIeR View Post
                Yes, I do that to load lots of different things.
                Including textures for loading screens, images for Slate UI and etc.
                That is not load, FSoftObjectPath only store infomation about the path to the object that is already loaded, if u want to load obj, u should use TAssetPtr and AssetManager. Pls correct me if i wrong but ue's documentation wrote that.

                Comment


                  #9
                  Originally posted by DenKuzn View Post

                  @hien27021990 On this page just talking about how use a AssetManager, but not have any information, how create that object, or where it can find.
                  It's more than enough lol.

                  Comment


                    #10
                    There's a link to the API page there. You could, maybe, read it...
                    ‚Äč
                    Wiki pages are written by users, they are not official documentation.
                    | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

                    Comment


                      #11
                      BrUnO XaVIeR Hi. Can you help me again?

                      I need to load UUserWidget to my WidgetComponent in AActor.
                      I set link to file via FSoftObjectPath.
                      File found, worked (I mean, if branch see him). Its not nullptr.
                      BUT. I can't Cast UObject to UUserWidget (Or my type like UWidgetTest which parant for my Widget that I tryed to load(If you understand my bad english =)) ))

                      So. How can I setup my Widget to Component (via c++, of course).

                      Comment


                        #12
                        To spawn widgets from asset, after loaded, don't cast the UUserWidget to your asset.
                        Instead get class from asset:

                        Code:
                        class UMyWidgetType* Widget;
                        Code:
                        Widget = CreateWidget<UMyWidgetType>( GetWorld(), WidgetAsset.Class );
                        | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

                        Comment


                          #13
                          Originally posted by BrUnO XaVIeR View Post
                          Instead get class from asset
                          Turns out that asset must be like auto WidgetAsset = ConstructorHelpers::FObjectFinder<UUserWidget>?

                          Something like that?

                          In constructor:
                          Code:
                          auto WidgetAsset = ConstructorHelpers::FObjectFinder<UUserWidget>( TEXT( "WidgetBlueprint' " %PathTo% /BP_Widget_Menu.BP_Widget_Menu'" ) );
                          
                              if ( WidgetAsset.Succeeded() )
                                  {
                                      UDepartMenuWidget_Base *Widget = CreateWidget<UDepartMenuWidget_Base>( GetWorld(), WidgetAsset.Class );
                                      WidgetComponent_DepartmentMenu->SetWidget( Widget );
                                  }

                          Comment


                            #14
                            If all you need from it is just to know the class, you can use TSubclassOf<UUserWidget> instead.
                            Works the same.
                            | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

                            Comment


                              #15
                              Originally posted by BrUnO XaVIeR View Post
                              If all you need from it is just to know the class, you can use TSubclassOf<UUserWidget> instead.
                              Works the same.
                              Thanks a lot =) But I still have problem. There I wrote some interesting behavior, when I try to use TSubclassOf.
                              In Short - its works wile I have open Editor. When it close and restart - I have problem - crash and errors =(

                              https://forums.unrealengine.com/deve...n-aactor-via-c

                              Comment

                              Working...
                              X