Understanding UObjects at the Risk of Sounding Dumb

I really need some help understanding this, being I feel like it is causing a big gap in my abilities. So what I understand about an object is that it is an instance of a class. So if I have a Character class and the character I spawn in at run time would be an object of that class.

Assuming that is right, it is really racking my brain to understand how to reference that object. I an reference the class as a whole, but I don’t even remotely understand how to say “this one” when I am coding. Especially if the object doesn’t even exist until run time, then how do I point to it? I have watched hours and hours and hours of videos and I just cant figure it out. I don’t even know what to search anymore.

Can anyone please help me understand this. When I try to use functions that want an object passed in I just dont understand how to tell it which one and I cant keep moving on until I get this. Every CPP and UE 4 tutorial I watch just tells me what the two things are and I already know that, I dont need a definition.

Can you give an example of one of the specific scenarios you’re struggling with where you need to reference something and also mention where you’re trying to reference it from?
You’re right that you need a way to get a reference to whatever object it is that you want to access. The exact way you would do that will vary depending on where you’re writing the code, what it is you want to get a reference to, and how you set things up. There’s also often multiple ways to do it

A uobject is the base unreal engine class to which you can apply UPROPERTIE if I remember correctly, but I think you are asking the wrong question.

What is it that you are trying to achieve?

From what I am reading, you are creating an asset at runtime, and want to find the reference to that asset after being created?

If so, you’re supposed to store the reference after creation, not search for it.

I’m new to Unreal but this is more of a general object oriented programming concept so I feel I can answer.

You will always need some kind of link/path between the location you’re writing the code and the specific object instance you are trying to get a reference to.

Sometimes that’s easy, like if you’re trying to reference a specific Component on an Actor and you’re doing that from your Actor class itself. In that scenario you can just use something like the FindComponentByClass function that exists on Actors and there you go, that gives you a reference to a specific instance of that component on that actor.

Other times it requires more thought and planning. Here’s some examples of different scenarios off the top of my head:

  • Sometimes you might need to use something more global like the GetWorld function and then drill down from there to locate the object. For example if you need to get a reference to your player, you could use something like GetWorld()->GetFirstPlayerController()
  • If your code that needs to get the reference is actually located in the same object where you’re writing the code, you can use the this keyword to refer to the current instance the code is being executed in.
  • You might need to capture a reference to the object as you spawn it in and store that somewhere that your code can access later (hard to be more specific without an example of what you’re trying to do).
  • You could even loop through all the objects of that class type in the entire level and look for something that is unique to this instance you want to get a reference to (if there isn’t anything unique about it, you’d need to add something when it gets created). That would be the least efficient way though.
  • If you want to detect when the player (or any actor) enters a trigger, the function that gets called when the actor overlaps the trigger box will always pass in a reference to the actor instance that entered the trigger. The official documentation for this seems to be pretty lacking but search for something like “UE4 actor beginoverlap C++” on google and you’ll find plenty of examples

So I have had this issue couple times. This time I am having issues with Montage_Play(). One of the params is a UAnimInstance, and I just dont know how to tell it to use the montage I made. I dont get it.

Have you tried this:

UPROPERTY(EditAnywhere)
TSubclassOf<UAnimInstance> YourInstance;

345823-2021-08-08-01-10-04-window.png

UFUNCTION(BlueprintCallable)
void Example(TSubclassOf<UAnimInstance> Instance);

345824-2021-08-08-01-15-08-window.png

I’ve not used the animation stuff myself yet but if you look for tutorials on that, surely they show how they’re getting a reference to the animation they want to play?