Should I call the override function “SetupPlayerInput” in my “base weapon” class or should all input be set up in my character class?
I don’t know if input will even be recognized inside the weapon class, but you should probably have it in your character class. That allows the must versatility and is the more common way to do things.
How would you call a function from the weapon class in the character class or get the input from the character class?
if your controller is possessing the character, then your input should automatically be detected. But you may have to set it up (although in newer versions of the engine it does it for you automatically)You’ll wan to override the SetupPlayerInput() function of the Pawn/Character to which you can bind your functions to.
As far as calling a function from the weapon you would typically setup a reference to your weapon inside your pawn with a variable. So AWeapon* Weapon. Then from there you would spawn the weapon, and assign that reference to it. Then you call functions from it from your input to your weapon like Weapon->Fire();
ok. so in the event I have 2-3 weapons just make those a variable or array and check to see if it is active or not null and call it. Here is a simi stupid question I am assuming that I have to include the weapon.h file to make a reference correct?
InputComponent->BindAction("Fire", IE_Pressed, this, &baseWeapon->OnTriggerPulled); gives an error
You don’t need to make a a reference for every weapon. What you should be doing is making a single reference to a class that all your weapons have in common, known as the parent class. C++ is setup so that you can take one class, and extend it so you’re not rewriting tons of code. Along with that, comes the ability to call the same function over all your different types of weapons.
So what you want to do, is have a parent class. We’ll call it Weapon. Inside that class, you make a function called Fire().
virtual void Fire();
//Bang Bang goes here
So now in your character class you make a variable to reference the weapon you’re using, and a new function to be called whenever you press the fire button (we do it in the pawn because if you don’t have a weapon, you don’t want to crash the game trying to call something not there)
void PawnFire(); //We'll call it PawnFire so we don't get confused in this tutorial
Now what we want to do is to have the input call PawnFire, then PawnFire says “let’s see if there’s a weapon in our hand, if there is. Let’s use that weapon and fire”. This way, whaetver gun you’re holding, be it a machine gun or a pistol it’ll only need 1 reference and 1 function to fire either one of them. No need to do crazy checking on what gun you have for every weapon type.
if(CurrentWeapon)//Make sure we have a weapon so we don't crash the game
And that’s the general idea! You do need to include AWeapon.h into your character’s header file, that way it knows what functions the weapon has. But from there, you make all your weapon’s functions inside Weapon, fire, reload, jamming, cleaning, and pistol-whipping. Then you can make sub-classes off of Weapon for all your different types and the only thing you have to do is change how the gun fires, and never bother messing with the character for every new weapon.
You’ll notice I used the word “virtual” infront of the Fire() function for the weapon. This let’s subclasses override it if you need to, you’d do that by adding and “override” modifier inside your child class that’s extending the main Weapon class. So inside WeaponPistol you could put this in your header:
virtual void Fire() override;
now you can add it to your WeaponPistol class and change all teh firing mechanics if you want without making a whole new class. But usually it’s best to set it all up in the parent class, and then only change small variables inside the child class, like bullet amount, fire rate, etc.
Thanks for the quick tutorial.