Basically, I have an inventory system where what I have set up is that I have coded the base class for an item (that can be picked up and put into the inventory, which is a TArray) in C++, and have created blueprints based off of it (because it is easier to just drag on the static mesh I want, rather than hard code it in.), and basically when the player wants to drop an item, it raycasts out and finds an the first hitpoint with the ground, and then spawns the actor there. is the spawn code:
auto Actor = GetWorld()->SpawnActor<ItemClass>(Hit.ImpactPoint, FRotator(), params);
(Hit being an FHitResult).
The actor is being spawned, but because its a BP, all the previous information associated with it, (the mesh, other variables, etc) aren’t given to it (by default the item is basically nothing), and I was curious if I was able to get the class of the blueprint, from which is stored into the inventory array, and spawn it with all its previous information.
Hey there i think this qustion has been asked several times before.
Am not 100% oon this so am just going to link you to a page and hope it be helpfull to you.
No, that mostly has to do with a crash by the looks of it.
My issue is that I want to know how to spawn actors from a class that has already been initialized etc.
So for example:I have a class,it is derived from AActor, and it has 2 variables. One is a UStaticMeshComponent, and the other is an FString. I then create blueprints based off this class, and set the variables up. I then place these in my game world.
Now what I want to be able to do is duplicate one of those premade blueprint items ( spawn another actor using GetWorld()->SpawnActor() ) from the class I get in C++, from a ray hitting it, and me getting resulting actor from the FHitResult, with all the variables still intact.
Well, solved this one for me (again), basically what I did was adapt Spawn Blueprint from C++ - Editor Scripting - Epic Developer Community Forums into a more dynamic system. Basically, on the item blueprint, you have to set a variable (UBlueprint*) to itself, and when the object needs to be recreated, it just uses
ItemClass* Actor = GetWorld()->SpawnActor((UClass*)ItemInventory[CurrentItem]->bp->GeneratedClass, Hit.ImpactPoint, FRotator(), params);