Godot Version
4.22
Question
Im trying to get the var Total_Earned into the Lab ges_punkte wich is in a diferent scene and the total Earned is always changin . I tried it make the main scene into a Global but it did not work
4.22
Im trying to get the var Total_Earned into the Lab ges_punkte wich is in a diferent scene and the total Earned is always changin . I tried it make the main scene into a Global but it did not work
If I’m understanding correctly, it looks like setting a global variable as a singleton is what you’re after.
In Project > Project Settings > Autoload; create a new script and we’ll call it something like Global.
~~Global.gd~~
var Total_Earned: int = 0
Then, in your main scripts, whenever you reference it you can either assign it to a script-specific variable (recommended if using multiple times in the script) or use it directly with $GesPunkte.
~~YourOtherScript.gd~~
var earned_coins = Global.Total_Earned
There are other ways to do this, such as creating a resource file to store the data, which doesn’t require you to autoload the file.
extends Resource
class_name Database #or PlayerData or w/e you want to call it
and then the rest is the same as if it’s an autoload.
EDIT: Then, you can use the variable you set as the text label data doing something like:
func _process(delta: float) -> void:
$GesPunkte.text = "*Your Text Here*: " + str(your_set_variable)
Thanks For Explaining . Im still pretty new to this so i dont realy get it i have tried using your ideas . sorry for not getting it
( It is a cockie cklicker like game and i need the Total_Earned( which is just what it s called) to show up in the label in the Prestige Window and later to calculate the Prestige points ) . Thanks for helping
Not a problem that you’re new! We’re all new once.
In your Global.gd you have it extending Node, when it should extend Resource (if you are not using an autoload).
If you’re new, I would recommend this tutorial on Youtube; it helped me a lot with sharing data across scenes when I was first starting.
EDIT: When posting code, it is preferable to copy/paste the code or a link to a pastebin. This allows us to see all of the code so we can better analyze it.
To post it on the forums with correct formatting, place three ` marks (the one next to the 1 key, not an asterisk) at the end and at the beginning. This will keep the formatting of the code.
So i just coppy the code in the chat ? I already whatched the exact yt vod before coming here xD and still could not get it i have now tried making the global a Recause but nothing changed
extends Resource
#global
var Total_Earned : int = 0
extends Node
#Gammanager\ Main
var score = 600000000000
var Total_Earned = Global.Total_Earned
extends Node2D
#Presige
@onready var ges_punkte = $GesPunkte
var total = Global.Total_Earned
func _ready():
ges_punkte.text = str(total)
So, from what it looks like in that code, you aren’t actually updating the total_earned in Global.gd. Global variables have to be manually called and updated since they’re in a global setting. These global resources can also have functions, however, and may be what you need to get it to update. For example:
~~Global.gd~~
extends Resource
var Total_Earned: int = 0
func update_total_earned(value) -> void:
Total_Earned += value
~~Gamemanager/Main.gd~~
extends Node
var total_earned = Global.Total_Earned
func update_player_score() -> void:
total_earned.udpate_total_earned(1) #the 1 is passed back to the Global Variable script function and updates the Total_Earned variable
# for testing, put the 'update_player_score()' value in '_process()', keeping in mind this updates every frame
func _process(delta: float) -> void:
update_player_score()
~~Prestige.gd~~
@onready var ges_punkte = $GesPunkte
func _process(delta: float) -> void:
ges_punkte.text = "*Coins/Gems/Etc*: " + str(Global.Total_Earned)
the final function in your Prestige.gd is simply setting the text value and adding the Global.Total_Earned as a string ‘str()’.
So i just found out by printig the Global.Total_Earned that it stays 0 but the normal Total_Earned is still increacing
Im gonna send you some part of the code bc i still dont get it sorry
extends Node
#Gammanager\ Main
var score = 600000000000
var Total_Earned = Global.Total_Earned
var clickwert = 1
func _on_button_pressed():
score += clickwert
Total_Earned += clickwert
func update_player_score() → void:
Total_Earned.udpate_total_earned(1)
#whenever i put in the next thing from your code it shows an eror (Invalid Call Nonexistent funcion update )
extends Node
#global
var Total_Earned : int = 0
func update_Total_Earned(value) → void:
Total_Earned += value
extends Node2D
#Presige
@onready var ges_punkte = $GesPunkte
var total = Global.Total_Earned
func _process(delta: float) → void:
ges_punkte.text = "Coins/Gems/Etc: " + str(Global.Total_Earned)
If you know any other way that i might better undertand im open to it xD
and thanks for helping
So for the function update_player_score()
, that should be in the Global.gd script.
~~Global.gd~~ #Make sure the *script* is in an autoload, not the scene as a whole. this would be a .gd file instead of .tscn
extends Resource
var Total_Earned: int = 0
#Leave 'value' here in Global.gd; this will be fed from your other script.
func update_player_score(value):
Total_Earned += value
Then, in your Gammanager script, you should only update the Total_Earned variable.
~~Gammanager.gd~~
var score = 600000000000
var clickwert = 1
func _on_button_pressed():
score += clickwert
Global.update_player_score(clickwert) # using 'clickwert' here, which is set to a value of '1', will feed 'value' in your Global.gd with the value you give it.
Is your score variable supposed to be mirroring the Total_Earned? Or is that to represent a maximum value? If it’s a maximum, I’d suggest setting it as:
const SCORE = 6000000000000
the ‘const’ makes it so Godot will tell you if you try to change it, and the all-caps is just a naming convention. If it’s not supposed to be a maximum, then please ignore that!
The high score is only because i tested the “Autominers”
and it shows an eror in global
if you have dc and are willing to call I could stream my screen for you to see my code . you can add me #ruudlii
If you’re brand new to programming, this 3-hour tutorial taught me almost all of the basics. I only won’t add you because I, myself, am not wholly versed in programming/godot. But that video is very thorough and, so long as you can get through it (I broke it down into 3-day sessions) it will teach you virtually everything you’ll need to know to make a game (a 2D Platformer, but skills translate).
i completed this yt 2d platformer tutorial https://youtu.be/LOhfqjmasi0?si=ayQDIvatp2moUykn and did some extra stuff so im not completly new so you think its still worth watching it ?
Oh it works now i dont know why and How but i does .
Thanks a lot for your help and Patience
When you want some data to persist between scenes, you can make a class an autoload. This class will be loaded at the start of the game and remains even when changing scenes.
class_name Database
static var Score : int = 0
There are also getters/setters that help you share data. So whenever you have something within the scene that needs to share that value, you can make a variable that automatically updates by sharing that data.
class_name ScoreBoard extends Node
var Score : int :
get : return Database.Score
Now you can use this new variable called Score inside your label. The get function will always return the value from the database.
var Score : int :
set(value) : Database.Score = value
func ChangeScore() -> void:
Score += 10 # Adds 10 to the score.
The set function will allow you to change the value from inside a class. So you can increase or decrease the score when something happens.
class_name Database
static var Chest_List = {"Lv1_Chest1": false, "Lv1_Chest2": true}
class_name Chest extends Node
@export var Chest_ID : String
var Is_Chest_Open : bool :
get : return Database.Chest_List[Chest_ID]
set(value) : Database.Chest_List[Chest_ID] = value
func _ready():
if Is_Chest_Open == true:
# State = Opened Chest
else:
# State = Closed Chest
func Pickup():
if Is_Chest_Open == false:
Is_Chest_Open = true
# Perform Animation and Add items
# State = Opened Chest
Or you can do something like this with an entire list. Where you assign a unique name to every chest within the game and use this list to save/load the state of every chest. So when the player opens a chest, they can go back to that location and see the chest is still open.
I used a Save thing so i save the var like a save game and then load it when entering the prestige scene but i think your solution is prob easyer to do so thanks will use it next time i need to do that
static var Score
the ‘static’. I always forget the static in my Globals!
Remember to add → void too. It might not seem like much, but all those static references like declaring variable types can impact performance. They don’t really matter much for smaller games, but they might for large games.
Inside Project Setting > General (Advance Settings) > Debug > GdScript and change the Untyped Declaration to warn you.