Hi, I’m just looking for a little bit more info on the UPROPERTY and UFUCNTION decorators.

I understand their use in terms of variable exposure to the engine but I’m looking for some more info regarding garbage collection.

Currently, if I have a basic variable like a float which I don’t need exposing to the engine, I’ve not put the UPROPERTY decorator on.
However, if I have a pointer to a UObject like UCameraComponent* , I’ve been putting UPROPERTY() ahead of it even if I don’t need it exposing to the engine. I’m assuming that the basic types like floats don’t need garbage collection while the UObjects do?

Is this the correct way of using UPROPERTY()?

As a follow-up, what’s the correct procedure when using UFUNCTION()? I understand how to use it if I need something exposed to blueprint but what do I do if don’t? I’ve watched and read many tutorials and people seem to put it in random places when they don’t need the function exposed to the engine. Some people put it ahead of every function, some people don’t use it at all.

This as left me a little confused as the to the right places to use both UPROPERTY and UFUNCTION.

Anyone got any clarity on these two for me?

Thanks a lot.

That is correct – UObject is the base class for anything that can be garbage collected and that can have properties that are reflected.
(Actually, I think there’s some smaller class that UObject inherits from, that technically is the garbage collection base class, but for all intents and purposes, UObject is it!)

UFUNCTION() is needed when you want to expose the function to Blueprint, OR when you want to use it in bindable delegates, IIRC. (This may also vary by networked-or-not – I forget the exact details, if I ever knew them…)
Functions that aren’t UFUNCTION() work fine in C++ in single player games, to be sure.

The specific documentation is here: UFunctions | Unreal Engine Documentation
Also, this answer is pretty good: When to use UFUNCTION() macro? - Programming & Scripting - Unreal Engine Forums


What you are talking about is not Garbage Collection but REFLECTION.
Garbage collection uses the reflection to KNOW what needs to be garbage collected.
UFUNCTIONS do net over get garbage collected. But they do get REFLECTED.

so. UOjects get garbage collected. They need to be reflected. If you are not reflecting them, then store them in a safe pointer knowing they could get removed, TWeakObjectPtr<> this allows you to use objects created by other classes without adding a reference to the asset which prevents it from being garbege collected. Each UPROPERTY() on a UOBJECT will add a reference to the garbage collector.

REFLECTION is whole nother story. It is what tells us what things are. So we have the thing, and reflection allows us to talk about what the thing is in code and BLueoprints. This is how we see the varibales in the Blueprint. SO adding UPROPERTY() to a float, REFLECTS the float. It can now be saved to disk, loaded from disk, shown in the blueprints, modified in the blueprints. REflecting a Function with UFUNCTION() allows us to describe the function. Again we can call those function from BLueprints, Delgates, or native code.

So reflection is used for a many number of things. And garbage collectionb baroows the reflection data to accomplish its task.

Could you explain more about how TWeakObjectPtr<> prevents garbage collection because from my understanding of weak smart pointers they don’t prevent garbage collection.

“without adding a reference to the asset, which”

The Reference Prevents it from being Garbage collected. TWeakObjectPtr<> do not add references.

They become invalid when the object is garbage collected.

1 Like

Oh gotcha, my bad, thank you for replying.

This was probably the easiest and best replies about the question on this topic!