Hello all. I am trying to make a key appear once player has collected 50 coins. I have almost made this work by setting the key's visibility to "false" and then once a coin is collected visibility = true and then I enable the collision mask. Problem is the game either crashes when I collect a coin or when I grab the key once it's appeared. I have code in a "HUD" node that keeps track of coins and hearts and that is where I originally tried putting the code but I couldn't make it work. So I copied the code that keeps track of coins and reused it on the "KEY" node. I am wondering if having the same code basically running twice is what's causing the issue. Unfortunately I can't remember what the specific error message was. I quickly commented the code so the game would not be broken.
All the nodes are children of the "Level01" parent. Any help you could offer this noob would be most appreciated. Thank you! 😄
HUD NODE SCRIPT:
extends CanvasLayer
var coins = 0
onready var key = $Key
var hasKey := false
func _ready():
$Coins.text = String(coins)
load_hearts()
Global.hud = self
func _on_coin_collected():
coins = coins + 1
_ready()
if coins == 01:
get_tree().change_scene("res://YouWinScreen.tscn")
func load_hearts():
$HeartsFull.rect_size.x = Global.lives * 53
$HeartsEmpty.rect_size.x = (Global.max_lives - Global.lives) * 53
$HeartsEmpty.rect_position.x = $HeartsFull.rect_position.x + $HeartsFull.rect_size.x * $HeartsFull.rect_scale.x
KEY NODE SCRIPT:
extends Area2D
export(NodePath) onready var hud = get_node(hud)
#var coins = 0
func _on_Key_body_entered(body):
visible = false
set_collision_mask_bit(0,false)
SoundPlayer.play_sound(SoundPlayer.SOUNDKEY)
hud.key.texture = load("res://STUFF TO IMPORT/USE THESE FIRST/KEYS/hudKey_yellow.png")
hud.hasKey = true
queue_free()
#func on_coin01_coin_collected():
#coins = coins + 1
#ready()
#if coins == 01:
#visible = true
#set_collision_mask_bit(0,true)
