Where to use a Transient variable.

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.

9 Likes