Download

How to spawn the correct Blueprint from C++?

Hi.

I have a PickupWeapon C++ class that the player can pick up to get the weapon.

For each weapon in my game, there’s a Blueprint child class (deriving from the C++ PickupWeapon class) where I put the StaticMesh and other components.

When the player dies, I want to spawn a PickupWeapon that corresponds to the weapon the player had before dying… so that other players can “steal it”.

The problem is: how can I know which Blueprint child to spawn?

In the function Die() of the Character, I can spawn the PickupWeapon actor… but that’s just the Base C++ class! I want to spawn the appropiate Blueprint child class!

Could you suggest me how to do it… or maybe a better system?

Thanks in advance.

Hi gedamial,

Maybe I am misunderstanding but if the pickup is of the same class as the weapon the character is holding, you can just detach it and stop moving it, right? There would be no need to spawn a new actor. If you do need to spawn a new actor, you can do so in the Die() function by fetching the class of weapon the character was holding, and passing that as an argument into SpawnActor. Also see https://api.unrealengine.com/INT/API/Runtime/CoreUObject/UObject/UObjectBase/GetClass/index.html and http://api.unrealengine.com/INT/API/Runtime/Engine/Engine/UWorld/SpawnActor/2/index.html. Note that it allows you to spawn an actor of a class different from the template argument.

My game has no graphics. It’s all code for now, since I’m not an artist.

The Character has an array of equipped weapons. The PickupWeapon makes the corresponding weapon in the array “usable”.

The PickupWeapon class has an enumeration EWeaponType which has to be set to the weapon type the Pickup should correspond to. (e.g. RocketLauncher, Pistol, GrenadeLauncher…) —> this value is set in the BLUEPRINT CHILD CLASS.

Once the PickupWeapon is picked up by the player, the Character’s weapon is enabled in the array (depending on the WeaponType of the PickupWeapon).

When the player dies I want to spawn the PickupWeapon… but since the PickupWeapon has many Blueprint child classes… I need a way to detect the Blueprint child class that has the WeaponType corresponding to the currently-equipped weapontype.

I hope I made it clearer…

My game has no graphics. It’s all code for now, since I’m not an artist.

The Character has an array of equipped weapons. The PickupWeapon makes the corresponding weapon in the array “usable”.

The PickupWeapon class has an enumeration EWeaponType which has to be set to the weapon type the Pickup should correspond to. (e.g. RocketLauncher, Pistol, GrenadeLauncher…) —> this value is set in the BLUEPRINT CHILD CLASS.

Once the PickupWeapon is picked up by the player, the Character’s weapon is enabled in the array (depending on the WeaponType of the PickupWeapon).

When the player dies I want to spawn the PickupWeapon… but since the PickupWeapon has many Blueprint child classes… I need a way to detect the Blueprint child class that has the WeaponType corresponding to the currently-equipped weapontype.

I hope I made it clearer…

Ok what you need todo is spawn the actor based on the player stored variable of the EWeaponType and use a switch to choose which weapon to spawn. IE if the last weapon the player had was a RocketLauncher then spawn a Rocketlauncher on top of the player.

EDT:
Furthermore I recommend if you do plan to work with an artist or assets later to attach the pickup to your player when its picked up and then unattached it on death because it will make it far easier to implement 3D models later. Bonus points if you use a socket to attach the weapon.(Otherwise your just going to have to re-write all the code later)