# How to access function from Character from GameMode?

**URL:** <https://forums.unrealengine.com/t/how-to-access-function-from-character-from-gamemode/348857>\
**Category:** Character & Animation\
**Tags:** gamemode, cast-failed, calling-a-function, unreal-engine, CPP, question\
**Created:** [February 27, 2016, 12:55pm UTC](https://forums.unrealengine.com/t/how-to-access-function-from-character-from-gamemode/348857 "2016-02-27T12:55:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_44632261](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_44632261](https://forums.unrealengine.com/u/anonymous_user_44632261)\
**Post date:** [February 27, 2016, 12:55pm UTC](https://forums.unrealengine.com/t/how-to-access-function-from-character-from-gamemode/348857/1 "2016-02-27T12:55:45Z")

</div>

Hello,

I’d like to call the function GetNumResponses (inside my Character-class; gets the number of space-presses that the character has received so far) from my GameMode-class. However, that does not work. Could you please help me with that? Any help is really appreciated!

This is what I have so far:

* * *

PD6GameMode.cpp

void APD6GameMode::Tick(float DeltaSeconds)  
{  
Super::Tick(DeltaSeconds);

```
// get number of space presses that the character has received so far
APD6Character* TestPD6Character = Cast<APD6Character>(ACharacter); // ERROR: type name is not allowed
NumResponses = TestPD6Character->GetNumResponses();

```

}

* * *

PD6Character.h

UCLASS()  
class PD6\_API APD6Character : public ACharacter  
{  
GENERATED\_BODY()

public:  
// other code

```
    // public way to access the number of responses given by the character
    float GetNumResponses();

```

protected:  
// NumResponses counts the space-presses  
UPROPERTY()  
float NumResponses;

```
// increase NumResponses when space key is pressed
UFUNCTION()
	void SetNumResponses();

```

}

* * *

PD6Character.cpp

// Sets default values  
APD6Character::APD6Character()  
{  
PrimaryActorTick.bCanEverTick = true;  
NumResponses = 0.f;  
}

// Called to bind functionality to input  
void APD6Character::SetupPlayerInputComponent(class UInputComponent\* InputComponent)  
{  
InputComponent-\>BindAction(“GoalResponse”, IE\_Pressed, this, &APD6Character::SetNumResponses);  
}

void APD6Character::SetNumResponses()  
{  
NumResponses = NumResponses + 1.f;  
}

float APD6Character::GetNumResponses()  
{  
return NumResponses;  
}

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_104c9a84](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_104c9a84](https://forums.unrealengine.com/u/anonymous_user_104c9a84)\
**Post date:** [February 27, 2016, 5:13pm UTC](https://forums.unrealengine.com/t/how-to-access-function-from-character-from-gamemode/348857/2 "2016-02-27T17:13:16Z")

</div>

Assuming that your character is a controlled by the player, you could get it like this …

```
ACharacter* MyCharacter = GetWorld()->GetFirstPlayerController()->GetCharacter();
APD6Character* TestPD6Character = CastChecked<APD6Character>(MyCharacter);

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_44632261](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_44632261](https://forums.unrealengine.com/u/anonymous_user_44632261)\
**Post date:** [February 27, 2016, 9:07pm UTC](https://forums.unrealengine.com/t/how-to-access-function-from-character-from-gamemode/348857/3 "2016-02-27T21:07:26Z")

</div>

Thank you very much, halifacx!! That solved the problem.
