Huge Blueprint Problem I've Been stuck for 2 days.

Hello everyone , i’ve been working on Gameplay for my Game and i’ve been following some tutorials scattered around youtube / forums etc. to create the mechanics im after.
I’ve found a really good tutorial series about creating an inventory / weapons etc. on this Website Tutorials | Shooter Tutorial

With the Inventory system that this fella created it was not possible to create WeaponPickups to add to inventory and i’ve fixed that.

but now i got a greater problem , somehow i cant create Ammo pickups.

in this tutorial "on the website is number6 Weapon Shooting and Reloading functionalities.
we create / modify the BP_BaseWeapon

in the Baseweapon we also create some variables that are CurrentAmmoInMag , MaxAmmoInMag , CurrentAmmoInBackpack , MaxAmmoInBackpack all these variables are Integers

after this we need to open BP_Weapon_Pistol that is a Child Blueprint of BP_BaseWeapon.
in this child blueprint i inherited all the variables from BaseWeapon and im able to modify them suiting what kind of weapon im currently using “its really handy”

but here’s my problem.

Following the tutorial. the only way to start / get ammo.
is in BP_BaseWeapon to setup an event begin play to set the max ammo in mag as current ammo in mag , and the Max ammo in backpack , as current ammo in backpack
this way as soon as i test the game , and equip the pistol that i pickup from the ground i got the ammo “great”.

problem number 1 , when i remove it from inventory and i re-equip it i get all the ammo back , also if i switch it from slot. and i dont want that.
problem number 2, i have no idea on how to create an ammo pickup for this type of setup.

if i disconnect event beginplay to set all those ammo that way , and i equip / unequip or switch slot of the pistol i dont get any ammo “and thats ok”

i tried to create a Pickup actor , also before doing that i’ve set all the variables on editable / public hoping that it would change something but i just cant get it to work.

in the test Pickup i created a CollisionSphere , with a static mesh “just a pistol magazine”
and

OnComponentBeginOverlap(Sphere) its my problem.
there’s no way i can cast it to BP_Weapon_Pistol. its just not working , and i cant modify the CurrentAmmo in backpack Variable since its only existing in the BP_BaseWeapon.
the only way i found out to get ammo by pickup.

is this

http://imgur.com/a/zP5yT (Blueprint that should act as Pickup)
http://imgur.com/a/r7Q1m (BP_WeaponPistol to call the event)

This way “yes” i can pickup ammo , but i recreate the original problem of , everytime i remove from inventory , or switch slot , the ammo of the weapon reload automatically.
even tho this part gets the pickup to work.

at first i thought it was simple. just to set the variable to the desired amount of ammo i wanted to pickup Casted to BP_WeaponPistol or BP_Base_Weapon and i would get my ammo.
but casting to either pistol or baseweapon doesnt work , and the only way to make the ammo pickup is on how i show on screenshots. but i got many issues with this.

I Really hope someone Way better than me in blueprints can point me to a solution. this thing is driving me crazy and im feeling stupid.
For any additional info i can post screens etc… just help me in desperate lol

EDIT: After Updating engine to 4.15.1 “using same blueprint as before” when i pickup the pistol it doesnt assign the ammo rightaway" works as intended.
When i pickup the Ammopickup on the ground , it correctly assigns the ammount of ammo specified “works as intended”
but when i unequip and reequip the pistol , it deletes all the stored ammo :frowning:

