Why can't I assign a default value to a object type variable ?

Hi,

I ran into a problem with blueprint variables.
Here is what I did:

  1. Create a blueprint class based on “object”.
  2. Create a blueprint based on “Game Instance”
  3. Add a object reference variable to the game instance class and make it of the type of object created in step 1.
  4. Select your object class blueprint in the content browser.
  5. In the game instance class, click on the little arrow adjacent to the variable default value tile.
  • Observe that the object reference does not get applied.

This is the first time I encountered this not working. Usually I can assign default values to materials, static meshes, datatables and whatnot…
Why is this not working here.

Did I miss something or is this a bug?

Cheers,
Klaus

Assignment works for me exactly as you described -

@OptimisticMonkey

Hmm. What version are you using? Im using 4.24.2.

When I look for the BP class in the dropdown, its not even there…

See … :frowning:

Cheers,
Klaus

@CosmicLobster

Hmm. But other blueprints are also just “defined” and I can still assign default values to variables in them…
So is Game Instance a special case then?

For example: I have in my Almanach object class blueprint (not an actor class) a variable of type datatable. I can assign a default value to that even no Almanach instance is actually in the game (yet).

No Almanach BP is anywhere in the level. I just created the asset and assigned the datatable to the variable. Why does it work here?

I expected to be able to do the same with the almanach Bp in the game instance class…

Greetings, @KVogler! I believe I can help.

When you create a new Blueprint in the Content Browser, you are not creating an instance of an object; you are creating a new **class, **the same way you would if you were programming in C++. It’s a definition of an object that can exist in your game somewhere, not an object in and of itself.

When you create a new Data Table, you aren’t making a Blueprint class that extends from Data Tables, you are creating an **instance **of a Data Table that uses some structure as its table row, and appears as an asset that can be referenced in the content browser.

Thus, if we’ve got two classes called GameInstanceBP and ObjectBP, and we want to have a reference to an object of type ObjectBP inside of our GameInstanceBP, here’s the extra steps you can add to make this implementation make sense:

  1. Create a new variable called ObjectClass that has a type Class Reference instead of Object Reference. This makes it a variable that points to a class type rather than an instance of an object. Compile the Blueprint for the added variable to take effect.

  1. In the **Details **panel, you will see a dropdown for the Default Value. Click it and you can select ObjectBP or any class derived from ObjectBP.

  1. Make sure that the Default Value for ObjectClass is not set to None. Then, create a second variable called ObjectInstance, change its type to ObjectBP, but make this one an Object Reference. Your Variables panel will look like this:

ObjectVars.png
4. Create an Init event inside of your GameInstanceBP’s Event Graph. Then, create a Construct Object from Class node, attach it to Init with the Execution Pin, and drag the ObjectClass variable into the graph with a Get node. Drag ObjectInstance into the graph with a Set node, and use the Return Value to set the value of ObjectInstance. Get a reference to Self and attach that to the Outer pin in order to make GameInstanceBP the owner of ObjectInstance.

When your Game Instance is created (provided that your game is set to use it in Project Settings), ObjectInstance will be filled with an instance of the class you provided in ObjectClass.

If you like, you can eschew the ObjectClass variable completely and just use the dropdown on the Construct Object from Class node to select what class you want to use.


One way or another, the Object Instance, once added, will stick around as long as your Game Instance does. The use of Construct Object is equivalent to using the **New **keyword in C++ to create a new instance of an object.

Hope that helps. :slight_smile:

4 Likes

https://forums.unrealengine.com/filedata/fetch?id=1715104

Content Browser’s thumbnails should follow the same color convention because the lack of color contributes to this confusion.

@MikhailPrinke

Thank you very much for your detailed explanations. It really helped me sorting out a few wrong assumptions that I had. :slight_smile:

I always thought all assets in the content browsers are just mere definitions. I thought for example a static mesh does only exist as an instance once I drag it into the level.
It never ocurred to me that they already exist as kind of default instance.

Just one thing I still need to wrap my head around to fully understand what is being a mere class (definition) and what is an instance in the editor:
When I create a class in C++ code, I noticed that a CDO instance is created automatically when the project loads up.
So, is there no default object instantitated for my ObjectBP?

Thanks again for your great help :slight_smile:

Cheers,
Klaus

ps:

[USER=“434”]BrUnO XaVIeR[/USER]

Sounds good.
Even better: Simply only allow “class” as an option in the variable type dropdown for object types. (Or not?)

The thing is developers can specify which label color an object has in Asset Browser, but when you create a Blueprint the Editor erase that custom color and apply the default cyan background that gives impression the blueprint is an object (wrong visual info).

Change the object referance to a soft object referance. then make sure to put an is valid check on the object before you use it. just for safety.