Actor Casting Problem

This should be a simple thing but I’m failing miserably. I’ve broken this down into it’s most basic form just to get a reaction from the cast event but it keeps failing no matter what I try.

I duplicated a basic shape static mesh, renamed it (Zzz just so it was easy to find in browser) and added a blueprint from it. In the blueprint of the mesh, I made an extremely simple custom event called “Talk” that goes into a Print String that reads “Pauly Wants a Cracker”. See image:

Now, I want to call this event, “Talk” from my characters blueprint. In my characters blueprint, I added a key event binded to the “G” key. I drag off and put a “Cast To Zzz_BP” node. Here is where I’ve tried 2 different variants with same results: First try I change the “” input node of the Cast directly to the “Zzz_BP” . After the Cast I have a call to the “Talk” event on success and a Print String that reads “FAIL” when the cast fails. I play/test and I get the “FAIL”.

Second attempt I created a variable called “Zzz_Ref” and make it of the type “” that is linked to the “Zzz_BP”. I plug this reference into the “” input of the Cast node and play/test again - exact same result: FAIL.

Last thing I tried was to hook the variable “Zzz_Ref” directly into the “Talk” event call as a direct blueprint communication but nothing happens during play. See next image for blueprint setup:

As you can see, I’ve ensured the variable reference was checked as “editable” so it would be seen in scope.

Last straw was to add a Print String to both the “Event Begin Play” and “Event Tick” of my “Zzz_BP” to see if those would show. Again, nothing. It’s almost like the actor isn’t being seen by the system. I have the in my world. As far as I can tell or find, your actors don’t need to be “registered” in the level blueprint.

I’ve poured over (again and again) the official documents on blueprint referencing (casting, direct communication, event dispatcher, interface) just to see what I was missing but I appear to be following the proper procedures but apparently I am missing * something * but I just don’t know what… I can reference the player blueprint from other objects just fine but just can’t seem to turn that around.

It feels even absurd to have to ask what I’m doing wrong but I just can’t see my mistake. PLEASE PLEASE PLEASE lend a brotha’ a hand?!

You could try casting this way.

To do this you need to understand what a cast does.

A cast will not create an or find anything. It’s part of the inheritance principle. Basically it’s like asking your friend if one of his parents was tall. He can answer yes or no. But you have to ask your friend. If you just ask a wall it won’t answer.

Right now you try to cast a variable from the type you want to itself. This won’t do anything in the first place since you already have the correct type. Casting from something to itself will not change anything. It’s purely to get a child of that actor or check if a specific belongs to that type. But even more importantly you just provide a variable.

It’s a bit like a bucket. You want to check if there is water in it, mud, lava or just nothing. You have a note telling you where the bucket is but you didn’t write anything on that note so all you have is an empty piece of paper.

That’s your newly created variable. It doesn’t contain anything and you can therefore not work with it.

As suggested you could try using “Get All Actors of Class” to get an array containing the objects you are looking for. Those will already be of the type you tried to cast to so it won’t be necessary to cast them again. You can also get objects via overlap events, saving them when you spawn them (since every spawn event provides a reference to the newly created ) or get them from a hit result (which you can get from traces or when your character collides with something).

I hope this helps.

Cheers

Casting might be hard for someone to understand that didn’t code before.

Just like Mr Zarg and Mr said, you need a way to tell player BP to talk with which Zzz_BP actor in the world with. If you got one actor, Mr Zarg’s way of doing it is more than enough. But if you got more than one of those actors, it might be confusing.

In your picture, you need to link that Zzz Ref to “” of casting node. The reason it didn’t work is that, that variable is currently empty. Player Actors that are spawned by player start (which means not put on map directy in editor but used Player Start instead) have zero idea what is happening in the map. You need to either use trace - get hit result, or make an overlap event to fill that Zzz_ref variable. Then you will see it will work.

You can also use Level Blueprint to do this all, but others are way better.

I know I didn’t say anything special as others already helped enough, just wanted to share my thoughts. :slight_smile:

I tried exactly that but cast is still failing:

This was put on my character BP

Thanks, pretty well explained on what a cast is and helping me understand that my variable is empty. As I noted in the comment to, I attempted his solution but still failing the cast.

Also one thing I didn’t mention in the first post was that ultimately this code is to be put on my weapons (which you can/do start the level with) so adding an overlap (I assume) wouldn’t work because it’s already on the character… Although that is an assumption and I will try the overlap event to see if it will load my variable and successfully cast then. I will post my results shortly. Again, thanks for the help.

Well the weapon is supposed to be a separate actor right? Then just spawn it and save the reference.

You need an instance in your level for this to work. All actors will only get the actors in your mail at that point in .

Is ZZZ in the game at this point “Event Begin Play”, if it is not in the game, then it has nothing to reference.

As said, Spawn it, then you have you reference and should not need to cast. Just set it as a Variable on spawn.

In addition to 's answer, here is a link about a short tutorial from me, explaining Pointers (and the Accessed None Error). There is also a second video, explaining casts.

I hope this will help you (: I use very simple examples and brought them over to the UE4

Thanks for the tuts.

When I was first testing this, I added the weapon as a component to the character for testing… Obviously that was a bad move. I have since taken the weapon out of the character, on begin I have had the weapon spawn next to the character then instantly attached the weapon to the character socket. Using that now allows me to cast the variable and use it accordingly. Thanks for your patience and support, it is MUCH appreciated! I’d give you a cookie if I could :wink:

Thanks for helping to simplify the examples. I appreciate the help!

Yeah it was but silly me had the mesh component in the scene, not the one that contained the BP.

, that was very useful… thanks, my problem fixed … : ) ,
Here is my scenario :

1- Assume you have a Spawner blueprint class. This class make numbers of SpawnActors at runtime. Each Actor has a “Public Blueprint variable” named ID, which will assign to a number at “**Spawn” **. Also, this class continuously generate random numbers on Tick and assign it to its public variable which is ID.
So in short, it Spawn couple of Actors with ID and has a random number generator on Tick event.

2- You need to add this class (spawner) to stage from editor. It should be there before running game.
3- In Actor class (which you re going to spawn), you need to use “Get All Actors of Class” on Tick and get your Spawner blueprint and put result in an array. It will be just one because you add just one spawner to stage. From that array simply get the first element and easy use public variable of your Spawner which is ID,.

you can use this in scenario like, if that random number match with the ID of you Actor, then you can play some functions thou.

In this approach you don’t need to use ANY event dispatcher.