Understanding "Reference"

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By peterhoglund

I came across the built-in Reference type and as I understand it I think it would suit one of my needs. But I would like to check if I have understood it right. Here is my scenario:

I have a class InventoryItems (weapons, armor, etc) that are shown in various views (shops, drops, inventory, equipped inventory, etc). Items are always represented visually by an Item-scene which holds a reference to the InventoryItem. InventoyItems themselves are never in the scene-tree. Is this a good case to let InventoryItem inherit from Reference? The way I understand it is that if an InventoryItem is no longer used it will automatically be removed from memory and I would not have to free it myself like if it was an Object type.

Thank you!

:bust_in_silhouette: Reply From: sash-rc

Yes, Reference is basically an Object for which you don’t have to care about free(), otherwise you’ll get memory leaks for a later one.

And yes, it’s a good approach when you split entity into non-visual data structure (Object/Reference/Resource) and its visual representation (Node) in a scene tree.

Great, thank you!

peterhoglund | 2021-09-08 14:55