Download

Explanation of object references

Hello,

Could someone please explain object references? I’m having trouble understanding how to properly utilize them. When (event), where (which BP), and how do I best initialize them? Do different types of object references need initialized differently? How do these differ from class references?

I feel like this is fundamental to using blueprints but can’t find a comprehensive explanation of them. I’m sure there are others having trouble grasping this.

Thank you!

Well it’s all programming so any information about referencing in programming is applicable in blueprints. Reference (computer science) - Wikipedia To answer your specific questions we need more specific details I think? Maybe I’m just not understanding?

From What i’ve seen and used them for, it’s useful for setting the variables of that blueprint/object and getting states of its partsScreenshot 2015-07-23 09.39.41.png

I understand why to do it, but I have trouble understanding where and when to do it. For instance, if I have a blueprint whose variable I need access to in another blueprint, what are the steps to do to initialize it? If I try to access the variable without any initialization, and run it through an “Is Valid” node, it returns invalid.

An example could be registering damage. From the bomb or explosive or whatever use a reference to the first person character BP and use a get health, float-float and a set health node.

What are the nodes I need to initialize a BP variable in another BP?

IE: Set, IsValid, etc???

You would have a reference to the other blueprint and drag off of that node the stuff you wish to access in that other blueprint. For example you can put a Cast to Third Person Character in your Level Blueprint and off of that Cast node, get the character’s Character Movement component.

Is that what you are referring to?

It really depends on the situation as there are many ways to get references to an object. The most simple way is, say you want to access your character blueprint from another blueprint then you would use the built in function ‘get player character’ and cast it to your custom bp type/class

Ok, how about a custom event in one blueprint that I want to call in another? It won’t work because I haven’t initialized the blueprint with the custom event, how do I initialize it?

35578a9ded341a990acc12238c92c36f8815a2b1.jpeg

If you want a pawn reference as a direct variable of the object type, you could do “get player pawn” on “event begin play” and set your object to that. Then it becomes a reference to the actual pawn in the world. You may have to do a cast-> to the exact object type your pawn is.

You could also just do “get player pawn” every time you want to use it, but I prefer to do it ahead of time on begin play so that you can avoid having to cast all the time. Once you have stored the pre-cast object reference you get easy access to all the variables in the object.

The other case for making an object reference is when you want to pass a variable from one BP to another just once, but have the 2nd bp contain the live values of the 1st bp’s version of that variable without any further events.

If you wanted to do that, you would create an “object” blueprint that simply has a variable of the type you want to pass. Then in any BP that you want to contain that variable, you crate a new variable, the type would be the name of your object BP. then you do “set [variable name]” or “get [variable name]” etc to set the values.

Then in your functions that pass values, mark them “by reference” and then when you receive the object in the 2nd bp, you store it in an object variable of the same name as the object BP you created. Then whenever the object in BP 1 changes variable values, BP 2 now stores a reference to the same object. If you do the same thing with a regular variable, the value will remain the same as the initial state at the time of the event/set unless you keep passing updates which is slow.

I hope that made sense.

A common thing to do is to have an object that nicely packages information about something using either a bunch of variables or a struct of some kind. Then you can simply replicate the object which tidies up the replication.

Remember, anytime you rely on these pre-setup variable for core gameplay, it is a good idea to use a macro like “IsValid?” and print some very obvious string in case you somehow have a null pawn. That can save time when things go wrong.

so that’s Get BP Char you want to Set BP Char and it’s initialized (Alt+click drag from the variables on the left). Like Ryan said a good place to do this is in event Begin Play. Here’s a quick example I wipped up in about 2 minutes on my own project http://i.imgur.com/YaumaIe.png