# How can I store my character reference in a variable? c++

**URL:** <https://forums.unrealengine.com/t/how-can-i-store-my-character-reference-in-a-variable-c/605851>\
**Category:** C++\
**Tags:** unreal-engine, CPP\
**Created:** [July 15, 2022, 8:26pm UTC](https://forums.unrealengine.com/t/how-can-i-store-my-character-reference-in-a-variable-c/605851 "2022-07-15T20:26:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Makadi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/makadi/32/553076_2.png) [@Makadi](https://forums.unrealengine.com/u/Makadi)\
**Post date:** [July 15, 2022, 8:26pm UTC](https://forums.unrealengine.com/t/how-can-i-store-my-character-reference-in-a-variable-c/605851/1 "2022-07-15T20:26:46Z")

</div>

how can I store my character reference in a variable?  
every thing works fine when I’m using AController::GetCharacter();  
but when I’m trying to store the character in a variable  
like `AMyProjectCharacter* myCharacter = Cast<AMyProjectCharacter>(AController::GetCharacter());`  
or `AMyProjectCharacter * myCharacter = AController::GetCharacter();`  
I’m getting build errors  
sow how can I do it?  
and is it better to keep using `AController::GetCharacter();` ?  
in terms of performance?

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [July 15, 2022, 9:06pm UTC](https://forums.unrealengine.com/t/how-can-i-store-my-character-reference-in-a-variable-c/605851/2 "2022-07-15T21:06:35Z")

</div>

> [@Makadi](#):
>
> AController::GetCharacter()

You should keep using this. Reason is that the character pointer is only pointing towards a character while the controller possesses one. If you store a pointer in another class you will have to keep that pointer updated manually when the controller possesses, unpossesses one which is messy.

But to answer the post title “How can I store my character reference in a variable? c++”:

```auto
ACharacter* MyCharacter = MyController->GetCharacter();

```

The reason your line is not working is because you are calling AController::GetCharacter which attempts to call GetCharacter without an instance of a controller. This way of calling a method is only valid if the method is static. Instead you must first get a pointer to the controller you want (AController\* MyController = …) and call GetCharacter on MyController.

This is also invalid:

```auto
 AMyProjectCharacter* myCharacter = Cast(AController::GetCharacter());

```

Because you must cast to something ( \<\> ):

```auto
 AMyProjectCharacter* MyProjectCharacter = Cast<AMyProjectCharacter>(MyCharacter);

```

---

<div class="post-metadata">

**Author:** ![Makadi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/makadi/32/553076_2.png) [@Makadi](https://forums.unrealengine.com/u/Makadi)\
**Post date:** [July 15, 2022, 9:15pm UTC](https://forums.unrealengine.com/t/how-can-i-store-my-character-reference-in-a-variable-c/605851/3 "2022-07-15T21:15:55Z")

</div>

> [@Roy\_Wierer.Seda145](#):
>
> AController::GetCharacter()  
> You should keep using this

and what about casting to get access to myCharacter functions should I cast every time or store the character once in a variable?

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [July 15, 2022, 9:27pm UTC](https://forums.unrealengine.com/t/how-can-i-store-my-character-reference-in-a-variable-c/605851/4 "2022-07-15T21:27:43Z")

</div>

You can Cast when you need it, no need to store it in a new variable. There will be no performance difference.
