Download

How to refrence blueprint and spawn from code?

How do you get a reference to a blueprint in C++. How can you then access the components of that blueprint? I know you can you construction Helpers but after that I am lost.

Sorry about the typo in the thread name, no way to change it now.

Heh, we basically have the same question.

Hey,
one can get reference to a blueprint using ConstructionHelper and a hardcoded path to an asset:

static ConstructorHelpers::FObjectFinder<UBlueprint> MyBlueprint(TEXT("/Game/Blueprints/Pawns/MyPawn"));

or from class:

UBlueprintGeneratedClass MyBGClass = …;
UBlueprint* MyBP = Cast<UBlueprint>(MyBGClass->ClassGeneratedBy);*

Component templates are in UBlueprint::ComponentTemplates.

UBluepirnt class should be used/referenced only in Editor. Blueprints should be ‘editor only’ assets soon. So they they hopefully wont be accessible from ‘runtime game’ code.

Component templates can be also obtained from a UClass object, since their outer object is always a class. See functions: GetObjectsWithOuter and FindObjectWithOuter.

I tried that already and the editor crashes, are you saying its not possible?

It’s possible. What exactly did you try, what is the callstack?

I have a blueprint, inside that blueprint is a static mesh and a constructor that sets the material to a random material.
I want to spawn that blueprint from C++. I use spawnActor.

When I try to spawn the blueprint the editor crashes.
I guess I could re create the blueprint in code, but I don’t want to.

It was my fault…I tried to use FObjectFinder outside the constructor. DOH. Sometimes asking for help, actually forces me to analyze the problem more carefully.

Thanks kaziu.

There are several way you can get Blueprint and spawn in to from C++. The above solutions are correct, but you can also get one as parameter in function:



void name(TSubclassOf<ClassOfBlueprint> parameter)
{
   ClassOfBlueprint* effect = ConstructObject<ClassOfBlueprin>(parameter);
}


Will construct object out of blueprint class. This is for constructing objects from classes that derive from UObject.

For actors you should use SpawnActor.

Not true. Blueprint should be accessible at runtime as they provide great way to procedurally generate content, by randomly assiging properties and then constructing object out of blueprint.

At this point I’m using Blueprints extensivily to create spells, all and all of them are spawned and created as I mentioned above.

Moreover weapons in ShooterSample are also created in that way.

yes, its really awesome to be able to spawn blueprints this way. Thanks for showing us your method, I will be trying it out shortly.

Just to be Clear, this is how do it using SpawnActor:



		FActorSpawnParameters SpawnInfo;
		SpawnInfo.bNoCollisionFail = true;
		ARPGItem* itemBase = GetWorld()->SpawnActor<ARPGItem>(item, SpawnInfo);



Instead of Blueprint the BlueprintGeneratedClass (or whenever possible UClass) object should be used.

This change is not ready (and afaik it wont be ready for a while), but that’s the plan.

So I have my blueprint created I can spawn the blue print but how do I access the properties from code of the blueprint? IE. i want to get the material of the mesh. which is only accessible from the class I created and I have no reference to that and I can’t cast AActor to my custom class as it doesn’t exist.

I spawned my blueprint as an AActor.

Solved.

The transition to BlueprintGeneratedClass is interesting.

At the moment, our code searches for certain blueprints in the asset registry at runtime to assemble composite objects from external data.

These can be updated to construct from BlueprintGeneratedClass objects instead, but the asset-registry seems to enumerate only a small number of objectname_C objects for some reason, meaning the majority of assets would be missing. Is there a better model for this kind of behaviour that we should be using, or should all generated classes be enumerable via the asset-registry?

So how exactly does one get a reference to a Blueprint Generated Class?

Go with the old Blueprint then.

Is this what you’re looking for?

https://answers.unrealengine.com/questions/92651/get-blueprint-class-by-string-in-c.html

I’ve only needed to spawn instances of blueprint objects. I’ve never tried actually accessing properties from them though in C++ now that I think about it.