Give an "item" after button is pressed

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

Not sure why the code block works wrong. I apologize in advance.

I have this game where i would like to keep track of the dialogs that have been played in order to give the player a “item”/“key” or show a new dialog.
I managed to make dialogs appear and disappear by creating buttons.
I would like that once the X button is pressed, the button will emit the visual dialog (this is working atm), and write itself inside a list that is located on a Global singleton (in order to keep a track of the texts.

The global.gd has a var called Data shown below

var Data = {
"Texts" : []
}

The objective of this “item” would be to keep a track of which dialog has already been played in order to: show a new dialog, open a new room.

My code for the button trying to get the “item” to appear on the global list

  extends Button

var mouse_enter = false
var clickable = true


# Called when the node enters the scene tree for the first time.
func _ready():
	$".".connect("mouse_entered", self, "_mouse_entered")
	$".".connect("mouse_exited", self, "_mouse_exited")

func _process(delta):
	if !$".".get_overlapping_areas():
		clickable = true

func _mouse_entered():
	Input.set_custom_mouse_cursor(Global.mouse_click)
	mouse_enter = true
	
func _mouse_exited():
	Input.set_custom_mouse_cursor(Global.mouse_norm)
	mouse_enter = false


func _unhandled_input(_event):
	if Input.is_action_just_pressed("mouse_click") and mouse_enter == true and clickable == true:
		Global.Data["Texts"].append(self.name.capitalize())
		_mouse_exited()
		print(Global.Data["Texts"])
		get_parent().get_node("ItemList").add_icon_item(self.texture)
		Global.save_data()
		self.queue_free()

My code for the scene that has the button and emits the signal (simplified)

extends Node2D

###Dialogs
func unpause():
get_tree().paused = false

func _on_Farmer0_pressed():
if get_node_or_null(‘DialgNode’) == null:
get_tree().paused = false
var dialog = Dialogic.start(‘Farmer0’)

dialog.pause_mode = Node.PAUSE_MODE_PROCESS

	dialog.connect('timeline_end', self, 'unpause')
	add_child(dialog)

get_node(“Farmer0”).queue_free()

func _on_Farmer1_pressed():
if get_node_or_null(‘DialgNode’) == null:
get_tree().paused = false
var dialog = Dialogic.start(‘Farmer1’)
dialog.pause_mode = Node.PAUSE_MODE_PROCESS
dialog.connect(‘timeline_end’, self, ‘unpause’)
add_child(dialog)

:bust_in_silhouette: Reply From: TheCoinCollector

Found a solution to the issue.
The way to make it work was to add the variable inside the signal pressed.

func _on_Woman1_pressed():
var a = "Woman1"
if !"a" in Global.Data["Texts"]:
	Global.Data["Texts"].append(a.capitalize())
	print(Global.Data["Texts"])
	Global.save_data()

if !"Cane" in Global.Data["Collected Objects"]:
	if get_node_or_null('DialgNode') == null:
		get_tree().paused = false
		var dialog = Dialogic.start('Farmer1')
#		dialog.pause_mode = Node.PAUSE_MODE_PROCESS
		dialog.connect('timeline_end', self, 'unpause')
		add_child(dialog)
		get_node("Woman1").queue_free()
else: 
	$Woman1.queue_free()

after it it was needed to add the function to hide in case that the name was on the list already