Mujoco.class.component

This commit is contained in:
gurkan01 2025-04-25 23:32:32 +03:00
parent 0ebe0cbfc0
commit 0ad6c21aa7
14 changed files with 493 additions and 26 deletions

View File

@ -1,14 +1,88 @@
#include "Components/MujocoActuatorComponent.h"
#include "Components/MujocoClassComponent.h"
UMujocoActuatorComponent::UMujocoActuatorComponent()
{
PrimaryComponentTick.bCanEverTick = false;
ClassComponent = nullptr;
}
void UMujocoActuatorComponent::BeginPlay()
{
Super::BeginPlay();
// Try to find class component in owner if not set
if (!ClassComponent)
{
if (AActor* Owner = GetOwner())
{
ClassComponent = Owner->FindComponentByClass<UMujocoClassComponent>();
}
}
}
void UMujocoActuatorComponent::OnRegister()
{
Super::OnRegister();
// Try to find class component in owner during component registration
if (!ClassComponent)
{
if (AActor* Owner = GetOwner())
{
ClassComponent = Owner->FindComponentByClass<UMujocoClassComponent>();
}
}
}
void UMujocoActuatorComponent::SetClassComponent(UMujocoClassComponent* NewClassComponent)
{
if (NewClassComponent != ClassComponent)
{
// Remove from old class component if exists
if (ClassComponent)
{
ClassComponent->RemoveAssociatedComponent(this);
}
ClassComponent = NewClassComponent;
// Add to new class component if valid
if (ClassComponent)
{
ClassComponent->AddAssociatedComponent(this);
}
}
}
FString UMujocoActuatorComponent::GetClassParameter(const FString& ParamName) const
{
if (ClassComponent && ClassComponent->ClassData.Parameters.Contains(ParamName))
{
return *ClassComponent->ClassData.Parameters.Find(ParamName);
}
return FString();
}
bool UMujocoActuatorComponent::HasClassComponent() const
{
return ClassComponent != nullptr;
}
#if WITH_EDITORONLY_DATA
void UMujocoActuatorComponent::OnComponentCreated()
{
Super::OnComponentCreated();
// Try to find class component in owner during creation
if (!ClassComponent)
{
if (AActor* Owner = GetOwner())
{
ClassComponent = Owner->FindComponentByClass<UMujocoClassComponent>();
}
}
if (GEngine && GetWorld())
{
FString UniqueName = GenerateUniqueName(GetWorld(), this, GetName());
@ -19,6 +93,18 @@ void UMujocoActuatorComponent::OnComponentCreated()
void UMujocoActuatorComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
const FName PropertyName = (PropertyChangedEvent.Property != nullptr) ? PropertyChangedEvent.Property->GetFName() : NAME_None;
if (PropertyName == GET_MEMBER_NAME_CHECKED(UMujocoActuatorComponent, ClassComponent))
{
// Update associations when class component is changed in editor
if (ClassComponent)
{
ClassComponent->AddAssociatedComponent(this);
}
}
if (GEngine && GetWorld())
{
FString UniqueName = GenerateUniqueName(GetWorld(), this, GetName());

View File

@ -0,0 +1,130 @@
#include "Components/MujocoClassComponent.h"
#include "Components/MujocoJointComponent.h"
#include "Components/MujocoActuatorComponent.h"
UMujocoClassComponent::UMujocoClassComponent()
{
PrimaryComponentTick.bCanEverTick = false;
JointComponent = nullptr;
ActuatorComponent = nullptr;
}
void UMujocoClassComponent::AddAssociatedComponent(UActorComponent* Component)
{
if (Component)
{
AssociatedComponents.AddUnique(Component);
// Also update single references if applicable
if (UMujocoJointComponent* JointComp = Cast<UMujocoJointComponent>(Component))
{
SetJointComponent(JointComp);
}
else if (UMujocoActuatorComponent* ActuatorComp = Cast<UMujocoActuatorComponent>(Component))
{
SetActuatorComponent(ActuatorComp);
}
}
}
void UMujocoClassComponent::RemoveAssociatedComponent(UActorComponent* Component)
{
if (Component)
{
AssociatedComponents.Remove(Component);
// Also update single references if applicable
if (UMujocoJointComponent* JointComp = Cast<UMujocoJointComponent>(Component))
{
if (JointComponent == JointComp)
{
JointComponent = nullptr;
}
}
else if (UMujocoActuatorComponent* ActuatorComp = Cast<UMujocoActuatorComponent>(Component))
{
if (ActuatorComponent == ActuatorComp)
{
ActuatorComponent = nullptr;
}
}
}
}
void UMujocoClassComponent::SetClassParameter(const FString& ParamName, const FString& ParamValue)
{
ClassData.Parameters.Add(ParamName, ParamValue);
}
void UMujocoClassComponent::SetJointComponent(UMujocoJointComponent* NewJointComponent)
{
if (NewJointComponent != JointComponent)
{
// Remove old joint if exists
if (JointComponent)
{
RemoveAssociatedComponent(JointComponent);
}
JointComponent = NewJointComponent;
ClassData.ClassType = EMujocoClassType::Joint;
// Add to associated components if valid
if (JointComponent)
{
AssociatedComponents.AddUnique(JointComponent);
JointComponent->SetClassComponent(this);
}
}
}
void UMujocoClassComponent::SetActuatorComponent(UMujocoActuatorComponent* NewActuatorComponent)
{
if (NewActuatorComponent != ActuatorComponent)
{
// Remove old actuator if exists
if (ActuatorComponent)
{
RemoveAssociatedComponent(ActuatorComponent);
}
ActuatorComponent = NewActuatorComponent;
ClassData.ClassType = EMujocoClassType::Actuator;
// Add to associated components if valid
if (ActuatorComponent)
{
AssociatedComponents.AddUnique(ActuatorComponent);
ActuatorComponent->SetClassComponent(this);
}
}
}
#if WITH_EDITORONLY_DATA
void UMujocoClassComponent::OnComponentCreated()
{
Super::OnComponentCreated();
}
void UMujocoClassComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
const FName PropertyName = (PropertyChangedEvent.Property != nullptr) ? PropertyChangedEvent.Property->GetFName() : NAME_None;
if (PropertyName == GET_MEMBER_NAME_CHECKED(UMujocoClassComponent, JointComponent))
{
if (JointComponent)
{
SetJointComponent(JointComponent);
}
}
else if (PropertyName == GET_MEMBER_NAME_CHECKED(UMujocoClassComponent, ActuatorComponent))
{
if (ActuatorComponent)
{
SetActuatorComponent(ActuatorComponent);
}
}
}
#endif

View File

@ -1,38 +1,124 @@
#include "Components/MujocoJointComponent.h"
#include "Components/MujocoBodyComponent.h"
#include "Components/MujocoClassComponent.h"
UMujocoJointComponent::UMujocoJointComponent()
{
PrimaryComponentTick.bCanEverTick = false;
ClassComponent = nullptr;
}
void UMujocoJointComponent::BeginPlay()
{
Super::BeginPlay();
// Try to find class component in owner if not set
if (!ClassComponent)
{
if (AActor* Owner = GetOwner())
{
ClassComponent = Owner->FindComponentByClass<UMujocoClassComponent>();
}
}
}
void UMujocoJointComponent::OnRegister()
{
Super::OnRegister();
// Try to find class component in owner during component registration
if (!ClassComponent)
{
if (AActor* Owner = GetOwner())
{
ClassComponent = Owner->FindComponentByClass<UMujocoClassComponent>();
}
}
}
void UMujocoJointComponent::SetClassComponent(UMujocoClassComponent* NewClassComponent)
{
if (NewClassComponent != ClassComponent)
{
// Remove from old class component if exists
if (ClassComponent)
{
ClassComponent->RemoveAssociatedComponent(this);
}
ClassComponent = NewClassComponent;
// Add to new class component if valid
if (ClassComponent)
{
ClassComponent->AddAssociatedComponent(this);
}
}
}
FString UMujocoJointComponent::GetClassParameter(const FString& ParamName) const
{
if (ClassComponent && ClassComponent->ClassData.Parameters.Contains(ParamName))
{
return *ClassComponent->ClassData.Parameters.Find(ParamName);
}
return FString();
}
bool UMujocoJointComponent::HasClassComponent() const
{
return ClassComponent != nullptr;
}
#if WITH_EDITORONLY_DATA
void UMujocoJointComponent::OnComponentCreated()
{
Super::OnComponentCreated();
if (GEngine && GetWorld())
{
if (UMujocoBodyComponent* ParentBody = Cast<UMujocoBodyComponent>(GetAttachParent()))
{
FString BodyName = ParentBody->GetName();
FString UniqueName = GenerateUniqueName(GetWorld(), this, BodyName + TEXT("_Joint"));
Rename(*UniqueName);
}
else
{
FString UniqueName = GenerateUniqueName(GetWorld(), this, GetName());
Rename(*UniqueName);
}
}
Super::OnComponentCreated();
// Try to find class component in owner during creation
if (!ClassComponent)
{
if (AActor* Owner = GetOwner())
{
ClassComponent = Owner->FindComponentByClass<UMujocoClassComponent>();
}
}
if (GEngine && GetWorld())
{
if (UMujocoBodyComponent* ParentBody = Cast<UMujocoBodyComponent>(GetAttachParent()))
{
FString BodyName = ParentBody->GetName();
FString UniqueName = GenerateUniqueName(GetWorld(), this, BodyName + TEXT("_Joint"));
Rename(*UniqueName);
}
else
{
FString UniqueName = GenerateUniqueName(GetWorld(), this, GetName());
Rename(*UniqueName);
}
}
}
void UMujocoJointComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
Super::PostEditChangeProperty(PropertyChangedEvent);
if (GEngine && GetWorld())
{
FString UniqueName = GenerateUniqueName(GetWorld(), this, GetName());
Rename(*UniqueName);
}
Super::PostEditChangeProperty(PropertyChangedEvent);
const FName PropertyName = (PropertyChangedEvent.Property != nullptr) ? PropertyChangedEvent.Property->GetFName() : NAME_None;
if (PropertyName == GET_MEMBER_NAME_CHECKED(UMujocoJointComponent, ClassComponent))
{
// Update associations when class component is changed in editor
if (ClassComponent)
{
ClassComponent->AddAssociatedComponent(this);
}
}
if (GEngine && GetWorld())
{
FString UniqueName = GenerateUniqueName(GetWorld(), this, GetName());
Rename(*UniqueName);
}
}
#endif

View File

@ -4,6 +4,7 @@
#include "Enums/MujocoEnums.h"
#include "Structs/MujocoActuator.h"
#include "Misc/UniqueNamedComponent.h"
#include "Components/MujocoClassComponent.h"
#include "MujocoActuatorComponent.generated.h"
UCLASS(Blueprintable, ClassGroup = (Rendering, Common), hidecategories = (Object, Activation, "Components|Activation"), ShowCategories = (Mobility), editinlinenew, meta = (BlueprintSpawnableComponent))
@ -20,8 +21,30 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco")
FMujocoActuatorV2 Actuator;
// Reference to the class component
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UMujocoClassComponent> ClassComponent;
// Function to set class component
UFUNCTION(BlueprintCallable, Category = "Mujoco")
void SetClassComponent(UMujocoClassComponent* NewClassComponent);
// Function to get class parameters
UFUNCTION(BlueprintCallable, Category = "Mujoco")
FString GetClassParameter(const FString& ParamName) const;
// Function to check if has class component
UFUNCTION(BlueprintCallable, Category = "Mujoco")
bool HasClassComponent() const;
#if WITH_EDITORONLY_DATA
virtual void OnComponentCreated() override;
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
#endif
protected:
virtual void BeginPlay() override;
// Called when the game starts or when spawned
virtual void OnRegister() override;
};

