Hey all wonder if anyone can help me. I’m trying to cast to my rifle blueprint from a pickup item blueprint I made but for the life of me I don’t know what to put in as the object.
I’ve got it working with get all actors of class to grab the variable out of bp rifle but I’ve heard that can be costly. Can anyone help please?
If you set up a BP_Rifle object ref variable in your BP_FirstPersonCharacter like “MyWeapon”, you can then pull that variable out of the As BP_FirstPersonCharacter and use that. You won’t need the cast in this case, but probably want to at least make sure it’s valid.
You will need to set the MyWeapon variable with a reference to the actual object, like when you attach the weapon to the character, you can then set the MyWeapon variable to reference the object.
Hey thanks so much gonna try that. Still kind of new but enjoying the learning (using Skillshare haha).
Could you tell me what I would put there if I just wanted to say grab stuff out of it or from one bp to another? If you could possibly snippet a picture that’s be amazing.
This seems like you have items in an array which do not exist anymore or which are null (invalid) depending on your implementation.
Has the BP_FirstPersonCharacter multiple items and not a single weapon now?
No there isn’t an array anymore. It’s just the standard first person game mode with the bp_rifle that you can pickup.
I did wht the guy told me to do at the top and put a ref to my bp_rifle in a variable within my first person bp. I am then casting to my first person bp from the buff pickup item and trying to get the bp_rifle ref and get the Boolean is buffed out of it.
When I do this tho it doesn’t return any value but null even though you can clearly see in the last image I can call upon said Boolean within my buff pickup.
Okay, so you added the variable for the rifle. Do you set the variable somewhere?
In the BP_Rifle Blueprint the rifle attaches itself to the character but it won’t set the value of your Weapon variable automatically.
You can let the BP_Rifle set itself to the character by setting the variable Weapon of the character in the OnComponentBeginOverlap event of BP_Rifle.
That worked!
You, my friend, are a legend. Okay so if I want to ref in the future I’d have to set it in the bp itself?
Also again is there a way to cast from one BP to Another that isn’t an pawn? Say I wanted to only cast to bp_rifle in my buff pickup what would I put in the Object part? I know on casting to first person I’d have to put get player character but for the life of me I can’t figure out what I’d put there if I wanted to just cast from one normal bp to another.
Like what would I even put on that part so I could get all the variables out of bp_rifle?
This is an example so (ignore it being on an on collision node) you understand what I mean, bad at explaining. Can’t figure out what to put on the object part.
Thanks so much for all your help I appreciate it SO much.
Yes, every variable you create on your own is normally empty (null) or has a default value which you can set. Because your weapon is something dynamic (because you can have one or not) you have to manage the variable yourself by setting it.
Casting is a feature object oriented programming languages use to access the variables of specific types which derive from a more generalistic one.
In Unreal Engine every object you place into the world is an Actor. Normally not just an actor but a specific one like the BP_Rifle.
In Unreal Engine you can create you own variant of an Actor to extend it with the functionality you need from it.
For example the BP_FirstPersonCharacter is extended through multiple subtypes.
Actor > Pawn > Character > BP_FirstPersonCharacter
This means the BP_FirstPersonCharacter extends the functionality of the Character, which extends the Pawn, which extends the Actor.
So, why all of this… to be able to bundle them up in an Array which the computer can work with they have to share the same type. And because all of the objects in a UE world are Actors you can always send them through functions like Begin Overlap as an Actor.
Casts will only return a valid object when the object you tried to cast is exactly or extending that type.
I guess there isn’t an other easier way to access the BP_Rifle.
Instead of casting i would recommend to do it with interfaces. You can get the same result on much better performance. And you are able to configure the output (if you need one)