[BP] Whats the difference between variable of class and object?

When i add a variable to my blueprint, and set the type to something like UserWidget, there are two options: Object_UserWidget or Class_UserWidget. What are the differences between the two? and why aren’t they interchangeable?

To understand classes and objects you have to understand a little bit about object oriented programming (OOP). A class is just a type. An Object is an instance of that Class which is defined by the type.

Here is one way to think of it. Say you have the plans to build a brand new 2015 Corvette (my next car). What you have is a Class. You don’t have the car you just have the plans to build one. When you instantiate (fancy programming term for build) a Class, you build the Corvette using the plans and you get an Object which is an instance of that Class. So the Object is a real Corvette that you built that actually exists. You can use a Class (the plans) to build as many Objects (the actual car) as you want. This is why the two are not interchangeable. Hope this helps!

2 Likes

Yes, i understand OOP very well, but i don’t understand how you can have a variable of the class, rather than the instance. Can you write it in C++ terms? because,

Baseperson b;
b = new BasePerson();

b is a variable of object. how do i get a variable of a class? In what case would i need it in UE4 blueprint?

You can use classes variables if you want to create an object which class is decided at runtime. In C++ you could have a UClass* variable, and construct an object based on it.

Example: You want to spawn a sword on a character, but there are multiple sword classes to choose from. You can set the class in the properties, and spawn the sword instance based on it.

Here is an example of where you might use a Class variable in blueprints:

Notice the dark purple input pin on the Create node. That pin is expecting a class variable. Also, notice that in C++ that equates to TSubclassOf UObject. This idea of class variables is something Epic came up with for UE4 and this is not a built in system in C++.

Ah thank you. That’s what i wanted to hear. I didn’t understand it because its not a C++ thing, but a UE4 thing. And I’ve been using that blueprint node for a while and never released what it was taking.

This is covered in the video tutorials: Blueprint Essentials: Object and Class Variables | 04 | v4.2 Tutorial Series | Unreal Engine - YouTube

Basically as far as Blueprint is concerned, if you have something in your scene already and you want to reference it, use an object variable. If you are creating/spawning the object in your variable, then use a class.

1 Like