Where to use a Transient variable.

I am confused at which occasions do I make a variable Transient. Mostly as far as I have seen generally replicated variables have transient UPROPERTY attached with it. Any reason behind it? And what exactly does it, like why should we use it? https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Properties/Specifiers/Transient/index.html

4 Likes

Transient properties are always initialized to zero and are not serialized to disk. The time to use one is if the object at runtime will set the variable. For instance, let’s suppose you had a character that has MaxHealth as a value setup by the designer, which you treat as const at runtime. At runtime, you’d copy that value into a transient called CurrentHealth. CurrentHealth would go up or down and can be compared to MaxHealth.

11 Likes

Very good to know. goes and makes CurrentHealth Transient :slight_smile:

Hello,

Resurrecting this thread because I’m curious about this: Why would someone use a UPROPERTY(Transient) UObject Ptr;* as opposed to just UObject Ptr*. It seems like UPROPERTY is used to mark things up and provide the ability for reflection. So you’re able to do things like “DisplayAll” on that property. However, are there other advantages to using UPROPERTY in this case? Does any of the serialization differ in this case?

Thanks.

Transient just disables serialization. The other functionality associated with UPROPERTYs would still remain, and be absent with a raw pointer - access through the reflection system, potential access from blueprints, automatic initialization, and perhaps most important of all, automatic referencing which prevents the UObject from being garbage collected.

6 Likes

Thanks for the reply, . I wasn’t thinking much about the GC system, but that makes sense. In my actual example I was debating, I was thinking of using a struct and exposing it as UPROPERTY transient or not. I suppose it only matters in that case if you actually care about the reflection. I’m on a big project where the editor takes ages to load and I was thinking the transient flags (or just not exposing the properties) might help load times… but it’s a lot of work just to figure that out :).

Cheers.

Hello,

I know this thread is a bit old but I’m a bit curious about this. When you say transient removes serialization you make it sound like UPROPERTYs will by default always be serialized and saved to disk (sometime somwhere)? What is the reason behind this default behavior which seems to only really be used on a small part of the game dealing with saving/loading from configs/savegame files?

Is it because it needs to be put into the correct context. Such as the serialization is done by default only for UPROPERTYs in certain classes?

Thanks.

NOTE: I actually just found this mentioned here: https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Classes/Specifiers/Transient/index.html
I had initially checked this one out: https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Properties/Specifiers/Transient/index.html
The last one does not describe the class-specific thing.

New question. Is there some list somewhere which shows all the classes which by default serializes UPROPERTY fields?

I don’t follow exactly what you’re asking. As I understand it the default (not transient) just means that the property won’t be skipped when, for whatever reason, its containing object gets serialized. The serializing context might be in-editor (saving/loading assets to/from disk), it might be a save/load game situation, or potentially anything.

If you can clarify your question I may be able to help, but then again I may not. There’s a lot I don’t understand about this stuff.

Hi joeGraf,

I know this thread is a bit old but I’m a bit curious about this. I dont understand exactly when is needed the use of “Transient”. I understand your sample, but exactly what is the point ? All properties that you modify at runtime need to be Transient for some specific reason, or properties that you modify in runtime and also replicate ?. Exactly why i need to use Transient ?

Thanks,

This makes no sense to me. Firstly he says to use Transient to disable serialization. However then gives an example using CurrentHealth, which goes up/down through game play. When the player stops playing, wouldn’t you want to serialize CurrentHealth so it can be restored latter when they continue playing from where they left off. You wouldn’t load the game with the player back at MaxHealth.

3 Likes

Save game serialization is different and is handled manually, UE4 doesn’t provide a way to serialize runtime state of whole actors out of the box.

Transient disables serialization of a property when the containing object (in this example a character) is serialized to/from disk, which generally means when saving/loading the character blueprint in the editor, or loading it at game startup. A CurrentHealth property has no meaning in those contexts, which is why it would be made transient.

what is the diference between declare variables like this:




UPROPERTY(Transient) //not in disk
float currentHealth;

UPROPERTY() //serializable for disk
float maxHealth;


or

float currentHealth; // same as transient AFAIK

UPROPERTY()
float maxHealth;



3 Likes

Note: This is just AFAIK.

Since you don’t care if a value type is handled by the garbage collection, the only difference between the two is that the non-uproperty version is no longer available to blueprints of that class.

If you mark it as UPROPERTY, blueprint can see it and interact with it depending on what specifiers you give it.
If you don’t mark it as UPROPERTY, you’d have to create UFUNCTION() getters and setters to interact with it.

2 Likes

Good Conversation. It help me a lot to understand what’s mean of UPROPERTY (Transient).:slight_smile:

2 Likes

I just want to say here that treating CurrentHealth in all contexts as a Transient variable is not correct. Let’s say a SP game where you save, close it and load back in again. Is your CurrentHealth going to be reset back to your MaxHealth? Or is it meant to be zero in that case? No. That is not the expected behaviour; CurrentHealth should not be marked Trasient in all cases. The person who has blindly replied to your post saying he’s making his CurrentHealth Transient is probably doing it wrong.

Transient is used for runtime-generated data/objects that shouldn’t or can’t be stored between sessions, such as sockets, oauth sessions, etc.

It can also be used for things that can be derived at runtime form other serialised data. To use your example, if you had a variable for MissingHealth, CurrentHealth and MaxHealth, you could avoid saving MissingHealth because you can save CurrentHealth and MaxHealth and just derive MissingHealth from those 2 other values at load time. This is a very bad example, but I can’t think of another one right now.

6 Likes

@yeah but why? You can also do that with a non-transient variable. What’s the point of it being transient or not, pros and cons.