0 votes

EDIT TO CODE MADE SEE FOOTNOTE [FOOTNOTE]

Im Currently trying to work out how to make the game restart after players lives hit 0.
This ive managed to do. Game Over screen pops up and then i can hit restart.
The game then restarts From the beginning. Then i hit this issue once i either try to increase the score. or decrease life.

This is my current Restart button code

func _on_Restart_button_down():
    global.score = 0
    global.lives = 3
    get_tree().reload_current_scene()
    get_tree().paused = false

Im Having no issue with the scene reseting but this error only happens when i try to do the following/ IT also happens when i press the pause button:

func updatescore():
    Score.text = str(global.score)

func updatelife():
    Life.text = str(global.lives)
    if global.lives == 0:
        get_tree().paused = true
        PlayPause.hide()
        Overlay.show()
        GameOver.show()

The Above Updates the global score or life var which is nothing more than:

var score = 0
var lives = 3

The second error line is just this line here:

GUI.updatelife()

Ive searched around but im not sure how to fix this issue?
Do i need to free the GUI Scene? Then re add it?
Ive tried this but, But none of the methods Ive tried Have worked.

Basically. After i restart the game the vars should reset And should play the game again like you would expect. With score updating and so on. Any help would be appreciated Thank you

[FOOTNOTE]
I Changed The restart button to this:

func _on_Restart_button_down():
    global.score = 0
    global.lives = 3
    get_tree().reload_current_scene()
    queue_free()
    global.restart("res://Scenes/GUI.tscn")

Then in my global:

onready var currentScene = get_node("/root/Main/GUI")

func restart(scenePath):
    #Free Scene
    currentScene.queue_free()
    #Instance Scene
    var addScene = load(scenePath)
    currentScene = addScene.instance()
    get_tree().get_root().add_child(currentScene) 
    get_tree().paused = false

This Adds a GUI over the top which means the pause button now works BUT it still wont update the score. It hits me with an error.

How can i remove the GUI From The Tree so when its re instanced it works?

in Engine by (51 points)
edited by

Sounds like if you are setting Score.text on a deleted node. Make sure it exists at the point of calling the function.

Can u please show me how you solved this Archtects.
I also have same problem in my project.Please.

1 Answer

+1 vote
Best answer

It looks like Score and Life point to nodes that exist in the scene you're reloading, hence end up pointing to freed nodes after you reload your scene. I assume you set them via onready or within your _ready function. Try setting them again after reload, or access the nodes they point to via their node path each time (e.g. via $...), and the problem should go away.

by (144 points)
selected by

Thank you, This makes so much sense, and kind of annoyed by my self that i didn't think about it!
Cheeers

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.