How to make a pointer to an object/class?

I was suggested to use casting, but I can’t find any examples that make full sense in terms of casting. Think of having a player character class, the character class having a camera and in another class I’m trying to get the location of the camera. I need to get a pointer to the player character object or class, but how do I do that?

This is a very broad question that doesn’t have a succinct answer. I recommend watching Zak Parrish’s video on blueprint communication. It’s all in blueprint, but the concepts are the same.

https://forums.unrealengine.com/unreal-engine/events/72934-training-stream-blueprint-communications-with-zak-parrish-february-23rd

As far as casting syntax, you’ll typically want to use a templated cast like MyCharacterClass* MyCharacter = Cast<MyCharacterClass>(PointerToObject). How you get the PointerToObject is up to the way your game works. If it’s something you’re going to access a lot from various places, you might consider having the character register itself on the gamemode, or some management class on BeginPlay(). You can also use the TActorIterator to iterate through all of the actors in your world, but that’ll be a lot slower.

Best,

Jonathan Lambert

Yeah, the main problem is precisely the PointerToObject. I wouldn’t need to use casting if I could just get the pointer anyway. I need to get the pointer. And I have no idea how, because the object I want to get the pointer of does not exist in the world. It is created by another class when the game starts.

To be fair, I’ve worked with another engine and even there it’s something that just wouldn’t work like that. But I need some class code to be applied to each player character individually and without having any reference or include in the player class file itself of the class that gets reference from the player characters created during gameplay. Is this in any way possible?

There are a number of ways to achieve what you are talking about. Interfaces, delegates, reference caching, and runtime iteration of objects are all options, but the best approach(s) is highly project dependent. I think the video in the link I posted above will really help you understand better than anything I can type here.

Best,

Jonathan Lambert

Also, check out the gameplay framework to help you understand the default building blocks of an Unreal game.

https://docs.unrealengine.com/en-us/Gameplay/Framework

Best,

Jonathan Lambert