Hey guys. Could someone tell me how to get a var in one BP from another? I’ve been told that it involves casting, but I don’t really know how to use that for this. Could someone give me a detailed answer on this? Sorry, I’m a complete noob here. Thanks!
Casting is a bit weird at first, but think of it like casting an actor to play a ‘role’, the ‘role’ being your blueprint script. The role is the specific character acting out the script, and the actor is any blueprint actor.
Casting allows you to basically choose a specific actor, and then get details about that actor. You can control which you are doing through the input and outputs.
Let’s say you want to make it so when overlap with an enemy, this enemy gives you a certain value of credits, and you can set this value in the world. Make a Enemy blueprint, and create a variable, integer or float named CreditValue, and make it’s value 20, and click make it Editable. Make sure the enemy has a collision box. Now put some enemies in the world and, with each individual selected, give them a random value for CreditValue in the world (since you made it editable, you can do so on the right in the Details pane)
Now we will get that value from the player when we collide with the enemies collision box. We will do it using casting so we can get the specific values you set in the world.
With your main pawns static mesh selected, go to where you create new events, and create a new event Event Begin Overlap.
From this, drag off the Other Actor node, and search for == (actor). Click that. Make sure it’s connected to the top portion, and in the bottom portion, search for your blueprint ‘enemy’. Drag off the exist node, and search Cast To (enemy).
Connect that up. Now what this is doing is, when your main actor overlaps with something, it’s trying to cast the actor it overlapped to the role of ‘Blueprint’. Note that if the cast will fail if it is not == to Enemy, which is why we added that check. If it IS an enemy we are overlapping with, we will get the actor we overlapped with, and we will cast it to the role of enemy. Note that with casting, a blueprint can be the actor and the role. In this specific case, the actor from our overlap results is the actor we are casting, and we are casting to the role of ‘enemy’ - even though the actor is an ‘enemy’, I hope this makes sense. But this allows us to both get specific information from him, and have a way to choose a specific actor (in this case, the one we entered range and collided with).
Now that we have the cast done, let me explain it’s purpose. We now have the actor we collided with casted as an Enemy, and we know that we created the variable Credit Value on the Enemy. So drag off the ‘As Enemy Blueprint’ (or As Enemy? can’t remember specifically, the exit node for the cast) exit node, and search ‘get Credit Value’. Once you have that, right click and search Print String, and drag the exit node value from Get Credit Value into the text box, and make sure it’s all connected.
Now start your game. You should be able to walk next to an enemy and have the game print the value of the variable you created. More importantly, you did so by casting to an actor, and hopefully learned how it worked in the process. This isn’t the only way to cast, the beauty of casting is it allows you to get specific actors and tell them what to do, or get information from them, or change their information.
If that was too detailed, sorry, I tried to be thorough. If you still need help just ask
Woah, thanks so much! Will read through this and try it out, and I’ll let you know if I still need help afterwards. Thanks again for your detailed answer!
Ok, so yeah, here’s what I’m trying to accomplish:
I have an enemy AI set up, and I’m trying to set its range of sight depending on your attention level. Basically, the more noticeable you are, the higher your attention level, and the further away the AI can find you. In the FirstPersonCharacter BP, I’ve got a variable set up that holds your current attention level. Basically, I need to set your attention level to the AI’s sight range (I’m using a PawnSensing component on my AI, so I need to set the Sight Radius value to whatever my variable in the other BP is, from inside the other BP.) I hope this makes sense.
This is probably EXTREMELY wrong, but here’s what I have right now:
Of course, this won’t work because I don’t have the Object selected for the cast. I’m not sure what to set that to. As I said though, this is probably all wrong anyways.
You can “Get Player Pawn” and plug that into your cast. You can also do the cast on BeginPlay and store the reference to your character as a variable (right-click on the output pin and “promote to variable”), so you don’t have to cast every single frame.
Ok, what am I doing wrong here?
I’m trying to, when I press G, check what the player’s attention level is. Then, depending on what it is, set the AI’s sight radius higher, then print out the new radius. However, when I press G, absolutely NOTHING happens…
woah thats messy . First tip if you double click a wire you can add reroute nodes this allows you to keep everything tidy.
Plus you should be using custom events or functions to hide most of the code that is not necessary to see
As for youre problem its hard to see with it set up as it is.
Only thing i can suggest is check that you are setting the attention variable properly and that it is being casted properly. Where you have the target/attention node right at the beggining drag out a print screen and attach it to press g. This should print out the value. Check this value first. If this is fine then you need to check where you are setting the value which im guessing is in youre first person character bp.
Double click to add reroute node is an awesome tip, thanks!
ALso to the above poster, try not using a pure cast node, and add a print to the failed output of that node to see if your casting is failing.
Yes, the attention is all set up in the FPCBP. I’m positive that that is all working, though. I’ve already tested that out a lot, and now I’m just trying to get it plugged into the AI. I will make sure that I’m casting properly. I’ll get back to you.