Download

Error c++/Blueprints interaction

Hello,
I just tried to make a life system in UE4 and I have a problem with UFUNCTION and UPROPERTY to return a variable or make a function which return the variable in blueprint.
So I made a Life class here :


#pragma once

class Life {
	public :
		Life();
		virtual ~Life();

		UFUNCTION(BlueprintCallable, Category = "Life")
		float getLife();
		void setLife(float nb);
		void setMax(float nb);

		UFUNCTION(BlueprintCallable, Category = "Life")
		void addLife(float nb);

		UFUNCTION(BlueprintCallable, Category = "Life")
		void removeLife(float nb);

	protected :
		float mLife;
		float mMax;
};




#include "Life.h"

Life::Life() {
	mLife = 100.0f;
	mMax = 100.0f;
}


Life::~Life() {

}

float Life::getLife() {
	return mLife;
}

void Life::setLife(float nb) {
	mLife = nb;
}

void Life::setMax(float nb) {
	mMax = nb;
}

void Life::addLife(float nb) {
	if (mLife < mMax)
		mLife += nb;

	if (mLife > mMax)
		mLife = mMax;
}

void Life::removeLife(float nb) {
	if (mLife > 0)
		mLife -= nb;

	if (mLife < 0)
		mLife = 0.0f;
}

And I add this function in my Character class :


Life* AProfectusTripCharacter::getLife() {
	return mVie;
}

And in the character header in protected :


Life* mVie;

and in public :


UFUNCTION(BlueprintCallable, Category = "Life")
		Life* getLife();

But when I call UFUNCTION begore “Life* getLife()” I had an error with the compilation and I need it to access to the Life system in blueprints :
Erreur 1 error : In ProfectusTripCharacter: Unrecognized type ‘Life’ C:\Users\Styvak\Documents\Unreal Projects\ProfectusTrip\Source\ProfectusTrip\ProfectusTripCharacter.h 45 1 ProfectusTrip

Can you help me ?
Thank you in advance,
Best regards,
Styvak

As a beginner myself i could be wrong of course, but…did you actually include the Life header in your character?

So in your ProfectusTripCharacter.h:



#include "Life.h"

(other includes...)


Cheers,
Indy

Yes, I include the Life header in my character it’s why I don’t understand this error :confused:
Thank you,
Styvak

There are a lot of things wrong here.First your class is not wrapped around UCLASS().Second if you added Life.h and Life.cpp through VS,which seems you did, UE wont find your files because it goes to Intermediate folder.You should add your code to “Source” folder.And your classes should include a “Class.generated.h” which is automatically generated which includes some information about your class for the engine.You can do it by right clicking your .uproject then choose generate visual studio files.Again,I recommend creating classes with Add Code to solution within the editor.

I tried to make it with Add Code to solution, so my source code is like this :


#pragma once

#include "GameFramework/PlayerState.h"
#include "Life.generated.h"

/**
 * 
 */
UCLASS()

class ALife : public APlayerState {
	GENERATED_UCLASS_BODY()

	UFUNCTION(BlueprintCallable, Category = "Life")
	void addLife(float nb);

	UFUNCTION(BlueprintCallable, Category = "Life")
	void removeLife(float nb);
	
	protected :
		float mLife;
		float mLifeMax;
};



#include "ProfectusTrip.h"
#include "Life.h"


ALife::ALife(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP) {
	mLife = 100;
	mLifeMax = 100;
}

void ALife::addLife(float nb) {
	if (mLife < mLifeMax)
		mLife += nb;

	if (mLife > mLifeMax)
		mLife = mLifeMax;
}

void ALife::removeLife(float nb) {
	if (mLife > 0)
		mLife -= nb;

	if (mLife < 0)
		mLife = 0;
}

And in my Character Class : in the cpp :


ALife* AProfectusTripCharacter::getLife() {
	return mLife;
}

In the header :


UFUNCTION(BlueprintCallable, BlueprintPure, Category = "Life")
	ALife* getLife();

protected:

	ALife* mLife;

And I haven’t any error so thank you very much but when I use “Get Player Character” in blueprint I don’t have my getLife function :confused:
Thank you very much,
Styvak

Just to be sure, when you get your Character Class in Blueprint, do you actually cast it to your own class?

I think you should try to add UPROPERTY() on top of that line with pointer to your class.

I wouldn’t have declared ‘health’ as an entire class. I would make it either a Component subclass or add it as a float to your Pawn subclass. Checkout the shooter example. Health is a replicated property on their custom subclass. Looks like you got a lot of good posts to help you out. Good luck