View File

@ -0,0 +1,94 @@
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Enums/MujocoEnums.h"
#include "Misc/UniqueNamedComponent.h"
#include "MujocoClassComponent.generated.h"
UENUM(BlueprintType)
enum class EMujocoClassType : uint8
{
None UMETA(DisplayName = "None"),
Joint UMETA(DisplayName = "Joint"),
Actuator UMETA(DisplayName = "Actuator")
};
USTRUCT(BlueprintType)
struct FMujocoClassData
{
GENERATED_BODY()
// Class name for the Mujoco element
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco")
FString ClassName;
// Type of the class
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco")
EMujocoClassType ClassType = EMujocoClassType::None;
// Additional class-specific parameters can be added here
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco")
TMap<FString, FString> Parameters;
};
UCLASS(Blueprintable, ClassGroup = (Mujoco), meta = (BlueprintSpawnableComponent))
class LUCKYMUJOCO_API UMujocoClassComponent : public UActorComponent, public TUniqueNamedComponent
{
GENERATED_BODY()
public:
UMujocoClassComponent();
UPROPERTY(Transient, VisibleAnywhere, Category = "Mujoco")
int32 MujocoID = -1;
// Class data for the Mujoco element
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco")
FMujocoClassData ClassData;
// Reference to associated components (joints, actuators, etc.)
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco")
TArray<UActorComponent*> AssociatedComponents;
// Single reference to a joint component
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco")
TObjectPtr<class UMujocoJointComponent> JointComponent;
// Single reference to an actuator component
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco")
TObjectPtr<class UMujocoActuatorComponent> ActuatorComponent;
#if WITH_EDITORONLY_DATA
virtual void OnComponentCreated() override;
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
#endif
// Function to add associated components
UFUNCTION(BlueprintCallable, Category = "Mujoco")
void AddAssociatedComponent(UActorComponent* Component);
// Function to remove associated components
UFUNCTION(BlueprintCallable, Category = "Mujoco")
void RemoveAssociatedComponent(UActorComponent* Component);
// Function to set class parameters
UFUNCTION(BlueprintCallable, Category = "Mujoco")
void SetClassParameter(const FString& ParamName, const FString& ParamValue);
// Function to set single joint component
UFUNCTION(BlueprintCallable, Category = "Mujoco")
void SetJointComponent(class UMujocoJointComponent* NewJointComponent);
// Function to set single actuator component
UFUNCTION(BlueprintCallable, Category = "Mujoco")
void SetActuatorComponent(class UMujocoActuatorComponent* NewActuatorComponent);
// Function to get single joint component
UFUNCTION(BlueprintCallable, Category = "Mujoco")
class UMujocoJointComponent* GetJointComponent() const { return JointComponent; }
// Function to get single actuator component
UFUNCTION(BlueprintCallable, Category = "Mujoco")
class UMujocoActuatorComponent* GetActuatorComponent() const { return ActuatorComponent; }
};

