Download

Having a hard time combining C++ and blueprints

Hi all,

I want to read a JSON file and from the data in the file create actors when the engine is running. However going from C++ to blueprints is giving me issues and it’s turning into a mess. I feel I’m not using the right approach and I’m wondering how you guys would cleanly architect a scenario like this.

The mess is caused by me not knowing what to do in C++ and what to do in blueprints. So I’m doing stuff in C++ that I should be doing in blueprints and vice versa and there’s all kinds of needless communication between these layers. My main preference currently would be to do as much as possible in blueprints (although that is a version control / Git nightmare, but that is another discussion).

I’ve created C++ parent classes and created blueprints off of those in the editor. The blueprints are my main actors I want to work with. Why the C++ classes, because I have not figured out how to spawn a blueprint class created in the editor from C++. I would love to skip the C++ base classes but those are the one I create from the JSON data.

Another issue I’m running into when hot-reloading C++ code I cannot save my map / level blueprint anymore due to “Can’t save …: Graph is linked to object(s) in external map. External Object(s): /Engine/Transient”, not dissimilar from this: https://answers.unrealengine.com/questions/97059/graph-is-linked-to-external-enginetransient.html

I think this is because of creating objects in C++ and not destroying them (and storing them in a static variable)? I’ve tried supplying an Outer to NewObject but so far that hasn’t worked. (Anyway, I hope this will be fixed by using the right approach.)

To repeat my main question: how would you guys handle a situation where you have to dynamically create multiple actors in a running game from information in a file, so that most of the logic can be implemented using blueprints. If this is not actually viable and I should just defer to C++, please tell me that.

I can’t really share any code or blueprint screenshots due to work but I’m hoping for inspiration from your approaches.

I hope I understood you corretly and not wasting your time…

You can use a c++ function to create blueprint objects** in blueprints**.

Create a new class, something where you can store your static methods. Make it blueprintable…


UCLASS(Blueprintable, BlueprintType)

Add this to your H file


	UFUNCTION(BlueprintPure, Category = "Xtended", Meta = (DisplayName = "Create Blueprint"))
		static UClass* CreateBlueprint(FString Folder, FString BlueprintName);

And this to your CPP file


UClass* UYOURCLASSNAME::CreateBlueprint(FString Folder, FString BlueprintName)
{
	FString Name;
	UBlueprint* bp;

	{
		Name = FString(FString(TEXT("Blueprint'/Game")) / Folder / BlueprintName + "." + BlueprintName + "'");
		bp = Cast<UBlueprint>(StaticLoadObject(UBlueprint::StaticClass(), nullptr, *Name));
		if (bp) return bp->GeneratedClass;
	}

	return nullptr;
}

You can use a parent blueprint that contains anything that you need. Via c++ you’ll be generating the real blueprint you want (the child bp), and use it via it’s parent’s functions/vars/etc…
Something like this (you can set inputs via your json file here):
7215dce7b42aac8aed051b93026ad944bc580587.jpeg

I’d prefer using structs in blueprint when it comes to creating variables for same type items. Create a struct and fill with any variable you’ll nedd.
After spawning the blueprint, you can fill it with your json then call an event maybe to update the character.
Also you can send it to a bp function to update…

Something like this maybe…
aa6772c5e949883833a0d0e2b27959ad7c413103.jpeg

Thanks, that looks promising!