this is my first project so im still trying to get how things like signals and nodes works in corillation with the scenes.
so i have 2 scenes, in the 1st one i get every card and created an instanse of it using the 2nd one which is working as a template for my different cards (don’t know if it’s bad practice, tell me if it is i just thought it would be better then creating a new scene for it’s card), so i basically use the first to display all the cards ect, on each card i have a toggle button and i want every time i press the button to just update the exp labe.
i tried preload the StartingDeck scene in CardScene.gd and then get the StartingDeck node, i got the node succesfully but i didn’t manage to access the update_ui func in StartingDeck, i also tried with signals and different syntax on them but i can’t seem to get it so i obviously don’t understad something correcly
also question: i can’t have a scene as a chinl to another scene right?
StartingDeck.gd
extends Control
var global_data = preload("res://Global.gd")
var grid_container : Control
func _ready():
grid_container = GridContainer.new()
grid_container.columns = 5
add_child(grid_container)
display_all_cards()
display_exp()
func display_all_cards():
var cards_data = global_data.get_card_data()
var card_scene = preload("res://StartingDeck/StartingCardScene/StartingCardScene.tscn")
for card_name in cards_data.keys():
var card_instance = card_scene.instantiate()
var card_node = card_instance.get_node(".")
card_node.set_power_and_level(cards_data[card_name]["name"], cards_data[card_name]["level"], cards_data[card_name]["power"])
grid_container.add_child(card_instance)
var card_button = card_instance.get_node("./Button")
card_button.connect('exp_updated', self.update_ui.bind())
print(card_button)
for card in grid_container.get_children():
card.custom_minimum_size = Vector2(200, 200)
func display_exp():
var player_data = global_data.get_player_stats()
update_ui(player_data['exp'])
func update_ui(new_exp) -> void:
print('test1')
$Exp.text = str(new_exp)
CardScene.gd:
func update_ui() -> void:
$Name.text = cards_data[card_name]['name']
$Power.text = str(cards_data[card_name]['power'])
$Level.text = str(cards_data[card_name]['level'])
if cards_data[card_name]['unlocked'] == false:
$Button.text = "Learn"
else:
$Button.text = "Forget"
signal exp_updated
func _on_button_toggled(button_pressed):
if $Button.button_pressed == true:
global_data.player_stats["exp"] = global_data.player_stats["exp"]- 10
global_data.card_data[card_name]['unlocked'] = true
emit_signal("exp_updated", global_data.player_stats["exp"])
else:
global_data.player_stats["exp"] = global_data.player_stats["exp"] + 10
global_data.card_data[card_name]['unlocked'] = false
emit_signal("exp_updated", global_data.player_stats["exp"])
update_ui()
how it looks then i run it: