x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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:

alt text

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:

alt text

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?!

Product Version: Not Selected
Tags:
zzz_bp.png (276.8 kB)
hero_bp.png (294.0 kB)
more ▼

asked May 02 '15 at 10:41 AM in Blueprint Scripting

avatar image

jtsmith
1.4k 70 29 92

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first

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.

Cheers

more ▼

answered May 02 '15 at 10:54 AM

avatar image

Erasio
5.9k 291 86 293

avatar image jtsmith May 02 '15 at 11:58 AM

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 Narg, 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.

avatar image Erasio May 02 '15 at 12:26 PM

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 time.

avatar image jtsmith May 03 '15 at 03:05 AM

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 ;)

avatar image eXi May 02 '15 at 03:25 PM

In addition to Erasio'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.

https://www.youtube.com/watch?v=PmcaFCMDnqQ

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

avatar image jtsmith May 03 '15 at 03:02 AM

Thanks for the tuts.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

You could try casting this way.

alt text

Narg

more ▼

answered May 02 '15 at 10:46 AM

avatar image

Narghile
1.7k 74 38 110

avatar image jtsmith May 02 '15 at 11:52 AM

I tried exactly that but cast is still failing:

alt text

This was put on my character BP

hero_bp2.png (238.5 kB)
avatar image Narghile May 02 '15 at 12:31 PM

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 Erasio said, Spawn it, then you have you reference and should not need to cast. Just set it as a Variable on spawn.

Narg

avatar image jtsmith May 03 '15 at 03:07 AM

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

avatar image emperor_katax Nov 06 '17 at 02:42 PM

Narghile , 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” time. 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.

https://answers.unrealengine.com/questions/722703/spawnactor-get-data-from-other-blueprint-without-u.html

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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. :)

more ▼

answered May 02 '15 at 11:20 AM

avatar image

Achilleon
782 38 34 70

avatar image jtsmith May 03 '15 at 03:06 AM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question