How to make global constant/variable representing a number or image

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By start123

I don’t understand anything in singleton.
I need the data from the scene to go to another.
How can this be done with pictures or numbers?

How can I split this script into two so that it works?

extends Node2D

#This should be the first script.
const appleimage = "res://icon.png"
var word1 = "It's"
var word2 = " apples"
var applenumber = "2"

#This should be the second script.
func _ready():
	$appleimage.texture = load(appleimage)
	$apple.text = word1+word2
func _on_button1_pressed():
	$applenumber.text = applenumber+" apples left"

And also, why this command doesn’t work?

$apple.text = word1\nword2

Parser Error: Parse error: Expected newline after ''.

:bust_in_silhouette: Reply From: jgodfrey

So, create a singleton as described here:

https://forum.godotengine.org/70842/how-to-make-global-constants-and-variables

Let’s assume that script is called Globals.gd

For your specific question, the Globals script could contain the following:

var my_image = preload("res://icon.png")
var my_number = 123

Now, in the script where you want to use the global information:

func _ready():
	$Sprite.texture = Globals.my_image
    var the_value = Globals.my_number

Regarding the concatenation question, I’d do that like:

$apple.text = word1 + "\n" + word2

jgodfrey | 2020-05-19 16:34

Thanks, it helped.

start123 | 2020-05-19 17:24