View File

@ -5,6 +5,7 @@
#include "Components/SceneComponent.h"
#include "Structs/MujocoJoint.h"
#include "Misc/UniqueNamedComponent.h"
#include "Components/MujocoClassComponent.h"
#include "MujocoJointComponent.generated.h"
UCLASS(Blueprintable, ClassGroup = (Rendering, Common), hidecategories = (Object, Activation, "Components|Activation"), editinlinenew, meta = (BlueprintSpawnableComponent))
@ -21,8 +22,30 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco")
FMujocoJoint Joint;
// Reference to the class component
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mujoco", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UMujocoClassComponent> ClassComponent;
// Function to set class component
UFUNCTION(BlueprintCallable, Category = "Mujoco")
void SetClassComponent(UMujocoClassComponent* NewClassComponent);
// Function to get class parameters
UFUNCTION(BlueprintCallable, Category = "Mujoco")
FString GetClassParameter(const FString& ParamName) const;
// Function to check if has class component
UFUNCTION(BlueprintCallable, Category = "Mujoco")
bool HasClassComponent() const;
#if WITH_EDITORONLY_DATA
virtual void OnComponentCreated() override;
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
#endif
protected:
virtual void BeginPlay() override;
// Called when the game starts or when spawned
virtual void OnRegister() override;
};

