Download

Player input and class spawning

I have a gun class and to make the gun fire i need to put the fire input in begin play and changing the class name like this Screenshot - 6264dcb2185d61fd0a09bb79bf9c4a69 - Gyazo
What would be a better way to setup the fire input for like 5guns so one input will work for all 5 gun class instead of making a new input with a different class names for them

Also you can see that on begin play i make the gun spawn here Screenshot - 418ed3f274af8f1e97c8baf1a53517a5 - Gyazo
what would be a better way to make my gun spawn on begin play because if i made an switching system so i can switch from my primary weapon to my secondary it would cause problems because both the guns would be shown on begin play and they would overlap.

Hey as for the input you usually override those functions:

void APlayerController::SetupInputComponent();

or

void APawn::SetupPlayerInputComponent(UInputComponent* Component);

As for the binding of you Weapon… well. You probably dont want to setup the input binding for actions of your weapon. As you said, this will get ugly when you have multiple weapons. What you can do is, since your character owns a weapon you can access the current held weapon from it. Consider an example like this:




void AYourCharacter::SetupPlayerInputComponent(UInputComponent* InputComponent)
{

      check(InputComponent);

      InputComponent->BindAction("Fire", IE_Pressed, this, &AYourCharacter::FireWeapon);
}

void AYourCharacter::FireWeapon()
{
      // You need to assign a weapon to "this" character, GetTheGunInMyHands will return this gun object and your GunClass has a public void Fire() function f.e
      GetTheGunInMyHands()->Fire();
}




maybe that helps

kind regards
Wallhalla