Hello! Visual Studio does not compile my project. Am I incorrectly adding two classes to one CPP file?
Your classes must be inherited from UObject to be able to use reflection via UCLASS.
You seriously need to look into the basic documentation of Unreal Engine. You are doing everything wrong. This isn’t how you’re supposed to use Unreal. You seem to be doing everything in the most C way possible.
- As mentioned, a UCLASS needs to a UObject, which requires the correct include file.
- All UObject classes must be prefixed with a U. Similarly, all non-UObject classes must be prefixed with an F - unless it’s a template class, then it is prefixed with a T.
- TArray is the only form of array you should be using, not raw C arrays.
- Strings should be an FString, not a char array.
- You should be using Unreal’s integer types instead of Cs: uint8, int8, uint16, int16, uint32, int32, uint64, int64. This is coz default C integer types are different sizes on different platforms.
- (this is more of a design choice) But avoid using static like this. There’s so many better ways to do this. Not sure what it is exactly you’re trying to do but looking into Subsystems (GameInstanceSubsystem, WorldSubsystem, LocalPlayerSubsystem) could be a good start.
- Why are you not using FDateTime for your time variable?
- UObjects depend on Unreal’s Garbage Collection and thus shall only be referenced via a raw pointer or a TObjectPtr - not by value. If you want value or have a more C-style control over memory, look into making PlayerProfile into a USTRUCT instead. Note: BP cannot access USTRUCTs by pointer - only by pointer or reference (huge limitation imo).
- If sticking with UObject for PlayerProfile, you need to mark the PlayerProfilesArray with a UPROPERTY, otherwise, Unreal’s Garbage Collection isn’t going to consider the pointer lying in the array as a ‘reference’ to the object, and will destroy it if nothing else is referencing that object.
I’m surprised your VS isn’t telling you any of this.
Here’s a quick example I made. You don’t need to copy exactly but I hope it gives you an idea on what Unreal code is supposed to look like.
USTRUCT(BlueprintType)
struct FPlayerProfile
{
GENERATED_BODY()
// Add UPROPERTIES if you want BP access.
// BP cannot access Functions in a USTRUCT! But you can make a static BlueprintFunctionLibrary that takes a USTRUCT as a parameter. Keep this in mind.
FString PlayerName;
FDateTime CurrentDateTime;
int32 CompletedLevel;
int32 PointsScored;
}
UCLASS()
class UPlayerProfileSubsystem : public UGameInstanceSubsystem /* ULocalPlayerSubsystem if you want splitscreen support but lose the 'singleton'-ness */
{
GENERATED_BODY()
// I would use Getters/Setters but up to you.
public:
UPROPERTY(BlueprintReadOnly, Category=PlayerProfile)
int32 NumOfPlayerProfiles;
UPROPERTY(BlueprintReadOnly, Category=PlayerProfile)
int32 CurrentPlayerProfileIndex;
UPROPERTY(BlueprintReadOnly, Category=PlayerProfile)
TArray<FPlayerProfile> PlayerProfiles;
}
To access a GameInstanceSubsystem, you can do GetWorld()->GetGameInstance()->GetSubsystem<UPlayerProfileSubsystem>()
.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.