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 "Object" input node of the Cast directly to the "Zzz_BP" object. 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 "Object" that is linked to the "Zzz_BP". I plug this reference into the "Object" 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 object 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?!
asked May 02 '15 at 10:41 AM in Blueprint Scripting
To do this you need to understand what a cast does.
A cast will not create an object 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 object 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 Narghile 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 object) or get them from a hit result (which you can get from traces or when your character collides with something).
I hope this helps.
answered May 02 '15 at 10:54 AM
You could try casting this way.
answered May 02 '15 at 10:46 AM
Casting might be hard for someone to understand that didn't code before.
Just like Mr Zarg and Mr Erasio 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 "Object" 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. :)
answered May 02 '15 at 11:20 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here