0 votes

-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

in Engine by (376 points)

1 Answer

+1 vote
Best answer

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)
by (9,782 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.