Show a node after another node is clicked

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

I’m doing a point and click game and I have some items that combined to create a new one

Is there a way for showing a new item once they are combined?

I tried this, but the only way it shows up is when i exit the room and enter again :cry:

func hide_object(): 
var Soup = $Soup
var IngrE = $"Ingredient E"
Soup.visible = false
IngrE.visible = false

if Global.Data["Collected Objects"]:
	if !"Cane" in Global.Data["Collected Objects"]:
		pass
	else:
		IngrE.visible = true
		
		

if Global.Data["Texts"]:
		if "Woman2" in Global.Data["Collected Objects"]:
			Soup.visible = true
else:
	pass

It sounds like your only calling hide_object(0 when you first enter a room. You need to call it when the contents of Global.Data[“Collected Objects”] changes.

SteveSmith | 2022-04-26 20:30

It sounds like your only calling hide_object(0 when you first enter a room. You need to call it when the contents of Global.Data[“Collected Objects”] changes.

How can I do this update? will it be on the Global singleton or inside this code?

TheCoinCollector | 2022-04-27 06:58

sorry if i ask basic questions. is the first game i try to do by myself.
you mean i call the hide_object on the func ready ? how can I make it update to show the items?

func _ready():
connect_signals()
add_collected_to_list()
remove_objects_from_scene()
hide_object()
hide_button()
$Fader.fade_to_transparent()

this is the hide object code

func hide_object(): 
var Soup = $Soup
var IngrE = $"Ingredient E"
Soup.visible = false
IngrE.visible = false

if Global.Data["Collected Objects"]:
	if !"Cane" in Global.Data["Collected Objects"]:
		pass
	else:
		IngrE.visible = true



if Global.Data["Texts"]:
		if "Woman2" in Global.Data["Collected Objects"]:
			Soup.visible = true
else:
	pass

TheCoinCollector | 2022-04-27 07:16

Call it when you change the contents of Global.Data[“Collected Objects”], e.g… when you add an object like “Cane” to it. Maybe call it via a signal. I don’t know how you’re code is laid out so I can’t be more specific.

Also, I’d change it to a class collected_objects.gd as well, with methods like “add_collected_object()” and “remove_collected_object()” and “contains_object()” or similar.

SteveSmith | 2022-04-27 07:38

I have to admit i feel dumb AF… I tried calling a signal without success,tried also adding a variable that calls the hide_object and tried using a super class with the same luck :frowning:

I feel I might have done a bit of a spaghetti code trying to achieve what I aim with the point and click game. If I understand correctly you say to make everything a different script and then call them in the global or when they are needed (this would be to polish the script style)?

For the interaction of the items with the inventory, I have 3 scripts (i want to add a 4th for the inventory itself which will be its own scene).
The scripts are:
Global - singleton called for dictionaries and saving the data)
Objects - Used to make items interactable ( i wanted to simplify and export all items and keys so it was easier to make items but is still a prototype)
Room01 has the rest of all the actions. I have around 10 rooms and my idea was to copy all the code on each room and adapt what was needed on each.

To make the function hide_object() run again i should remove it from the func _ready() and add it only on the add_collected_to_list()?

I place the scripts below to try to see what I’m doing wrong :')

Global.

    extends Node

var mouse_norm = load("res://Assets/Cursor1.png")
var mouse_click = load ("res://Assets/Cursor2.png")
var mouse_camera = load("res://Assets/Arrow.png")



var Data = {
	"Collected Objects": [],
	"Texts": [], 
	"Unlocked_Doors":{	"Alchemy Lab Door": false,
						"Corridor01 Door": false,
						"Corridor02 Door": false,
						"Corridor03 Door": false,
						"Corridor04 Door": false,
						"Corridor05 Door": false,
						"Kitchen Door": false,
						"Outside Door": false,
						"Room05 Door": false,
						"Room08 Door": false,
						"Secret Room Door": false,
						"Secret Society Door": false,
					}
					#add any other list with a "," and the name
					# {} =  Dictionary, set of data that contains info, strings, lists, other dictionaries [] = List 
}

var Game_Objects = [
						"Cane", 
						"Doc Coat", 
						"Empty Jar", 
						"Filter Jar", 
						"Firefly", 
						"Game Piece 1", 
						"Game Piece 2", 
						"Game Piece 3", 
						"Gold", 
						"Ingredient A", 
						"Ingredient B", 
						"Ingredient C", 
						"Ingredient D", 
						"Ingredient E", 
						"Jar Blue Filter", 
						"Jar Glow Fireflies", 
						"Jar Glow Urine", 
						"Loaf Bread", 
						"Mandolin String", 
						"Medallion", 
						"Potato", 
						"Potion", 
						"Recipie", 
						"Ring", 
						"Rock", 
						"Scroll", 
						"Soup", 
						"Urine"
					]
var Game_Characters = [
						"Doctor",
						"Farmer",
						"Gossip",
						"Guard 2",
						"Guard",
						"Jester",
						"Kid",
						"King",
						"MadMan",
						"Woman"
					]
var Game_Texts = [
					"Woman0",
					"Woman1",
					"Farmer0",
					"Farmer1",
					
				]

var Keys = [
				"Alchemy Lab Key",
				"Corridor01 Key",
				"Corridor02 Key",
				"Corridor03 Key",
				"Corridor04 Key",
				"Corridor05 Key",
				"Kitchen Key",
				"Outside Key",
				"Room05 Key",
				"Room08 Key",
				"Secret Room Key",
				"Secret Society Key"
			]
var Game_Rooms = []

func _ready():
	copy_objects()
	load_data()
#	print(Data)
	OS.shell_open(OS.get_user_data_dir()) #PLACE WHERE WE SAVE DATA

func save_data():
	var f = File.new()
	f.open("user://Data.dat", f.WRITE)
	f.store_line(to_json(Data))
	f.close()
	
func load_data():
	var f = File.new()
	if f.file_exists("user://Data.dat"):
		f.open("user://Data.dat", f.READ)
		Data = parse_json(f.get_as_text())
		f.close()
func copy_objects():
	var dir = Directory.new()
	if dir.open("user://") == OK:
		dir.make_dir("user://Objects")
		for obj in Game_Objects:
			dir.copy("res://Assets/Items/%s.png" %obj, "user://Objects/%s.png" %obj)
	if dir.open("user://") == OK:
		dir.make_dir("user://Rooms")
		for room in Game_Rooms:
			dir.copy("res://Assets/Rooms/%s.png" %room, "user://Rooms/%s.png" %room)
#	if dir.open("user://") == OK:
#		dir.make_dir("user://Texts")
#		for text in Game_Texts:
#			dir.copy("res://Assets/Texts/%s.txt" %text, "user://Texts/%s.txt" %text)

Object

extends Sprite

var mouse_enter = false
var clickable = false
var is_active := false


### Locks
export var item_key: String
export var item_locked: String
export var unlock_image: String

var tex
signal unlocked

# Called when the node enters the scene tree for the first time.
func _ready():
	$Area2D.connect("mouse_entered", self, "_mouse_entered")
	$Area2D.connect("mouse_exited", self, "_mouse_exited")
	
	#make default required item per item null // NOT SURE IT WORKS
	item_key = "null"
	item_locked = "null"
	unlock_image = "null"
	
func _process(delta):
	if !$Area2D.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):
	### Adding items to data and deleting item
	if Input.is_action_just_pressed("mouse_click") and mouse_enter == true and clickable == true:
		Global.Data["Collected Objects"].append(self.name.capitalize())
		_mouse_exited()
		get_parent().get_node("ItemList").add_icon_item(self.texture)
		print(Global.Data["Collected Objects"])
		Global.save_data()
		self.queue_free()
		
		
	### Locking items
	if Input.is_action_just_pressed("mouse_click") and mouse_enter == true:
		if get_parent().item_selected == item_key and Global.Data["Unlocked_Doors"][item_locked] == false:
			unlock(item_key)

