Creating a survival system?

Hi there! I have spent a while on UE4 (I am fairly new) and I’m starting to get the gist of the workflow. I used to work on Unity, but I really wanted to work on Unreal because I think it’s time for an upgrade :p. Anyways, there was something that I wanted to create that I managed to get in Unity, and I’m wondering if I can replicate it here?

First things first, I am working in C++. I know the language decently so I shouldn’t have a problem with that.

Ok so in Unity, I made this thing called the Survival system. Think of a typical survival game -> there’s always health, hunger, stamina etc. All of these share some common values like their currentValue, or their maxValue, or their depletionRate etc. We can think of each of these as “Subsystems”. In my survival system I created a parent class called BaseSystem. This class essentially has all the variables that each subsystem will contain. Then, for each subsystem (like health for example), I create a new class which inherits from BaseSystem. Ideally, all subsystems will derive from BaseSystem. In this particular subsystem, I can define more unique variables and methods.

This approach has been very flexible with me in Unity. Once I have made all the subsystems, I create another class called Character. In unity, this was a component that would be attached to the player object. Character will be the place where I declare what subsystems I’ll be using. Then, I can call their methods in Start or Update.

I hope to do something similar in Unreal. I am starting off from the Third Person Template that you can choose from when you create a project in Unreal. My question is, will my approach in unity be the same?

What I know so far is that when I make the subsystem classes and BaseSystem, I want them to derive from UObject. The class called Character that I made in Unity will be replicated in the form of an ActorComponent. Is this a good start?
Additionally, I am also concerned about this : In Unity, I can edit the values for each subsystem declared within the inspector. Unreal has a similar window : the details panel. I know that you can expose properties by calling UPROPERTY on an individual variable. If I do this on classes, will all the individual variables of that class be exposed?

Thanks and let me know if I have to be more clear!

Alright, so in sections:

This is usually not a problema at all. It only depends on your willingness, saviness and time which you are willing to put into the project. UE4 is far more exposed and dynamic than Unity, so its all about yourself, your creativity and knowledge.

Should work just fine. Personally, I have worked with similar systems for overall character stats, and they worked just fine, we called them StatsSet.

If you want to replicate the StatsSet, then yes, derive them from UObject, if not, then its fine to use an empty class. Just remember you’ll have to GC any objects you dynamically create since it doesn’t have UE4’s GC. In principle, you should derive from UObject, but if you want to avoid the overhead…

Yes, you can expose each individual variable using the UPROPERTY macro and selecting the specifiers (consult the documentation/wiki on this subject). If you do it on classes, it means your class will be exposed to blueprints, meaning, you can instantiate them and manipulate its objects. Its purpose is not to expose all of its objects, just think of the amount of unecessary variables you would expose to the editor by doing so.

Hope I helped and I haven’t lied :stuck_out_tongue:

To make sure I understand your point, what you’re trying to achieve is:



class Health : public BaseSystem
class Heat : public BaseSystem


Where BaseSystem is something like:



UCLASS(ABSTRACT)
class BaseSystem : public UObject
{
protected:
 float depletionRate;
 float currentValue;
 float maxValue;

public:
 BaseSystem();
}


Thanks! I am still confused on one thing though, so let me clear myself up.

Say I have a c++ class called MyObject which inherits from UObject right? Now let’s say this class has a variable of type int32 called someNumber. I also have a UPROPERTY(EditAnywhere) macro on top of it. The class “MyObject.h” has this code:



#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "MyObject.generated.h"

/**
 * 
 */
UCLASS()
class LEARNING_API UMyObject : public UObject
{
	GENERATED_BODY()
	
public:
	UMyObject();

	UPROPERTY(EditAnywhere, Category = "MyObject's fields")
		int32 someNumber = 0;
	
};

Now let’s say I have an ActorComponent class called TestComponent. This class MUST be attached to an actor in my scene.

My goal is to make it so that TestComponent can expose MyObject’s fields. Is this possible? What is the correct approach? Here was my attempt of the TestComponent.h file - of course, this wouldn’t work. It would be expecting an actual instance of MyObject instead. I am quite confused here…


#pragma once

#include "MyObject.h"
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "TestComponent.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class LEARNING_API UTestComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UTestComponent();

	UPROPERTY(EditAnywhere, Category = "Something")
	UMyObject* myObject;

protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

		
	
};

Essentially in Unity, I was able to accomplish this. If I made a class that had some variables and made sure to set them as Serializable, then if i create an instance of that object inside a script-component (in which that script-component get’s attached to a game object), I can access those variables.

I found that this thread answered my questions:

Glad to hear that!

To be honest I was unaware of the existence of such flag. And I don’t really see a use for ActorComponents for most engines, guess I’ll have to dig deeper and understand them better.

Good luck!