Inheritance question, how does one apply custom functions to new created instances?

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

-Reference: Using Godot 3.02

Hi guys, I devloping a inventory and stumbled across another problem. I create a button to each item in my array, with the following code:

var UPDi = 0
	while UPDi < InventoryItems.size():
		var newInventoryItemButton = Button.new()
		newInventoryItemButton.set_text(str(InventoryItems[UPDi].Name))
		InventoryListVBox.add_child(newInventoryItemButton)
		UPDi += 1

I simply want to define automatically functions inside the new buttons, like:

ItemUse(): #Use item and removes it.

ItemDrop(): #Remove item and create a instance near the player.

But I want to define these one time, and apply them in my code. Would be like:

var UPDi = 0
	while UPDi < InventoryItems.size():
		var newInventoryItemButton = Button.new()
		newInventoryItemButton.set_text(str(InventoryItems[UPDi].Name))
		InventoryListVBox.add_child(newInventoryItemButton)
		UPDi += 1

		#WHAT I WANT TO ACHIEVE
		newInventoryItemButton.ApplyMagicAutomaticFunctions()

How I would do that? I am trying for the first time to wrap my head around inheritance, but can’t understand it.

Your help is appreciated!

TBCK

:bust_in_silhouette: Reply From: volzhs

I think you can use signal for that.

newInventoryItemButton.connect("pressed", self, "ItemUse", [ InventoryItems[UPDi] ])

and then define ItemUse function

func ItemUse(p_what):
    printt("use", p_what.Name)

Thank you Volzhs! That did the trick in another way, and I learned a lot from those simple lines, much appreciated.

The_Black_Chess_King | 2018-03-22 20:50