For what ever reason, I am having the hardest time grasping how to use ‘Cast to ____’ nodes. I tried to create the most simplistic scene possible to test this out, and I can’t get it working.
First blueprint “BlueprintA”. It generates a random integer and stores it as the variable “PrintMe”
Second Blueprint, “BlueprintB”. Attempts to reference “PrintMe”, and prints it to the screen on Begin Play.
For the life of me I don’t know what to plug into the ‘Object’ wire. Could someone please explain this to me?
Are you trying to print the value stored in one of our blueprint’s variables? If so, you don’t need a cast at all, you just need to do a “get” node to retrieve the value. Right-click on the graph and type “get Print Me”, and you should get a node that will give you access to that. Then delete the cast and target nodes and hook Begin Play directly up to Print Text, and hook the Get Print Me to the To Text (Int) input pin
Casts are used when you have an object and you know it’s of a different class, usually (But not always) a subclass or descendent of the class Blueprint thinks you have. A Cast tells Blueprint to convert it to the actual class so you can access additional functions or properties the subclass has that the parent doesn’t.
When I right click and choose “get PrintMe”, I get the following node that is throwing the error:
This is actually where I started, but a user on the answerhub told me I HAD to use a ‘cast to’. I’m finding this all very frustrating when I’m only trying to do a very simple thing and no one can give me a straight answer.
In order to access a variable from BP_A in BP_B you first need to have a reference of BP_A in BP_B (if that makes sense) so what you are trying to do is not going to work like that
I suggest you read into blueprint communication.
Casting is like a conversion between object types. So if, for example, I have an Event Hit, and I have the hit actor, and I know that the hit actor in this situation is going to be some sort of BP_Lamp, I can Cast the generic Actor var to a BP_Lamp var which allows me to call a function on it (such as FlickerLights, for instance).
For what you’re attempting, you need to know how to find the specific BP in question. Remember, Blueprints are like BLUEPRINTS, in that they specify the layout of an object that can exist multiple times. “Blueprint A” does not refer to a SPECIFIC object, it refers to a TYPE of object, of which there may be any number in the level.
You want BlueprintB to talk to BlueprintA… But you must understand that for this to work, BlueprintB must know WHICH BlueprintA you’re talking about! It’s a reference to that specific object which you need, and from THERE you may or may not need to cast.
You’re trying to learn blueprint communication with too vague a set of starter tools. Without some way of knowing which Blueprint A to talk to (it was the one we just overlapped, it was the one underneath this line trace, it was the one spawned when I pressed the Y key, whatever) you’re stuck.
As an example, try this: on BeginPlay of Blueprint B, run a “Get All Actors of Class” with a class of “Blueprint A”. Then, connect that Array of actors to a ForEach loop… And from the output pin of that loop, under the Loop Body execution, run your Get PrintMe > IntToText > PrintText
Thank you very much, that actually helps a lot. I will try that out a little later when I have some time.
As a non-programmer I’ve been finding the blueprint system very frustrating. I follow tutorials, explore the content example files, watch videos etc- But it feels like trying to learn English by reading the dictionary. You start out with “a” and then follow it with “aardvark” and immediately you’re at a deadend… I’ll keep trying, thanks again.
So a user on answerhub finally gave me the key words I was looking for; Get all actors of class. I suspect when I start building more complex scenes the advice you gave me, RhythmScript, will come in handy, but for now I just needed the baby steps of getting this very simple script working.
This is the way I always do it but I always think it can’t be the best way. It seems like a sledge hammer to crack a walnut but until someone tells me a better way I’ll keep doing it like this. Another way is to dynamically spawn the actor from your second blueprint then collect that reference in a var.
I’m not coming from a ‘true’ programming background (by trade I’m a web developer) and for me the BP system works really well. I think it’s just a case of familiarizing yourself with what each node does, or at least what it sounds like it can do. They’re all named so it’s easy to understand really. it just takes practice.
You’ll be creating your own blueprints and game functions etc in no time I assure you!