"RESETING" score,health and other variables.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By THE HELIX

Sorry since i am a noob in godot programing can anyone explain the answer to the question in the link :
https://forum.godotengine.org/33518/previously-freed-instance-restarting-trying-update-score?show=33518#q33518
Here he has selected the correct answer but i didn’t understand how to code it.
So please can anyone help me in explaining and coding the answer as i have the same problem in my project.

:bust_in_silhouette: Reply From: kidscancode

This is really going to depend on how you have things set up. The person in the linked question seems to have a very specific setup using nodes for score and life, which wouldn’t apply to you if you’re using variables for them.

Regardless, it could be as simple as calling

get_tree().reload_current_scene()

to restart everything, or you may need to write a function that resets the values of the variables and you call that when you want to reset.

If you include more information about what you have so far, I could try and be more specific.

Thanks for your guidance.
Here is the project :
I created a level 1 scene with coins the player collectedenter image description here

I attached the codeenter image description here

to the play button and to the coin label this code
enter image description here

when i press the PLAY button the Game_1.tscn opens , here i created the walls using tilemap and applied it a codeenter image description here

and whenever the player hits the wall his life gets decreasing and becomes 0 .Then the Game_1.tscn ends.I applied a area2d for the player to detect collisions with the codeenter image description here

I created a block in the walls .For the block i added the code.enter image description here
I again added another Area2D for the player and added the code .enter image description here
So that when the player touches the block , the level ends and a coin is added in the Level 1 scene , but no coin is added when he touches it.
The code for the player is enter image description here
here the 2 Area2D’s is the child of player.
The autoload scripts for coin , life isenter image description here

When i exit Game_1.tscn and again press play it wouldn’t reset itself ; it had same life value before i exited the scene . In the code when life becomes 0 it would open the Level 1 scene and wouldn’t open again when i press play as the life is 0. So attached a new autoload script for scene changing enter image description here
And for the play button enter image description here
The game_1 scene opens and when i again press play i getenter image description here
So can you please give me suggestions how to solve the scene changing problem and coin collecting.
[ i first used set_text (game.coin_current) for the coin label]
and how can i save the coin so that when the player plays Level 1 again the coins he collected in the game should add to the amount of coins he had earlier . how can i the save the amount of coins after i quit the game.
I know it is a long question since i am a noob in coding can you please help me ?

THE HELIX | 2019-02-14 04:13

If you want to preserve the value of the variable after a restart, put the variable in your autoload script. Then it will be independent of the scene.

When you call goto_scene() the current scene is freed. So current_scene.free() will fail.

Also, a couple of tips:

  • When you add an autoload, you don’t have to use get_node("/root/global"), you can just access it by global.goto_scene() - in the Autoload tab you can set the name it will be referred by.

  • Remove all those pass lines. They do nothing but clutter up your code.

kidscancode | 2019-02-14 22:04

thanks for your answer. when i remove current_scene_free() from the global and added global.goto_scene() . When i press play the level 1 scene merges with the game_1.tscn and the game crashes.enter image description here
enter image description here
When i removed the life_current_1 from game script and added to the label which had life value and removed the game global from area 2D ; i get the error as “life_current_1 not found”.
Can i send you my project and tell me where i am doing wrong ? please.

THE HELIX | 2019-02-15 05:36

Could u please show me how to seperate the life_current variable from the game script and should I put the life _current value to area2D or label script ?

THE HELIX | 2019-02-15 18:42

Post a link to your project and I’ll try to look at it this weekend.

kidscancode | 2019-02-15 18:55

I figured out how to separate the value of life from autoload and save the coins but i have 2 questions
1 : how to make the label show the life value decreasing to 0
enter image description here
here is the code for Area 2d , i need to make the label show the values of life points
Here is the path of label
enter image description here
Here Life_2 is the label i need

2 : How to add the amount of coins collected in a level to the current amount of coins already present in the game ?

Can you help me to solve those two problems?

THE HELIX | 2019-02-16 15:53

Can you solve the two problems which I posted above please ?

THE HELIX | 2019-02-16 18:36