func unlock(key):
	emit_signal("unlocked", key)
	Global.Data["Unlocked_Doors"][item_locked] = true
	Global.save_data()
func check_unlock(door):
	if Global.Data["Unlocked_Doors"][item_locked] == true:
		self.queue_free()
	
	
	
	
	
	
	
	
	
	
	
	
#func _on_item_clicked(item):
#  if not item.is_active:
#    return
#  ItemList.add_icon_item(item)

Room01

extends Node2D

var i = 0
var tex
var item_selected = ""
var map_display = false
var mouse_enter = false
var destination = ""
var clickable = false

export var key: String
export var Door: String
export var Unlock_Door: String

signal hide_object

# Called when the node enters the scene tree for the first time.
func _ready():
	connect_signals()
	add_collected_to_list()
	remove_objects_from_scene()
	hide_object()
	hide_button()
	$Fader.fade_to_transparent()

func connect_signals():
	$ItemList.connect("item_selected", self, "on_item_selected")
	$ItemList.connect("nothing_selected", self, "on_nothing_selected")
	for child in self.get_children():
		if "Lock" in child.name:
			$Lock.connect("unlocked", self, "remove_key")

func create_texture(obj):
	var img = Image.new()
	img.load("user://Objects/%s.png" %obj)
	img.load("user://Characters/%s.png" %obj)
	tex = ImageTexture.new()
	tex.create_from_image(img)

