The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

So for my first game in godot I've been making a platformer. I have an area2d with a death script collisionshape2d as a child. The code for the script is

(sorry I can't make the sample code feature work)


extends Area2D

var deaths = 0

func onbodyentered(body :PhysicsBody2D):
deaths = deaths + 1
print(deaths)
gettree().changescenetofile("res://lv_1.tscn")


This resets the player as I want, but what's printed in the console is always 1, no matter how many times the player dies. What am I doing wrong?

Godot version 4.0.1 (I think)
in Engine by (12 points)

Now I'm no professional, but try making the script a singleton (Project>Project Settings>AutoLoad)

1 Answer

0 votes

Hi,
I don't work because you reload the entire scene with this script as well. This recreate everything including your counter go back to 0.
You need to save the counter in a singleton (in Autoload as mention by INeedMentalHelp) or change the way you reload your scene.

by (116 points)
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.