Get default value on component

I have a component that inherits from a custom C++ class which in turn inherits from UActorComponent. This component contains a Damage variable of type FVector2D, and I’d like to get the default value that is set via Blueprints.

I know I can use GetDefaultObject in C++ and GetClassDefaults in Blueprints, however both of those methods return the value that’s set on the class, not the added component “instance”.

Damage is set to 0.0f, 0.0f as a default. I then add the component to an Actor and change said default to 2.0f, 5.0f. It is this 2.0f, 5.0f that I’m looking to retrieve.

To illustrate:

I need Projectile Component’s Damage 2.0f 5.0f, as seen in the lower-right corner of the screenshot. I guess you could call it a Blueprint instance-default value.

1 Like

I may be mis-understanding this, but can’t you just get the value of Damage? Provided nothing is changing it elsewhere, it will have the default value you set in the instance.

You need to set the default values for your variables in your default constructor (the one with FObjectInitializer&) for them to get defaults in blueprints, for example in my UBuoyancyComponent class I set all variables like this:

UBuoyancyComponent::UBuoyancyComponent(const class FObjectInitializer& ObjectInitializer : Super(ObjectInitializer)
{
	//Defaults
	MeshDensity = 600.0f;
	FluidDensity = 1025.0f;
	TestPointRadius = 10.0f;
	FluidLinearDamping = 1.0f;
	FluidAngularDamping = 1.0f;

	VelocityDamper = FVector(0.1, 0.1, 0.1);
	MaxUnderwaterVelocity = 1000.f;

	StayUprightStiffness = 50.0f;
	StayUprightDamping = 5.0f;

	WaveForceMultiplier = 2.0f;
}

Doing it this way the initial values of the component will get carried over to blueprint as long as each variable has a

UPROPERTY(BlueprintReadWrite, EditAnywhere) 

decorator applied to it in the .h class.

Hope that helps!

If you can edit the values in the editor then you’re probably missing the BlueprintReadWrite or ReadOnly

    UPROPERTY(BlueprintReadWrite)

I’m trying to avoid having to set anything in c++ - the default values should be editable by designers easily and without having to change c++. What you’re suggesting sets default values on a class - I need them set on an “instance component” that’s added to an actor.

That just gives me access to the values once I have an actual instance of the component, which I don’t - it’s a projectile and is null until it gets fired from a gun. I need access to default values before instantiation, e.g. for calculating DPS by the AI to figure out which opponent is the strongest / weakest, etc.

I’m after the value of Damage before the actual component is instantiated. It’s part of a projectile actor that I only spawn once it’s actually fired from a gun. I need access to the values before that e.g. to calculate DPS by the AI.

Ah, I see. I think your best bet here would be to go data driven: .unrealengine.com/en-us/Gameplay/DataDriven

You can use data tables or use a DataAsset. In either case you can then have various parts of your game look up the values before your component is instantiated. It also has the benefit of being more maintainable.

Doing it this way means that whatever class is trying to get the value can either look at the contents of the datatable or data asset. Similarly, when your component is instantiated it can also look up the values from that same data table or asset. Assuming you’ve loaded the datatable or data asset, your values are always accessible.

Yeah, I’ll probably have to resort to data tables, eventually. Appreciate the follow-up, but I want to wait & see if there’s really no way to access those defaults like I want. Cheers.

I’m a little confused that you would add a ProjectileComponent to a LaserProjectile Class, isn’t that double?

Anyways, your problem is most likely unsolvable, unless you use a Datatable like Phil_me_up suggested.

You want to get the Instance’s default value, without creating the instance, which is not possible.
Since you can not edit Instance-Defaults, unless you created an instance (dropped it inside the editor or CreateDefaultSubobject/other functions like this)

You can only get the Archetypes default, or the Class’ default.


What you can do however, is to make the damage-variable “Exposed on Spawn”, which enables you to set it at the time of firing, based on a value passed into the Constructor. This value is part of the LaserProjectile-Actor, and exists before the component is instantiated, can be modified at runtime or by designers in the Editor, etc… This way (as long as you have instantiated the LaserProjectile, otherwise same problem again) you can change this value on a per-instance base.

EDIT: If you do not have the LaserProjectile-Actor instantiated before, you could make the Value it passes to its ProjectileComponent “exposed on spawn”, too. But your firing Actor, the one who spawns the LaserProjectile, would be in charge of the damage then, which I suspect is not what you want. It’s a design/architecture-problem of your code.

As a workaround I do put Damage on the actor that houses the projectile component. Obviously it’s a workaround I don’t like. I don’t believe my code suffers from a design problem: any actor may possess the projectile trait, also, any actor may act as a weapon by having the appropriate weapon component added to it. The game uses such a component-based design and it’s been very efficient so far. Anyway, I will accept this as an unfortunate limitation of the engine and will probably migrate to a data table-based approach as someone has suggested above. Thanks for your input.