# How to declare vars in C++

**URL:** <https://forums.unrealengine.com/t/how-to-declare-vars-in-c/486550>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [July 24, 2021, 2:16am UTC](https://forums.unrealengine.com/t/how-to-declare-vars-in-c/486550 "2021-07-24T02:16:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nstalker0829](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nstalker0829/32/88711_2.png) [@Nstalker0829](https://forums.unrealengine.com/u/Nstalker0829)\
**Post date:** [July 24, 2021, 2:16am UTC](https://forums.unrealengine.com/t/how-to-declare-vars-in-c/486550/1 "2021-07-24T02:16:22Z")

</div>

I want to declare a var in C++,as like declare a var in blueprints.But I don’t know how to declare ealisy a blueprint-Objectvar in C++,just like this image.

![344766-4-hkn4digu3s5jjjqz6.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/a/8/da86fe4dbf9a585cf4d0631ff2b1227840ee4bc3.png)

---

<div class="post-metadata">

**Author:** ![DevinSherry](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/devinsherry/32/1101099_2.png) [@DevinSherry](https://forums.unrealengine.com/u/DevinSherry)\
**Post date:** [July 24, 2021, 6:50am UTC](https://forums.unrealengine.com/t/how-to-declare-vars-in-c/486550/2 "2021-07-24T06:50:31Z")

</div>

You declare variables within the header (.h) of the class in which you want the variable to exist. Here are some examples from within different classes of my project:

```
/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* SpringArm;

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Weapon)
TArray<TSubclassOf<class AHeroItemWeaponBase>> DefaultWeapons;

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Fox Logic")
FVector CalledPosition = FVector(0.0f);

```

![344778-springarm.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/c/3/cc3d6a0a98a2d26cf7d0e0d8fe06f564577c66ff.png)

![344779-weapons.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/c/b/5cb0f07f06a5f83c89b8dad1ff82988feab33e0f.png)

![344780-vector.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/e/f/3ef1f25bdbc9c298c651887214b6fd371d61fc50.png)

These are examples of different types of variables that can be declared within your header file. Depending on the type of variable, your code will look different. There are a few parts to declaring variables:

1. UPROPERTY - A set of properties that the variable has; things such as accessibility, categories, display names, and others.
2. The variable type. FVector, AActor\*, custom classes, etc. It is important to include ‘class’ when defining the variable, or #include the class in question within the header file; otherwise there will be compilation issues.
3. Defining a default value. Depending on the variable type, you may want to define here a default value; such as the case of the CalledPosition FVector example.

You can learn more about UPROPERTY here: [Properties | Unreal Engine Documentation](https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScripting/GameplayArchitecture/Properties/)

Here is also a Youtube tutorial: [Day 2 - UE4 c++ Variables and methods - YouTube](https://www.youtube.com/watch?v=PJF_PZ4pF1o)

In cases of creating a component, after you declare the component in your header file, you will also need to create the object in the construction script. Here are some helpful links to give more information:

[What is the correct way to create and add components at runtime ? - #8 by JamesMugford](https://forums.unrealengine.com/t/what-is-the-correct-way-to-create-and-add-components-at-runtime/15605/8)

  
I hope this helps, and good luck.
