Announcement

Collapse
No announcement yet.

Where to use a Transient variable.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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...ent/index.html

    #2
    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.

    Comment


      #3
      Originally posted by joeGraf View Post
      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.
      Very good to know. *goes and makes CurrentHealth Transient*
      Journeyman's Minimap - Available on Marketplace - Forum topic - Video

      Comment


        #4
        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.

        Comment


          #5
          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.

          Comment


            #6
            Thanks for the reply, kamrann. 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.

            Comment


              #7
              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...ent/index.html
              I had initially checked this one out: https://docs.unrealengine.com/latest...ent/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?
              Last edited by Insanez; 10-13-2015, 11:41 AM.

              Comment


                #8
                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.

                Comment


                  #9
                  Originally posted by joeGraf View Post
                  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.
                  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,

                  Comment


                    #10
                    Originally posted by joeGraf View Post
                    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.
                    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.

                    Comment


                      #11
                      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.

                      Comment


                        #12
                        what is the diference between declare variables like this:

                        Code:
                        UPROPERTY(Transient) //not in disk
                        float currentHealth;
                        
                        UPROPERTY() //serializable for disk
                        float maxHealth;
                        
                        
                        or
                        
                        float currentHealth; // same as transient AFAIK
                        
                        UPROPERTY()
                        float maxHealth;

                        Comment


                          #13
                          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.

                          Comment

                          Working...
                          X