0 votes

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 '\'.

in Engine by (43 points)

1 Answer

+1 vote
Best answer

So, create a singleton as described here:

https://godotengine.org/qa/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
by (14,748 points)
selected by

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

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

Thanks, it helped.

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.