Player variables that do not reset after death

Issue description

I know you are already planning this, but I couldn't find any ticket on it so I opened one.

Nbt variables were an awesome addition. They made almost everything possible.

But entity variables have an issue. If the player dies, they will reset. It would be cool if we had a new kind of variable that worked exactly like entity variables but only are for players and don't reset after death.

(and if we can choose their names while making procedures, like with nbt variables, it would be even better for cross compatibility with mods)

Issue comments

I will consider adding option GLOBAL_PLAYER that will act as a tag for which player NBT variables to be cloned when the player respawns as cloning on death event is the only way to achieve this.

Whenever you want to preserve player specific variables on death simply create a GLOBAL_SESSION variable:

On player leaves the world: Set Entity data number NBT tag "varX" to Get Global(session): X.

On player joins the world: Set Global(session): X to Get Entity data number NBT tag "varX".

And that is all there is to it (: This will preserve player specific data on death +  on death and leaving the game + on dying and leaving the game directly without respawning and everything.

This is actually the way we would implement this. As this is already possible, I will consider this resolved at this time.

Would it work in multiplayer? And could aren't global session variables shared between all the worlds in a session and reset when it is closed?

(I mean that if a player dies then closes Minecraft without respawning, isn't the session variable lost?)

Every player has their own GLOBAL_SESSION. This is the only one of the 3 that isn't shared between all players.

Dying, clicking title screen and closing minecraft will not clear the variable. Upon joining the world, you'll still be met with the respawn screen and your variable will be stored.

These 3 cases I tested while printing the value in chat succesfully: 

This will preserve player specific data on death +  on death and leaving the game + on dying and leaving the game directly without respawning and everything.

"and could aren't global session variables shared between all the worlds in a session and reset when it is closed?"

This part is irrelevant as we don't use the GLOBAL_SESSION for storing the variable when the game is closed. All of that is done with the NBT tag. It therefore doesn't matter whether they are shared across all dimensions, all worldsaves, as when the game start, they get their value from a world specific NBT tag anyway (:

StellaeLux, nice explanation. This page will be a nice and quality reference to link to when users will ask about this feature :)

Did yo u follow all steps? 

create a GLOBAL_SESSION variable:

On player leaves the world: Set Entity data number NBT tag "varX" to Get Global(session): X.

On player joins the world: Set Global(session): X to Get Entity data number NBT tag "varX".

In what scenario does it not work for you?

I followed the steps you said. I didn't create a debug procedure though, but I will today to check what's wrong

Anyway, I am using the entity nbt variables to store which items has the player traded with an NPC and how many times (it is important for the mod progression). The NPC remembers of you, until you die, then he speaks with you as it was the first time... (he's supposed to remember of you)

You are using the NBT tag to store the number in instead of the GLOBAL_SESSION. We only use the NBT tag to store the value when leaving the game.

You need to use GLOBAL_SESSION in all of your procedures. (:

But I used entity nbt because this way it is compatible with another mod that shares the same nbt variable (once you traded 5 or more items with the NPC, he will turn on an entity variable to make you able to get a treasure hidden in a feature of another mod).

I don't think I can do the same with global variables (maybe I could only keep this variable as nbt - and turn the others into global - and make the player have to speak again with the npc to "remember" where the treasure is)

I think its possible with NBT tags too, but you need to remember:

GLOBAL_SESSION is removed when exiting the game. Entity NBT tags are removed upon dying.

To avoid data loss when exiting the game = NBT tag. (Thus we pass the value to the NBT tag when leaving and getting it when joining)

To avoid data loss when dying = GLOBAL_SESSION. So you can have the player NBT tag get the value from the GLOBAL_SESSION whenever the player respawns.

Because Global_Sessions erase upon exiting the game, we give an NBT_Tag the value of that Global_Session when the player exits the world. The NBT_Tag has the same value as the Global_Session, but is stored even when you close the game:

  1. This is made by creating a new procedure that is triggered when "Player leaves the world". We need to give the NBT_Tag the value of that Global_Session upon closing the game.
  2. You also need to create a new procedure that is triggered when "Player joins the world". We need to give the Global_Session the value that the NBT_Tag has stored while the player was out.

 

To actually change the variable you have to update the Global_Session instead of the NBT_Tag, otherwise it would reset whenever the player dies. In basic terms:

  • Global_Session stores the data while playing the game.
  • NBT_Tag stores the data while the game is closed.

I hope my explanation has helped you in any way crispy_chips1234. Thank you very much StellaeLux for explaining it, I had been trying to find a way around a problem from my mod and you helped me a lot!

Sorry for so many comments, i just figured out how to so the part where i kill the mobs, i still dont know how to activate the code before i die. help me pls