Hello. I’m trying to make a simple blueprint that changes the target of a homing projectile between two alternating teams. It’s working as intended, but I feel like I repeated too many things in this blueprint and I can’t figure out a way to make it more simple.
I tried to make a function which takes an input that should be the next target, but I ran into a lot of problems. If anyone could show me a way to improve this, it would be a big help.
Much better! haha…now, what is this?? I am not understanding the gameplay mechanics here. If you are basically deflecting a missile back and forth and there are multiple team members that can be targeted I am not sure this is the way to go. “Get all Actors” is going to randomly sort all available actors of that class, but then you are looping through all of them and setting the projectile target? This will only ever target the last actor in the array because the loop will constantly switch the target as it executes. So not sure where that is going. What exactly are you trying to do here? In words not code haha
oh, that’s just the way I found to access the actor I want to target that is in another blueprint. I don’t understand casting very well, so that’s what I used for now. It worked so I didn’t think it was actually wrong
As long as something is working, “wrong” in coding is a spectrum that goes from very poor performance to optimal performance. So if it works, then your “logic” isn’t “wrong” per se but that doesn’t mean it is the “right” choice or the “best” choice. If there is only 1 actor on each team I would cast directly to that actor. Casting is basically a way to transform 1 data type into another. In order for it to work your data types have to be compatible. For example, you have an overlap event, the player character overlaps a trigger volume and the volume recognizes an “actor” overlapped it. It does not however, know exactly “who” did the overlap, it could be a player or a bullet, so you take the “overlapped actor” output which holds the reference to the overlapped actor and you would “cast to” the player character for example. This will then attempt to turn the “overlapped actor” into a “player character” reference, if the actor that overlapped was in fact the player character, this cast will succeed if not, it fails to convert. You would then script logic after that accordingly. This is the purpose of casting. Casting when successful also allows you access to variables contained within the blueprint you cast to, so in our example above, once you have the player character cast succeed you would then be able to alter any variable located within the player character.
What confuses me about Casting is the Object input, especially if I’m casting from one actor blueprint to another. If I cast to the player character, I can just plug the Object pin with a Player Pawn node, but if I’m casting between actor BPs that I made myself, I have no idea what to plug into that pin. Everything I tried gives me either a warning or a note, saying that whatever I’m doing is redundant. So I kind of gave up on trying and just used the Get All Actors of Class node in these scenarios. Could you shed some light on this?
Haha sure. So basically you have to remember in order to “cast” something you need what is called a reference. This is the object pin you are talking about. So for common things like “player character” or “player pawn” Unreal has a built in function/ node that through some back end code actually generates the reference to the specific instance of the player character or pawn in the level. For custom blueprints you have to generate the reference yourself and that’s where everyone gets stuck. You can use “get all actors” but it’s one of the least efficient ways because it requires the computer to loop through every actor in your level and check to see if it is the desired type. Depending on your set up, overlap events are good ways to get references, as are hit events. Other times for things like widgets what you can do is create them in a blueprint you can easily reference like a player character and save the created widget as a variable within the player, that way you can easily cast to the player and as I mentioned before doing that gives access to all player character variables one of which would now be your widget. This is a way to back door into some more difficult actors to directly reference. I’m gonna make a tutorial on casting at some point I’ll post it here when I do because I feel so many people struggle with this visual aids would probably help haha anyway hope you have a better understanding now, if anything wasn’t clear let me know.
What Nebula said about how casting is basically a way to transform 1 data type into another is essentially correct, and I would add that in order to cast to a certain data type (either a bot or player character in your case), the object parameter has to have some relation to the data type it’s being cast to.
For example if I were trying to access the properties in a static mesh component, but all I had was a primitive component reference from my linetrace, I could cast downwards to the static mesh component because static mesh inherits from primitive component, even though it is a different type.
This is not the most thorough example of casting, but this helped make casting a little bit easier to understand for myself, so it might help you as well.
If you could show an example of the notes/warnings you get that say your cast is redundant, I could be of more help.