Godot Label doesn't respond/show when being called inside a class method

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ddarkmoto
:warning: Old Version Published before Godot 3 was released.

Hi everyone. I’ve been having a problem calling the label from a class that I did. It doesn’t show or respond whatsoever. I hope that you could enlighten me a bit because I might’ve miss a lot. Here’s my code, and thank you in advance.

enter code here
extends PopupPanel

var obj = preload("res://object_stack.gd")

var slot_size = 32
var c = 6

var DRAG = true
var MOVING = false

var old_m_pos = 0
var slotname = []

var click = false

var timer
var label


class Slot:
extends Control

 var container = null
 var item_stored = null
 var quantity = 0
 var label

 var mouse_click = false
 var en_timer
 var timeout = .8

func _init(c):
container = c


 func _ready():
 en_timer = Timer.new()
 en_timer.set_one_shot(true)
 en_timer.set_wait_time(timeout)
 en_timer.connect("timeout", self, "mouse_click")
 add_child(en_timer)

 set_size(Vector2(32 + 2, 32 + 2))
 set_process(true)

func mouse_click():
 mouse_click = false

func _draw():
 draw_rect(Rect2(Vector2(0, 0), Vector2(32, 32)), Color(1, 1, 1, 1))

func update_qty():
 if(mouse_click == false):
    mouse_click = true
    quantity += 1
    print("------")
    print("q "+str(quantity))
    set_tooltip("test")
    print("Updating...")
    print("------")

    #this is where the problem occurs
    print("------")
    label = Label.new()
    add_child(label)
    label.show()
    label.set_align(Label.ALIGN_RIGHT)
    label.add_color_override("font_color", Color(0, 0, 0))
    label.add_color_override("font_color_shadow", Color(50, 50, 50))
    label.set_text(str(quantity))
    label.set_pos(Vector2(get_size().x-40, 0))
    label.set_size(Vector2(40, 10))

    print("get text "+str(label.get_text()))
    print("------")
    if mouse_click == true:
     en_timer.start()
     print("time start")

   func update_slot(last_item):
    if mouse_click == false:
     mouse_click = true
     var slotx = get_node("/root/inventory")
     var sprite = Sprite.new()
     add_child(sprite)
     print("update is activated with item "+str(last_item))

     quantity += 1

     if (last_item == "item"):
        item_stored = "item"
        #sprite.set_texture(preload("res://icon.png"))
        #sprite.set_scale(Vector2(.35, .35))
        #sprite.set_pos(Vector2(16, 16))
        #set_tooltip(str(item_stored))
     elif (last_item == "apple"):
        item_stored = "apple"
        #sprite.set_texture(preload("res://apple.png"))
        #sprite.set_scale(Vector2(.05, .05))
        #sprite.set_pos(Vector2(16, 16))
        #set_tooltip(str(item_stored))

  if(mouse_click == true):
    en_timer.start()


 func _input_event(event):
 #slot name
if event.type == InputEvent.MOUSE_BUTTON:
    print("name "+str(get_name()))
    pass

 #end class

func on_update_contents():
 pass

func checkslot():
 var index = c
 return index

func _ready():

 for i in range(c):
  var slot = Slot.new(self)
  slot.set_name("slot_"+str(i))
  slotname.append(slot)
  add_child(slot)
  slot.set_pos(Vector2(6+(32 + 5)*(i%5), 25+top_space+(32 + 5)*(i/5)))
  slot.set_ignore_mouse(false)
  set_process(true)

 func update_slot():
  var inv_slot = get_node("/root/inventory")
  #correct implementation

 if slotname[inv_slot.index].item_stored == null:
   slotname[inv_slot.index].update_slot(inv_slot.last_item)
 elif slotname[inv_slot.index].item_stored != null:
   slotname[inv_slot.index].update_qty()
   #print("updated: "+str(slotname[inv_slot.index].get_name()))

 func process_update():
  var item = get_node("/root/item")
  if(item.mouse_click == true):
  set_process(true)
  pass


 func _process(delta):
   var slot_stat = get_node("/root/inventory")
   if slot_stat.index != null:
    if slotname[slot_stat.index].item_stored == null:
      slotname[slot_stat.index].update_slot(slot_stat.last_item)

I don’t think we know enough from these snippets to answer.
Is the Slot control being added as a child to the container?
What type of node is the container?

CowThing | 2017-06-09 16:42

Indeed it’s not clear whether that code is called at all.

Besides I am wondering why you are creating a new label everytime you call update_qty. I mean I don’t know what exactly the code is meant to accomplish, but it looks weird to say the least.

Warlaan | 2017-06-09 16:56

Hi CowThing, yes the slotn is being parented to a popup panel. Im sorry thise are selected codes that i though ti be significant to my inquiries. I’ll post the rest as soon as I get up. Thank you very mych for the prompt reply.

ddarkmoto | 2017-06-10 01:12

Hey warlaan. I’m creating a slot for the item. I instantiated every slot based on the code, and the purpose of the label is for counting the item inside the instantiated slot. Hope that helps :slight_smile:

ddarkmoto | 2017-06-10 01:16

did you check live scene tree at debugger > remote inspector?

volzhs | 2017-06-10 15:00

Hi. Yes I checked the remote inspector and it’s there but it doesn’t show when being executed.

ddarkmoto | 2017-06-11 14:39

:bust_in_silhouette: Reply From: ddarkmoto

Hi everyone. I was able to fix the problem by updating the func update_slot() with this following modifications:

func update_slot(item_name):
var inv_slot = get_node("/root/inventory")
#correct implementation
var slotnamex = slotname[inv_slot.index].get_name()
print(slotnamex)
if get_node("/root/Control/Bag/"+str(slotnamex)).item_stored == null:
	get_node("/root/Control/Bag/"+str(slotnamex)).update_slot(inv_slot.last_item)
else:
	get_node("/root/Control/Bag/"+str(slotnamex)).update_qty(item_name)

I simply changed the object’s full path to make it work. Thanks everyone for your help. Cheers!