Understanding UObjects at the Risk of Sounding Dumb

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