i am wondering how i should store my values for weapons. In blueprints (in my first Projects) that was realy simple. But now i want to make it all in full c++. Now i have two weapons as a blueprint, derived from the base class (C++).
But for every weapon i have to store the informations like Ammo, Current Ammo, Is Weapon collected and so on. For an example i take the reload function. The base class have a enum for the weapon type (blueprint read write)
If i store the Infos within the base weapon class, the reloading function Looks like this:
void Project::Reload()
{
if (CurrentAmmo < MaxAmmo)
{
Do some stuff here....
}
}
Such reload function is easy and the weapon works as expected. But if i store the values in the Player Code, then i have to call it everytime if i reload, and the weapon type too. I can store the values in the Header, or in a struct…
void Project::Reload(ACharacter* Owner)
{
ACharacter* Player = Cast<ACharacter>(Owner);
if (Player)
{
if (WeaponType == EWeaponType::Pistol)
{
if (Player->CurrentAmmoPistol < MaxAmmoPistol)
{
Do some stuff here...
}
}
}
}
If i have, let’s say, 10 or more weapons, then i have to write for each weapon this Code. I know there must be a way to do that in a short, easy and clean way. But i don’t find it. Is there something that i haven’t seen yet?
I think on a method to pass the values i Need to the reload function. Both ways are working, but with the second one i am not happy. The first one is good, but later i have to save the informations (ammo, reload ammo and so on) in a savegame.
Why you would store the values of an encapsulated object in the Player class?
Also, are you trying to cast a Character to a Character? Pls say it was typo
The values of your weapon should be only inside the weapon class. A simple FStruct can handle this.
Your Player should carry just the references of the weapons he owns. For this amount of Weapons, a simple TArray should work neat for you.
So, you will spawn the weapons, set the proper owner and update this TArray. Basically what a “GiveWeapons()” method should do called in a GameMode class. It’s pretty fun when you start encapsulating steps as you start to have full control of the events in your game
Having the references, just add a method “RequestReloadCurrentWeapon()” inside your player. Then you get the reference and call from it ->Reload()
No need to pass any parameter for this routine.
If you wish another solution, you can set a pending flag if can be the case Reload can take priority over events in a single frame.
I’m sorry. I had so much work that i didn’t look at the forum. My project is running. But just now i have only 2 or 3 hours per week to work on it. I reply when i worked on the inventory for the next time.