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?

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.

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?

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?

Yeah that worked thanks!!

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

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.