func add_collected_to_list():
	for obj in Global.Data["Collected Objects"]:
		create_texture(obj)
		get_node("ItemList").add_icon_item(tex)
		emit_signal("hide_object")
		

func remove_objects_from_scene():
	if Global.Data["Collected Objects"]:
		for item in Global.Data["Collected Objects"]:
			if "_"  in item:
				item = item.replace("_", " ")
			remove_item_from_scene(item)
	if Global.Data["Texts"]:
		for item in Global.Data["Texts"]:
			if " "  in item:
				item = item.replace(" ", "")
				remove_item_from_scene(item)
func remove_item_from_scene (item):
	for child in self.get_children():
		if item == child.name:
			get_node("./%s" %item).queue_free()

func on_item_selected(idx):
	item_selected = $ItemList.get_item_text(idx)
	if "Cane" in item_selected:
		$ItemList.unselect_all()
func on_nothing_selected():
	$ItemList.unselect_all()
	item_selected = ""

func _unhandled_input(_event):
	if mouse_enter == true and Input.is_action_just_pressed("mouse_click"):
		get_tree().change_scene("res://Scenes/Rooms/%s.tscn" %destination)


###Hide objects directly on the scene if action is not meet, Global.Data["Texts"] for dialogs and Global.Data["Collected Objects"] for items
func hide_object(): 
	var Soup = $Soup
	var IngrE = $"Ingredient E"
	Soup.visible = false
	IngrE.visible = false

	if Global.Data["Collected Objects"]:
		if !"Cane" in Global.Data["Collected Objects"]:
			pass
		else:
			IngrE.visible = true



	if Global.Data["Texts"]:
			if "Woman2" in Global.Data["Collected Objects"]:
				Soup.visible = true
	else:
		pass


#For texts showned
func hide_button():
	var Woman0 = $Woman0
	var Woman1 = $Woman1
	Woman0.visible = true
	Woman1.visible = true
	
	if Global.Data["Texts"]:
		if !"Woman0" in Global.Data["Texts"]:
			pass
		else:
			Woman0.visible = false
	if Global.Data["Texts"]:
		if "Woman0" in Global.Data["Texts"]:
			pass
		else:
			pass

###	Rooms and Door Passages (with possible locks)

#Corridor01
func _on_Corridor01_Area_entered(viewport, event, shape_idx):
	mouse_enter = true
	if !"Cane" in Global.Data["Collected Objects"]:
		if Input.is_action_pressed("mouse_click"):
			if get_node_or_null('DialgNode') == null:
				get_tree().paused = false
				var dialog = Dialogic.start('Cant')
				dialog.connect('timeline_end', self, 'unpause')
				add_child(dialog)
#				
	else:# "Cane" in Global.Data["Collected Objects"]:
		destination = "Corridor01"

func _on_Corridor01_Area_exited():
	mouse_enter = false
	destination = ""

###		Dialogs  
func unpause():
	get_tree().paused = false
##################################
func _on_Woman0_pressed():
	var a = "Woman0"

	if !"a" in Global.Data["Texts"]:
		Global.Data["Texts"].append(a.capitalize())
		print(Global.Data["Texts"])
		Global.save_data()

	if "a" in Global.Data["Texts"]:
			$Woman0.queue_free()

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

	else: 
		$Woman0.queue_free()

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('Woman1')

			dialog.connect('timeline_end', self, 'unpause')
			add_child(dialog)
			get_node("Woman1").queue_free()
	else: 
		$Woman1.queue_free()

TheCoinCollector | 2022-04-27 08:38