Problem manipulating variables in a singleton

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

Hello! I am currently trying to get a hang of some basic in Godot features (I am a complete beginner), but I have run into a problem and I can’t find any solution online.

Right now, I have two buttons in my “game”: “Bed” and “Work”. To go to work, you have to get up first. I wanted to set up things so two different messages may appear after pressing “Work”, depending on whether “Bed” was pressed before. This is the code I used in singleton:

extends Node
var gotUp = 0
func _on_Bed_pressed():
	gotUp = 1

The code for the message that appears if “Bed” wasn’t pressed:

extends RichTextLabel
var dialog = ["You need to get up first."]
var page = 0
func _ready():
	set_bbcode(dialog[page])
	set_visible_characters(0)
	set_process_input(true)
func _on_Work_pressed():
	if scn_house.gotUp == 0:
		set_visible_characters(get_total_character_count())

The code for the message that appears if “Bed” was pressed:

extends RichTextLabel
var dialog = ["You went to work."]
var page = 0
func _ready():
	set_bbcode(dialog[page])
	set_visible_characters(0)
	set_process_input(true)
func _on_Work_pressed():
	if scn_house.gotUp == 1:
		set_visible_characters(get_total_character_count())

Unfortunately, the function in the singleton doesn’t seem to work. The last message (“You went to work”) never appears and this code

func _on_Work_pressed():
	print(scn_house.gotUp)

always prints “0”. What am I doing wrong? Any help will be appreciated.

because you put “extends Node” in your singleton’s script, now it is considered as a node and you can refer to your “gotUp” variable from any script by using command like this :

get_node("/root/global").gotUp = 1;

(if your singleton is called global.gd)

nonomiyo | 2018-07-25 14:10

Thank you, I got it to work :slight_smile:

masterocookies | 2018-07-25 15:55

:bust_in_silhouette: Reply From: ferrai

Looks like you didn’t connect the singleton function to the button signal. You can add a script to the “Bed” and call the singleton function when the button is pressed.