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"

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?

Product Version: UE 4.21
Tags:
more ▼

asked Feb 15 '19 at 06:07 PM in Using UE4

avatar image

L11777
11 2 3 4

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

1 answer: sort voted first

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

more ▼

answered Feb 15 '19 at 07:23 PM

avatar image

Jon.Lambert
21 1 1

avatar image L11777 Feb 15 '19 at 07:56 PM

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?

avatar image Jon.Lambert Feb 15 '19 at 08:23 PM

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

avatar image Jon.Lambert Feb 15 '19 at 08:25 PM

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

(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