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"

Finding Objects, Casting, and pointers

I've read that TObjectIterator is used to find any class, How do you use it. Can someone show me some example code of how to use it to find a custom class?

Also how does casting work. Say I'm looking to cast to my custom character class Called AMainCharacter.

AMainCharacter* mainChar= Cast(GWorld->GetFirstPlayerController()); would just get a ACharacter right? Not a AMainCharacter? Can someone show me example code and explain please.

How could I find or set a pointer to a custom class. Like for instance I made a UMG Class called UItemWidget derived from UUserWidget what would be the best way to get reference to that UItemWidget. Would it be storing a pointer, if so what is the best way (with code example)? Or are there Unreal functions that can find that class? In bp Get Actors of Class doesn't even allow me to select UItemWidget in that dropdown, its not even listed. How could I find a custom class like this?

Product Version: Not Selected
more ▼

asked Oct 28 '14 at 03:41 AM in C++ Programming

avatar image

134 39 35 40

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

1 answer: sort voted first

For your first point you probably don't want to search all available actors or iterators for a specific instance of a class, but there are a variety of simpler use cases in engine code that you can reference as an example. Here's an example

For casting you need to include the template value so it would be AMainCharacter* mainChar= Cast(GWorld->GetFirstPlayerController()->GetPawn()); //note i added the GetPawn bit as I asume you dont want to cast a playercontroller to a character.

I'm having trouble understanding your third question. I think you might want to do some standard C++ tutorials and lookup the topic of Encapsulation. What you want is a pointer to a class instance returned from some class that has owner of that pointer. The class instance should have getters/accessors for the members that are publicly available.

more ▼

answered Oct 28 '14 at 05:24 AM

avatar image

723 33 30 56

avatar image CJ31387 Oct 28 '14 at 07:05 AM

You almost completely answered this but I have 1 more question. To the casting part. What if its a really custom class. Like a UInventoryWidget that derives from UUserWidget . Get pawn is for characters, How can i find ANY class. Finding UUserWidget would not work with getpawn correct? Or if i wanted to find a class that is not even a actor, but some derivative of another non actor class?

avatar image dzeligman Oct 28 '14 at 10:09 PM

If you just want the type you can include it and do MySpecialClass::StaticClass(). If you have an instance of a class you can do ->GetDefaultObject() to get the default object. If you want a particular instance of something special that is a non actor you'll have to obtain a reference as you would any other normal C++ class.

avatar image CJ31387 Oct 28 '14 at 08:45 AM

By the way

 AMainCharacter* mainChar= Cast<AMainCharacter>(GWorld->GetFirstPlayerController()->GetPawn());

doesn't work.

I need to pass data from UItemWidget to AMainCharacter to UInventoryWidget All these classes don't know about any of each other. How can I reference them?

avatar image schmit Oct 28 '14 at 10:07 PM

There's a typo in there. It should be:

 AMainCharacter* mainChar= Cast<AMainCharacter>(GetWorld()->GetFirstPlayerController()->GetPawn());

Rama's tutorial is great for learning object and actor iterators

avatar image CJ31387 Oct 28 '14 at 10:19 PM

Yeah that worked thanks!!

(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