# how can i simply move a UI variable to an c++ class

**URL:** <https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480>\
**Category:** C++\
**Tags:** unreal-engine, CPP, Blueprint, question\
**Created:** [April 2, 2023, 10:04pm UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480 "2023-04-02T22:04:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![emir143](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@emir143](https://forums.unrealengine.com/u/emir143)\
**Post date:** [April 2, 2023, 10:04pm UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480/1 "2023-04-02T22:04:00Z")

</div>

so im trying to make a inventory that was made with blueprint and move it to c++ but the problem is, is that i dont know how to move it, and for exmaple now i’m thinking of just moving a UI variable to a c++ class (inventoryComponent) but i dont know how. im really new to this so bare with me if i dont understand

---

<div class="post-metadata">

**Author:** ![mnelenpridumivat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mnelenpridumivat/32/1194643_2.png) [@mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat)\
**Post date:** [April 2, 2023, 10:33pm UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480/2 "2023-04-02T22:33:15Z")

</div>

Well, if it’s a widget component (Text, CanvasPanel or similar, even other UserWidget), you can create in C++ a variable with same type and name, just need to make it BindWidget. For example, if you have a Text widget component with name “TextName” in you widget and you want to move this variable to C++ class, you’ll need to create in C++:

```auto
UPROPERTY(<your extra specifiers like BlueprintReadWrite>, meta=(BindWidget))
UTextBlock* TextName;

```

If it’s an ordinary blueprint type (int, string, Actor reference…), just create UPROPERTY field in C++ and replace blueprint variable with your C++ class field.

---

<div class="post-metadata">

**Author:** ![emir143](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@emir143](https://forums.unrealengine.com/u/emir143)\
**Post date:** [April 2, 2023, 10:44pm UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480/3 "2023-04-02T22:44:32Z")

</div>

> [@mnelenpridumivat](#):
>
> If it’s an ordinary blueprint type (int, string, Actor reference…), just create UPROPERTY field in C++ and replace blueprint var

well i was thinking of taking a whole UI setup (inventory Ui ) to be a variable in c++ so i can access it, or basically im trying to move a variable i have in blueprint to c++. something like this

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/b/d/ebdb8bbecd11a0b0cff0d7dd23fd7e7d024f1361.png)

---

<div class="post-metadata">

**Author:** ![mnelenpridumivat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mnelenpridumivat/32/1194643_2.png) [@mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat)\
**Post date:** [April 2, 2023, 11:07pm UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480/4 "2023-04-02T23:07:25Z")

</div>

If I’m inderstanding you correctly, you want to access your 3 variables (button, grid and custom widget) in C++. That’s what BindWidget is for: in C++ you creates a UPROPERTY with BindWidget meta specifier (just creat a field, don’t init them) and after that creates in BP your widget component in design page (or just create in C++ fields, in your case).  
It’s OK with button and grid element, but you can’t do it easily with Parent variable, because you can’t use blueprint as C++ type - you need to create a field of parent type of that variable (if UI\_W widget inherits from UUserWidget, you can use only UUserWidget as a type of this variable in C++, but if it inherits from a custom widget class inherited from UUserWidget, you can use a custom class).

---

<div class="post-metadata">

**Author:** ![emir143](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@emir143](https://forums.unrealengine.com/u/emir143)\
**Post date:** [April 4, 2023, 5:22am UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480/5 "2023-04-04T05:22:06Z")

</div>

Sorry for answering late  
ok so if i understand correctly about making a variable of UI, should i write something of

```auto
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UUserWidget UI_W;
```

---

<div class="post-metadata">

**Author:** ![mnelenpridumivat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mnelenpridumivat/32/1194643_2.png) [@mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat)\
**Post date:** [April 4, 2023, 5:25am UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480/6 "2023-04-04T05:25:24Z")

</div>

Yes, except one thing: you need to make a pointer:

```auto
UPROPERTY(BlueprintReadWrite, meta = (BindWidget))
UUserWidget* UI_W; // you need to make a pointer, not an ordinary variable

```

---

<div class="post-metadata">

**Author:** ![emir143](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@emir143](https://forums.unrealengine.com/u/emir143)\
**Post date:** [April 4, 2023, 5:37am UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480/7 "2023-04-04T05:37:45Z")

</div>

aha ok, and it doesnt show in the blueprint variable section when typing this but instead i have to find it or search for it in the blueprint search bar, right?

also another thing im little confused about is, its about the inventoryComponent class, so if i want to have my inventoryComponenet class to be connected with either my charcter or an object/actor, like how its done with blueprint. you open up your characters blueprint, and in component you add the component you have made, and its connected.

so is including the cpp class enough for it to be connected or do i also need to call functions from the inventoryComponent class?  
sorry if its sounds confusing

---

<div class="post-metadata">

**Author:** ![mnelenpridumivat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mnelenpridumivat/32/1194643_2.png) [@mnelenpridumivat](https://forums.unrealengine.com/u/mnelenpridumivat)\
**Post date:** [April 4, 2023, 8:19pm UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480/8 "2023-04-04T20:19:38Z")

</div>

> [@emir143](#):
>
> it doesnt show in the blueprint variable section when typing this but instead i have to find it or search for it in the blueprint search bar

Yes, inherited fields don’t shows in variable sections, but they’re still accessible via blueprint search bar (if any of BlueprintReadWrite or BlueprintReadOnly modifiers used).

> [@emir143](#):
>
> so is including the cpp class enough for it to be connected or do i also need to call functions from the inventoryComponent class?

Such component is a UActorComponent child, you need to init it like other actor components (Static/SkeletalMeshComponents, for example): make a UPROPERTY field and init default components in constructor.

---

<div class="post-metadata">

**Author:** ![emir143](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@emir143](https://forums.unrealengine.com/u/emir143)\
**Post date:** [April 5, 2023, 6:07pm UTC](https://forums.unrealengine.com/t/how-can-i-simply-move-a-ui-variable-to-an-c-class/839480/9 "2023-04-05T18:07:05Z")

</div>

oh i see now, that cleared alot of things for me now, i think thats all for the questions that i have, thank you for your time 🙂
