![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | jebenipapa |
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)