需要找出哪个按钮被按下。

huangapple go评论74阅读模式
英文:

Need to find out which button was pressed

问题

以下是您提供的代码的翻译部分:

下面的脚本的任务是扫描给定的目录并找出其中有多少个文件(或字符)。然后,它创建与文件总数相等的按钮。目前我的问题是,在运行时我无法知道哪个按钮被按下。

extends Control

# 要导入的字符数量
var characters = 0
# 所有可用字符的文件名
var files = []
# 与这些字符关联的按钮
var array = []

# 当节点首次进入场景树时调用。
func _ready():
    scan_directory()
    populate()
    setup()

# 扫描文件夹中的字符数量
func scan_directory():
    var current = ""
    var dir = DirAccess.open("res://Characters/")
    dir.list_dir_begin()
    current = dir.get_next()
    while current != "":
        files.push_back(current)
        current = dir.get_next()
    print(files)
    characters = files.size()

func setup():
    var x = 0
    for i in characters:
        var but = array[x]
        var path = "res://Characters/" + files[x] + "/" + files[x] + ".gd"
        var char = load(path)
        var dic = char.import()
        x += 1

# 创建与字符数量相等的按钮
func populate():
    var x = 100
    var y = 100
    var texture = Texture2D.new()
    texture = load("res://img/button.jpg")

    for i in characters:
        var but = Button.new()
        var path = load("res://Scripts/Character_Select.gd")
        add_child(but)
        but.pressed.connect(self.export)
        but.set_position(Vector2(x, y))
        but.set_button_icon(texture)
        x += 250
        if x > 1250:
            x = 100
            y += 250
        array.push_back(but)
    print(array)

func export():
    var temp = self.get_instance_id()
    print("当前ID:" + str(temp))

希望这能帮助您解决按钮点击时传递变量或获取被点击按钮的实例ID的问题。

英文:

The following scripts job is to scan the given directory and find out how many files(or characters) are in it. Then it creates a number of buttons equal to the total number of files. Currently my problem is that i have no way knowing which button is pressed during run time.

extends Control
#number of characters to be imported
var characters = 0
#file names of all avalible chararters
var files = []
#buttons associated with said characters
var array = []
# Called when the node enters the scene tree for the first time.
func _ready():
scan_directory()
populate()
setup()
#scans for the number of characters in the folder
func scan_directory():
var current = ""
var dir = DirAccess.open("res://Characters/")
dir.list_dir_begin()
current = dir.get_next()
while current != "":
files.push_back(current)
current = dir.get_next()
print(files)
characters = files.size()
func setup():
var x = 0
for i in characters:
var but = array[x]
var path = "res://Characters/" + files[x] + "/"+ files[x] + ".gd"
var char = load(path)
var dic = char.import()
x += 1
#creates buttons equal to the number of characters
func populate():
var x = 100
var y = 100
var texture = Texture2D.new()
texture = load("res://img/button.jpg")
for i in characters:
var but = Button.new()
var path = load("res://Scripts/Character_Select.gd")
add_child(but)
but.pressed.connect(self.export)
but.set_position(Vector2(x,y))
but.set_button_icon(texture)
x += 250
if x > 1250:
x = 100
y += 250
array.push_back(but)
print(array)
func export():
var temp = self.get_instance_id()
print("Current id: " + str(temp))

So im currently looking for a way to either have the button pass a varible when clicked or be able to know the instance id of the button that was clicked so i can compare it to know buttons in the array.

Any help is appreciated.

答案1

得分: 1

连接信号时:

but.pressed.connect(self.export)

你传递的是从方法 export 创建的一个 Callable。你可以像这样绑定参数:

but.pressed.connect(self.export.bind(1))

然后当信号被发射时,它会将其作为参数传递给方法 export。当然,你需要一个参数来接收它,例如:

def export(data):
    print(data)
    # ...

在你的情况下,你可能想传递迭代变量。这是想法:

for i in blahblahblah:
    # ...
    # 一些操作
    # ...
    but.pressed.connect(self.export.bind(i))
    # ...
    # 其他操作
    # ...

由于每次都会绑定不同的值,这允许你知道它是哪个值。

当然,如果你愿意,你也可以绑定其他东西。

英文:

When you connect to the signal:

but.pressed.connect(self.export)

What you are passing is a Callable created from the method export. You can bind arguments to it, like this:

but.pressed.connect(self.export.bind(1))

Then when the signal is emitted, it will pass that as argument to the method export. You, of course, need a parameter to receive it, for example:

func export(data):
prints(data)
# ...

In your case, you probably want to pass the iterator variable. This is the idea:

for i in blahblahblah:
# ...
# stuff
# ...
but.pressed.connect(self.export.bind(i))
# ...
# other stuff
# ...

Since each time it would be binding a different value, this allows you know which one it is.

Of course, you can bind something else if you prefer.

huangapple
  • 本文由 发表于 2023年6月22日 11:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定