Can you post a snippet of where you are actually pasting the code in, and what the error message is?
Anyway this is really straight forward.
Firstly you wouldn’t want this in the Character Controller, as only character(s) that are possessed by the player(s) would have controllers to begin with, and as you said, you want this on all NPC’s and other objects. They could have AI controllers, but based from what you wrote it sounds like you want some inanimate objects to have some sort of life variable too. Therefore controllers will not work.
For Characters you’ll want to put these properties in their Character.h file, which .h is the only location you can declare variables and assign them property specifiers (ie UPROPERTY(…))
But since you want this to be an instance for NPC’s and perhaps other objects other than pawns, you can make a UObject Class, lets say its called “UMyLifeObject”, so itll look like this
#include "Object.h"
#include "MyLifeObject.generated.h"
/**
*
*/
UCLASS(BlueprintType)
class TDEXAMPLE_API UMyLifeObject : public UObject
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Life Stats")
float Life;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Life Stats")
float LifeRegen;
public:
//Next you will need a basic constructor
UMyLifeObject();
//Finally a function to instantiate this property, this is optional only if you are planning to do this in blueprints
UFUNCTION(BlueprintCallable, Category = "Life Stats")
static UMyLifeObject* ConstructUMyLifeObject(float LifeValue, float LifeRegenValue);
};
in the .cpp
#include "TDExample.h"
#include "MyLifeObject.h"
UMyLifeObject::UMyLifeObject(){
//Default constructor code here, but lets say...
Life = 100.f;
LifeRegen = 50.f;
}
UMyLifeObject* UMyLifeObject::ConstructMyLifeObject(float LifeValue, float LifeRegenValue){
UMyLifeObject* LifeObj = NewObject<MyLifeObject>();
LifeObj->Life = LifeValue;
LifeObj->LifeRegen = LifeRegenValue;
return LifeObj;
}
So now to actually instantiate it and add it to an NPC can be tricky.
In C++ You could just add UMyLifeObject as a property to that NPC’s .h file, and instantiate it by calling NewObject() in the NPC’s constructor.
Or if you are doing this in blueprints make note of the
UCLASS(BlueprintType)
specifier in the above snippet. You need to specify this is a BlueprintType in order for this class to be declared as a variable in blueprints.
You then go into the NPC’s blueprint class, add a variable of type UMyLifeObject (though it will appear as “My Life Object” in the list)
In the end you should get something like this:
It be best if you could get this to work in an actor component. I haven’t tried it, but in theory it should work, and then all you would need to do is add this component to all your NPC blueprints.
But personally I would just go the C++ route, and just inherit from some base NPC class that does this in the base constructor. That way I know it will always be instantiated in any class that would inherit from it.
I recommend you check out UE’s youtube channel. The programming tutorial helped me get going.