How to set initial weapon?

Hey guys.

I’ve a Weapon class that I used to make a pistol pickup.

When I pickup the pistol it gets automatically added to my WeaponsInventory

70450-1.png

This is the code when the weapon gets picked up:

The problem is that I want to use OtherCharacter->AddWeapon() in the constructor of my character.
But since it was a Pickup, I just needed to pass the pickup itself to add to the inventory.

When I’m in the Character class, how do I add a new Pistol?

It’s like the Character pickups the pistol at the begin play

I heard about SpawnActor, but that spawns an actor in the WORLD.
Instead, I want my actor to spawn in the inventory of the character.

Thanks!

You can still use SpawnActor and instantly move the weapon to the character’s inventory. This will be result in the weapon being placed on the map depending on what Transform you feed the function, so you’ll have to initialize it invisible (SetVisibility) and probably also disable its collision (SetActorEnableCollision),

Hi, thanks for the answer, i need help!

Actually, weapons are Blueprints Childs inheriting from a C++ Parent Class

70565-1.png

So I need SpawnActor to actually spawn the Pistol Blueprint in the same location of the player character.

How?

---------------- 2nd question ----------------

Is it a good practice to spawn a Weapon into the world and set it as Invisible and No-Collision? We are spawning something in the World that won’t be really used

So I need SpawnActor to actually spawn the Pistol Blueprint in the same location of the player character.

You can spawn a Blueprint actor via C++: How to spawn a Blueprint Actor in C++? - Blueprint - Epic Developer Community Forums

You can pass a Transform struct into the SpawnActor to specify the spawned actor’s location/rotation/scale:

const FTransform targetTransform = FTransform( FQuat::Identity,
	selectedCharacter->GetActorLocation(),
	FVector( 1.0f, 1.0f, 1.0f ) );

AWeapon *spawnedWeapon = Cast<AWeapon >( GetWorld()->SpawnActor( m_weaponClass, &targetTransform , NULL) );

Is it a good practice to spawn a Weapon into the world and set it as Invisible and No-Collision?
We are spawning something in the World that won’t be really used

If an object is never supposed to be visible in the world, you could make it a UObject instead, which doesn’t have a transformation. (On the other hand, you mentioned that its location needed to be the same as the player’s…)

I did what you said, but the function parameters seem to be different

It says it’s required a FVector, not FTransform

Yeah, that can be confusing. SpawnActor is overloaded a couple of times with different parameter sets. Visual Studio’s intellisense system usually suggests a couple of them when you start typing the function.

AActor* UWorld::SpawnActor( UClass* Class, FVector const* Location, FRotator const* Rotation, const FActorSpawnParameters& SpawnParameters )
AActor* UWorld::SpawnActor( UClass* Class, FTransform const* UserTransformPtr, const FActorSpawnParameters& SpawnParameters )

Nothing wrong with using the FVector version if you don’t need the scale.

The compile error is confusing because the compiler appears to have recognised the correct function signature (FTransform) but is still trying to convert it to FVector, which is strange. Usually when I get such an error, one of the references is an incorrect type (const vs. non-const or reference/pointer).