Load *.cfg to AZ::MeshComponent, how?

Hi, how i can load *.cfg file to AZ::MeshComponent if i have pointer to AZ:Component. In example code (_newMeshComponent)

Component* _newMeshComponent = nullptr;
AZ::Uuid _meshComponentID = "{C4C69E93-4C1F-446D-AFAB-F8835AD8EFB0}";
_pEntity->Deactivate();
_newMeshComponent = _pEntity->CreateComponent(_meshComponentID);
_pEntity->Activate();

Hi @Anatoliy,

The correct way to set the mesh asset is through the MeshComponentRequestBus. However, the SetMeshAsset function that will be in the next release needs to be manually added in Lumberyard 1.3, and here’s how.

  1. Open dev/Code/Engine/LmbrCentral/include/LmbrCentral/Rendering/MeshComponentBus.h and modify MeshComponentRequest by adding the following function declaration:
    class MeshComponentRequests : public AZ::ComponentBus
{
public: ... virtual void SetMeshAsset(const AZ::Data::AssetId& id) = 0;
};
  1. Open dev/Code/Engine/LmbrCentral/source/Rendering/MeshComponent.h and modify MeshComponent by adding the following function declaration:
        class MeshComponent
: public AZ::Component
, public MeshComponentRequestBus::Handler
, public MaterialRequestBus::Handler
, public RenderNodeRequestBus::Handler
{
public:
...
void SetMeshAsset(const AZ::Data::AssetId& id) override;
}
  1. Open dev/Code/Engine/LmbrCentral/source/Rendering/MeshComponent.cpp and add the function definition around line 755.
    void MeshComponent::SetMeshAsset(const AZ::Data::AssetId& id)
{
m_mesh.SetMeshAsset(id);
}
  1. Open dev/Code/Engine/LmbrCentral/source/Rendering**/EditorMeshComponent.h** and modify MeshComponent by adding the following function declaration around line 69:
        class EditorMeshComponent
...
void SetMeshAsset(const AZ::Data::AssetId& id) override;
}
  1. Open dev/Code/Engine/LmbrCentral/source/Rendering/EditorMeshComponent.cpp and add the function definition around line 239.
    void EditorMeshComponent::SetMeshAsset(const AZ::Data::AssetId& id)
{
m_mesh.SetMeshAsset(id);
}
  1. Now you can use the SetMeshAsset EBus message to set the model like this:

#include <AzCore/Component/Entity.h>
#include <AzFramework/Entity/GameEntityContextBus.h>
#include <LmbrCentral/Rendering/MeshComponentBus.h>
#include <AzFramework/Asset/AssetCatalogBus.h>
AZ::Entity *entity = nullptr;
EBUS_EVENT_RESULT(entity, AzFramework::GameEntityContextRequestBus, CreateGameEntity, "ExampleEntity");
if (entity != nullptr)
{
AZ::Uuid meshComponentID = "{9697D425-3D28-4414-93DD-1890E576AB4B}";
entity->CreateComponent(meshComponentID);
// get the asset ID for the mesh asset (.cgf) AZ::Data::AssetId meshAssetId;
const string meshName = "objects/default/primitive_cube.cgf";
EBUS_EVENT_RESULT(meshAssetId, AzFramework::AssetCatalogRequestBus, GetAssetIdByPath, meshName, false);
if (meshAssetId.IsValid())
{
// assign a the mesh asset (.cgf) EBUS_EVENT(LmbrCentral::MeshComponentRequestBus, SetMeshAsset, meshAssetId);
}
}
entity->Init();
entity->Activate();<br>
  1. Profit.

p.s.

If you haven’t already modified your game’s wscript file to include the LmbrCentral module you’ll need to do that as well. Here’s are the settings you need to modify in your game’s wscript file:

    def build(bld):
bld.CryEngineNonRCModule(
# Add LmbrCentral to the use list
use = ['AzGameFramework','LmbrCentral'],
# Add the LmbrCentral includes path
includes = [ '.' ,
bld.Path('Code/CryEngine/CryCommon'),
bld.Path('Code/CryEngine/CryAction'),
bld.Path('Code/Engine/LmbrCentral/include')],
)

Hi petrocket :slight_smile: It so cool !!! Worked perfectly from first try !!! I am not dreaming about of such nice answer !!! Thank you for answer and your time :slight_smile: