Can someone explain soft variable references?

it seems to have to do with garbage collection and memory usage. its explained pretty well in the stackexchange post linked below.

1 Like

Soft variable references…

  1. What are they?
  2. How are they used?
  3. WHY would I use them?
  4. Can someone give me a good example use?

Simple terms please!!! Thanks in advance!

1 Like
  1. They are made to keep refrences to objects or assets that may not exist and referencing assets without loading them. Soft object variables also wont create hard references to blueprint. While normally object references would throw you errors, if you would delete asset and dont fix refrences that could damage the blueprint. In technical terms, they hold path to the object or asset, similar way as ini config does or data tables.
  2. Same as object reference practically, blueprint will auto cast to object refrence if you want to call function on asset or send it, when used asset will be automatically loaded. If object won’t exist the cast would give null, you can check validity beforehand too
  3. As side of refrencing assets that may not exist at runtime, many possibile reasons, one is to reference asset without loading it when it not really needed, can be used for optimization. You can actully (and i didn’t know that myself checked that just now) reference objects on the level, without it needed to be loaded. You can actually make soft reference and set actor on the level as default. Any reason you can think of when object may not need to be loaded and or asset may not exist at the time of the use
  4. I don’t use them much myself so i don’t have any, but i can explain what they do.
9 Likes

Can someone give me a good example
use?

I’ve been using soft references recently when building an inventory crafting system of sorts.

  • I want to show an image of an item in the inventory without spawning the actual item.
  • The user will browse a lot of items and I do not want to spawn anything until they’re happy with their selection.
  • I want to show just the image in the user interface and maybe some variables that define it, stats and so on.
  • Stats are easy, but the image - Texture2d is actually an object itself; an object that does not exist until its owner is instantiated.

My object class has soft referenced Texture2d that I just fetch from the class defaults and load that bit only instead:

11 Likes

Can someone give me a good example use?

Soft References are one of the way to reference objects in sublevels from the persistent level. Hard references don’t work, as you can’t be sure whether or not the sublevel is loaded.

3 Likes

There’s no quick answer to this but…

If reading is your preferred learning approach:

If video serves you better:

8 Likes

Thanks alot for sharing the links, I’ve already watched the stream but the article is far more interesting and clear :wink: