Please help me understand my mistake.
I have 2 classes - MainCharacter (ACharacter) and EnemyCharacter (ACharacter)
From EnemyCharacter i can cast to MainCharacter :
AMainCharacter* MainChar = Cast<AMainCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
HealthValueMainCharacter = MainChar->Health;
and get all parameters what i want.
But if i try to cast from MainCharacter to EnemyCharacter - i have problems:
AEnemyCharacter* EnemyChar = Cast<AEnemyCharacter>(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
HealthValueEnemyCharacter = EnemyChar->Health;
may will need to use TArray…i dont understand.
Thank you for youre help!
i understand you. But i cant write/make/understand code in my situation.
Is there a specific error you are getting? Is it in the IDE (Visual Studio, X Code etc.) or is it during runtime? Do you have an output of the specific error? Is the AEnemyCharacter object controlled by another player either locally or in a networked game?
Apple inherited from Fruit and Banana inherited from Fruit. Apple can be cast to Fruit, since it is initially a Fruit. Same for Banana. But an Apple can’t become a Banana, or Banana can’t become an Apple. This is the same for your EnemyCharacter and MainCharacter.
You are casting pawn to a character
AEnemyCharacter* EnemyChar = Cast(UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
should probably be
AEnemyCharacter* EnemyChar = Cast(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
I try this. But EnemyChar Is Null too.
I’ll assume you have a main character and enemy character at the same time controlled by other players. You’ll need to get the player with the right index. Now it is always getting the player at index 0, which is (i assume) a maincharacter. Enemycharacter is probably at index 1.
I create two characters with 2 classes.
My code in widget class:
const AMainCharacter* MainChar = Cast<AMainCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(),0));
Main = MainChar->MainCharHealth;
const AEnemyCharacter* EnemyChar = Cast<AEnemyCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(),1));
Enemy = EnemyChar->EnemyCharHealth;
The enemycharacter is present when you play, right? Who posseses it? AI or a 2nd player?
Yes,he present.I wanted to make control through the AI controller, but I have not yet started this.I now do not know what to do when I create a new controller class, how to access the Enemychar class - cast failed(
But right now it is not beung possesed by a player? It just stands there? Then GetPlayerCharacter wont work. You could try with getactorsfromclass function to get all enemychar in the level in an array. Then get the health from there. There is probably another more efficient way but its too early for me to think xd
You right! I try BP variant -work. But i dont understand syntax of UGameplayStatics::GetAllActorsOfClass in c++
i try this:
TArray<AEnemyCharacter*> FoundEnemy; // i thing is wrong((
AEnemyCharacter* EnemyChar = Cast<AEnemyCharacter>( UGameplayStatics::GetAllActorsOfClass(GetWorld(), 0, FoundEnemy);
Not working…its not static Actors - i write zero (may be right)…and FoundEnemy must have parameters…i dont undrestand this sorry(
In your c++ you are getting all actors of class “0”. Replace 0 with AEnemyCharacter::Static() and all enemycharacters should be in FoundEnemy array.
From this array you should be able to get health of the enemy.
Still not sure if this is the best way but it should work
You shouldn’t need the cast anymore btw
I try. Compile failed. FoundEnemy - failed syntax…i dont understand.
could you screenshot the code
remove everything before UGameplaystatics::… , give &FoundEnemy or *FoundEnemy as parameter if there is still an error.
If & Error 2664…
If * Error 2100…