View File

@ -1,4 +1,4 @@
// Fill out your copyright notice in the Description page of Project Settings.
// Fill out your copyright notice in the Description page of Project Settings.
#include "Core/LuckyRobotsGameInstance.h"
@ -663,6 +663,30 @@ void ULuckyRobotsGameInstance::SetLuckyRobot(FString RobotName)
UE_LOG(LogTemp, Error, TEXT("Invalid robot name: %s"), *RobotName);
}
}
void ULuckyRobotsGameInstance::LuckyLevelOpen(FString LevelName)
{
if (LevelName.IsEmpty())
{
UE_LOG(LogTemp, Warning, TEXT("Level name is empty!"));
return;
}
// Level opening logic
if (UWorld* World = GetWorld())
{
FName LevelFName(*LevelName);
if (FPackageName::DoesPackageExist(LevelName))
{
UGameplayStatics::OpenLevel(World, FName(*LevelName));
UE_LOG(LogTemp, Log, TEXT("Opening level: %s"), *LevelName);
}
else
{
UE_LOG(LogTemp, Error, TEXT("Level '%s' does not exist!"), *LevelName);
}
}
}
void ULuckyRobotsGameInstance::LuckyRobots()
{
UEnum* EnumPtr = StaticEnum<ERobotsName>();
@ -674,7 +698,7 @@ void ULuckyRobotsGameInstance::LuckyRobots()
Name = Name.Replace(TEXT("ERobotsName::"), TEXT(""));
GameUserWidget->DoLogItemAdd("Robot:", Name, ELogItemType::Consol);
UE_LOG(LogTemp, Display, TEXT("Robot: %s"), *Name);
if (GEngine)
if (EUnrealBuildType::Development == CheckBuildConfiguration())
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, FString::Printf(TEXT("Robot selected: %s"), *Name));
}

View File

@ -327,7 +327,8 @@ public:
void SetLuckyRobot(FString RobotName);
UFUNCTION(exec)
void LuckyRobots();
UFUNCTION(Exec)
void LuckyLevelOpen(FString LevelName);
UFUNCTION(BlueprintPure)
EUnrealBuildType CheckBuildConfiguration() const;