0 votes

Perhaps a dumb question as I'm still quite new to Godot (Loving it so far) but my problem is the following: I've made a really basic catching game where the game follows the mouse and sends params to my backend on 2 occasions.

1st being once the user loses the game when the game will send his current score, time and a hardcoded reward of Lost via the HTTPRequest and the other being once the user gets to 50 points, afterwhich the scene changes and the user has 3 things to chose from, upon clicking it the user gets a "reward" which is sent to the backend and stored in the database.

I've tried many things found on the forums but even though I'm able to "find" the score and time variables from my main scene their result resets to 0 after I leave the first scene and enter the reward scene.
Any suggestions on how to "remember" the 2 varaibles and pass them to my next scene?

The code I have is the following

    #This is the main scene Main.tscn
extends Node
var score = 0
var time = 0
var savegame = File.new()
var save_data = {"highscore": 0}
signal game_over

    var reward = "Lost"
...
#Game lost which works perfectly with the time and score
func _on_Area2D_body_entered(_body):
    $HUD.show_game_over()
    $SpawnTimer.stop()
    $ScoreTimer.stop()
    $Portas.stop()
    var grapes = get_tree().get_nodes_in_group("Grapes")
    for grape in grapes:
        grape.queue_free()
    emit_signal("game_over")
    var format_url = "http://localhost:8080/score?score=%d&time=%d&reward=%s"
    var url = format_url % [score, time, reward]
    $HTTPRequest.request(url, [], true, HTTPClient.METHOD_POST)

#Game won
func _on_Player_hit():
    if score < 50:
        score += 1
    if score == 50:
        $HUD.show_game_won()
        $SpawnTimer.stop()
        $ScoreTimer.stop()
        $Portas.stop()
        clear_screen()
    $HUD.update_score(score)

And the other scene has the following as one of the functions

 #Rewards.tscn
func _on_Barrel01Area_input_event(viewport, event, shape_idx):
    if (event is InputEventMouseButton && event.pressed):
        get_tree().get_root().set_disable_input(false)
        $SmasherSprite/SmasherAttack.play("smash")
        yield(get_tree().create_timer(1.1), "timeout")
        $Barrel01.play("idle")
        yield(get_tree().create_timer(1.1), "timeout")
        random.randomize()
        var x = random.randi()%3+1
        if x == 1:
            var reward = "Mixer"
            var url = format_url % [score, time, reward]
            $Barrel01.hide()
            $Barrel02.hide()
            $Barrel03.hide()
            $SmasherSprite.hide()
            $SelectedRewards.show_Mixer()
            $HTTPRequest.request(url, [], true, HTTPClient.METHOD_POST)
        if x == 2:
            var reward = "Lawnmower"
            var url = format_url % [score, time, reward]
            $Barrel01.hide()
            $Barrel02.hide()
            $Barrel03.hide()
            $SmasherSprite.hide()
            $SelectedRewards.show_Lawnmower()
            $HTTPRequest.request(url, [], true, HTTPClient.METHOD_POST)
        if x == 3:
            var reward = "Airconditioner"
            var url = format_url % [score, time, reward]
            $Barrel01.hide()
            $Barrel02.hide()
            $Barrel03.hide()
            $SmasherSprite.hide()
            $SelectedRewards.show_Airconditioner()
            $HTTPRequest.request(url, [], true, HTTPClient.METHOD_POST)
Godot version Godot 3.3.2
in Engine by (27 points)

1 Answer

+1 vote
Best answer

You should use a Global script

To make the Globals singleton, go to the Script tab in the editor, then click New and a Create Script box will appear, leave everything unchanged except for the Path where you need to insert the script's name Globals.gd.

by (636 points)
selected by

Score and time are still 0 even though I've set it up in both the places and followed the examples given in the link, it works fine on one scene but total resets on the following even though its a global now. I've tried to do this with autoload/singletons previously but couldn't get it to work either (Currently just have var time and var score in the global and in my main and rewards i have either currTime/currScore = Globals.time/score and finalTime/Score = Globals.time/score).

I'm probably mising something very obvious but I'm to inexperienced to figure out what it is. On a side note if I also add onready var Globals = get_node("/root/global/Globals") to the start of it I get an error "Invalid get index 'score'(on base:NiI)."

Tried using a print (Global.score) thoughout my code now so it gets printed every action, noticed that the number remains a 0 even while its increasing in the main screen.

I added the note to 'activate' your globals.

Globals = get_node("/root/global/Globals")

is not how it works ;-)

I've done as stated in the documentation, you only highlighted the part which I've gone through already the only change I've done now is move it down from my global folder to my main folder and as expected nothing else changed in the functionality of my code as it didn't do anthing.
Regarding the Globals = get_node part I've done it without it and tried to play around it like that hence why I stated "if I also add" because I was unsure if I needed it as it didn't make much sense to my as I'm already autoloading it.
The global script works thats not my issue I've made global and autoload elements previously, I can get the score and time without a problem the problem is still the same the global score nor time trasnfer and remember the score from the scene.

Well the solution was to simply get rid of the currScore and currTime and replace all places where it was used with globals.time or globals.score. Thank you for the time

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 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 webmaster@godotengine.org with your username.