@snajlen - Im not familiar with this tutorial - I think this might be whats happening - when you “remove from inventory” are you SPAWNING a new BP class into the world (which of course would inherit a full magazine of ammo because it is brand new, and NOT the one you actually picked up and used. If you set variables to count down the number of ammo left in a magazine, and then ‘destroy’ that actor in inventory, and Spawn a brand new actor- they have nothing to do with each other. They even have different names 01, 02, 03 etc as you spawn and destroy.

Instead, of Destroying actors, try Hiding them, they will remain in the world but not visible and will retain their variables. When you ‘remove’ from inventory, simply ‘Reshow’ the original Blueprint class actor, and Move it to where it should now be (in front of character). That way when you go to pick it back up again (actually just hiding it again) it will be empty as it will retain the variables you set while using it. Best of luck! - Jerry

Thank you for the Reply infact i was investigating in the UI Weapon Selection and indeed when i close the inventory The actor gets destroyed on all slots , otherwise all the weapons are shown in hand at the same time.

Sadly by setting actors hidden , instead of Destroyed will cause overlapped weapons aswell. and some other issues.
but testing with only the pistol removes the ammo anyway when i close the inventory , even if i put the actor on hidden.
Is there anyway for me to store “the old” current ammo information , to reassign them when the new actor is spawned ?

Edit: i forgot to put Hidden to true , well , hiding actors actually works the same way as destroying them “i get the same visual effect” so it works.
but it still removes my ammo when i close inventory man its frustrating.
also after the hidden event , it will call the function to Spawn the weapons and assign them to the slots by Spawning actor. since you can switch weapons in slot everytime the system is a bit messy.
so i really need to find a way to store those variables and reassign them when they spawn the weapons.

hi, my inventory is a component blueprint in the character so CurrentAmmoInMag , MaxAmmoInMag , CurrentAmmoInBackpack , MaxAmmoInBackpack are outside from the weapons, from this way you saved some headcaches.

well the problem is that sadly i need the variables in the weapons. and yeah im getting serious headaches with this system. but i feel like its the most optimal for the system im trying to build. i managed to get the working ammo pickup for the current weapon equipped.
now i only need to find a way to store those variables and reassign them everytime the weapon is respawned.
sadly i feel like if i cant figure this out i have to delete everything i did in the past 2 weeks and start all over again with a more simple system involving variables for each weapon.

You dont need the variables in the weapons, Just read them,
the weapons can access to variables in the inventory using casting, or put the values in a table, or structure in base class

i have no idea on how to do that , programming / blueprinting is my worse area of expertise.

It sounds like you are storing the variables in the inventory (not sure where that would be) and not the Pickup item blueprint. You could try storing them in the pickup item instead. That way they will be retained while hidden and will have nothing to do with the inventory version. The challenge with NOT keeping them with the pickup item is if you ever do multiplayer, and another player picks up that gun, it should have the same amount of ammo as when you left it last. I would try to do a test by creating a new variable that counts the number of times you pull the trigger and store that in the pickup item. Hide/show as many times as you want and see if you can get that new pickup item variable to behave correctly using print strings. If you can do that, then you should be able to transfer the logic over to the pickup item and not have to redo all your work. @erWilly’s solve works too, just might be more complex if not familiar with a good way to do that - I often get tripped up with casting personally.

I will probably retain the system as it is if i cant manage to successfully create the ammo pickup mechanic to work correctly and let players access the inventory to switch weapon only in particular areas of the game “that way the ammo gets refilled” and it should work as intended.
The inventory system following this tutorial works like this.

Basically i have a Structure that contains “Weapontospawn” reference to BPBaseWeapon Class.
inbackpackimage texture 2d “has the icon of the weapon”
is selected bool
inslot integer.
The structure is then used in GameInstance where i have an array named Backpack_Weapons reference to the structure named above this Array has 0 Elements “0 weapons” so the inventory is empty when launching game.
Most of the functions are run through UIWidget where calls the events referencing the Game instance and run the whole thing of moving stuff from “backpack” to equipped weapons where im running 4 active slots that i can switch with 1 2 3 4.

in fact to had the possibility to add the weapons to the backpack after hours of “trying things” i figured out that i had to create array’s referencing the WeaponBackpack array “example Pistol array reference to WeaponBackpack” and as element WeapontoSpawn has BP_Weapon_Pistol with the backpackimage of the pistol.

nothing in the array has ammo , all the ammo variables are in the BPBaseWeapon.
in BP base weapon i have the Fire Function that sets CurrentAmmoInMag -1 then set CurrentAmmoInMag
then the reload function “that works correctly”

and 2 other functions “HaveAmmoInMag” and “HaveAmmoInBackpack” outputs on ammoinmag points to if current ammo in mag > 0 then HaveAmmo bool.
and if current ammo in mag = maxammoinmag then mag is full , and these functions are used on playercharacter to decide when you can reload etc etc.

All the ammo variables are only in the BP_Baseweapon “maybe thats where my problem is”
and all the other weapons are childBP’s of this baseweapon and they inherit the variables.
and thats what makes me unable to create these ammo pickups , or better i found a way to get the ammo pickup but adds bullets for all the weapons , doesntmatter what i try.
by changing a few things i can add bullets only for 1 weapon and only if its equipped and thats no ideal either.
i’ve tried by casting and whatever but it just wont work to pickup these ammo’s , and the other problem is that when i close the inventory. my ammo gets deleted anyway. and thats because everytime i close the inventory on playercharacter the function that SpawnWeaponsandassigntoslot , from the Backpack array gets the “weapon to spawn” and based on the slot selection spawns the actor on the weaponslots. and then attach the actor on the socketname specified in the Baseweaponbp.

TLDR: its a huge mess. but overall it works. now i just have to do some research about erwilly’s suggestion about putting values in a table or structure. cause that might be the thing that could save me “or maybe not” otherwise ill just try to find a good way to make a good gameplay using this system.
i just have the regret to be extremely bad at everything that involves programming / scripting. :frowning:
ill try to find a solution in the next 2 or 3 days , if im wasting too much time i just have to keep going.
but i hope someone can point me in the right direction

From an OOD perspective, having the CurrentAmmoInMag and MaxAmmoInMag variable in the weapons seems like good practice because you want the object to be responsible for its own properties. It makes the design more scalable, because if you plan to add different types of weapons in the future you could design each one to hold different amounts of ammo easily and they could check their own state if they’re “full.” If you put that logic outside of the weapon then you’d have to check the type of weapon to figure out the amount of ammo it can hold before assigning a value to it, so if you have hundreds of different weapons then it would get messy really quick if you need to check how much ammo it can hold, unlike if it just “knows” how much it can hold.

Anyway, I’m trying to make sense of the screenshots of the blueprints you’ve posted and it seems like you’re giving ammo to BP_WeaponPistol everytime your pickup begins to overlap. It doesn’t check what object overlaps with the pickup though so I guess if anything overlaps with it then your weapon will reload?

At the moment I’m not entirely sure I understand how your system is working yet, but from what I understand the problem you are trying to solve is to retain the same amount of ammo on each weapon even when you switch between them?

Cast is a fundamental pillar to work with blueprints as it was for the unrealscritp.

The good news is that it is “hard to learn, easy to master

The bad thing is that there is no decent tutorial that explains the logic

but, Please, understand first all about cast and you will multiply your effectiveness

yeah “overall” i kinda figured out how casting works and everyday that i start working on my project again i feel like i know how things are working and yes , tutorials are pretty lacking. i had to figure out most of the things myself by testing etc etc.
i decided that i wont try to do this ammo pickup thing for the moment , and im working on otherstuff , ill go back at it when i understand more about the blueprint system and the comunication between blueprints. maybe ill figure something out later on.
apart from the ammo pickup thing . the entire system is working as intended , sadly to understand this kind of blueprints that have been created following this tutorial , is to watch the tutorial steps itself. everything is written and has a few screenshots of the BP hierarchy

Short version of everything is that i was trying to change variables of a blueprint that inherits variable from another blueprint. Example: Pickup blueprint , from here i want to change variables inside BP_Weapon_Pistol that inherits Variables from BP_WeaponBase. (using castingsomehow but on overlap if casting is active its not working)
and i still cant accomplish that sadly.

Your first post is a bit confusing.
You do not have to make a cast because “get actor of class” already returns in itself a variant actor of type weapon_pistol

One of the problems with cast is the name itself, if it were called “convert” for example would be easier to understand what its use.

casterr.png

Change “cast to” for “convert to” and you will understand exactly what it does.

You can only cast classes that are below your hierarchy or the cast fail

Casting from actor you have a vast conversion posibilitys.

Casting from weapon_base only see the classes under weapon_base