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

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

Need to find out which button was pressed

问题

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

  1. 下面的脚本的任务是扫描给定的目录并找出其中有多少个文件(或字符)。然后,它创建与文件总数相等的按钮。目前我的问题是,在运行时我无法知道哪个按钮被按下。
  2. extends Control
  3. # 要导入的字符数量
  4. var characters = 0
  5. # 所有可用字符的文件名
  6. var files = []
  7. # 与这些字符关联的按钮
  8. var array = []
  9. # 当节点首次进入场景树时调用。
  10. func _ready():
  11. scan_directory()
  12. populate()
  13. setup()
  14. # 扫描文件夹中的字符数量
  15. func scan_directory():
  16. var current = ""
  17. var dir = DirAccess.open("res://Characters/")
  18. dir.list_dir_begin()
  19. current = dir.get_next()
  20. while current != "":
  21. files.push_back(current)
  22. current = dir.get_next()
  23. print(files)
  24. characters = files.size()
  25. func setup():
  26. var x = 0
  27. for i in characters:
  28. var but = array[x]
  29. var path = "res://Characters/" + files[x] + "/" + files[x] + ".gd"
  30. var char = load(path)
  31. var dic = char.import()
  32. x += 1
  33. # 创建与字符数量相等的按钮
  34. func populate():
  35. var x = 100
  36. var y = 100
  37. var texture = Texture2D.new()
  38. texture = load("res://img/button.jpg")
  39. for i in characters:
  40. var but = Button.new()
  41. var path = load("res://Scripts/Character_Select.gd")
  42. add_child(but)
  43. but.pressed.connect(self.export)
  44. but.set_position(Vector2(x, y))
  45. but.set_button_icon(texture)
  46. x += 250
  47. if x > 1250:
  48. x = 100
  49. y += 250
  50. array.push_back(but)
  51. print(array)
  52. func export():
  53. var temp = self.get_instance_id()
  54. 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.

  1. extends Control
  2. #number of characters to be imported
  3. var characters = 0
  4. #file names of all avalible chararters
  5. var files = []
  6. #buttons associated with said characters
  7. var array = []
  8. # Called when the node enters the scene tree for the first time.
  9. func _ready():
  10. scan_directory()
  11. populate()
  12. setup()
  13. #scans for the number of characters in the folder
  14. func scan_directory():
  15. var current = ""
  16. var dir = DirAccess.open("res://Characters/")
  17. dir.list_dir_begin()
  18. current = dir.get_next()
  19. while current != "":
  20. files.push_back(current)
  21. current = dir.get_next()
  22. print(files)
  23. characters = files.size()
  24. func setup():
  25. var x = 0
  26. for i in characters:
  27. var but = array[x]
  28. var path = "res://Characters/" + files[x] + "/"+ files[x] + ".gd"
  29. var char = load(path)
  30. var dic = char.import()
  31. x += 1
  32. #creates buttons equal to the number of characters
  33. func populate():
  34. var x = 100
  35. var y = 100
  36. var texture = Texture2D.new()
  37. texture = load("res://img/button.jpg")
  38. for i in characters:
  39. var but = Button.new()
  40. var path = load("res://Scripts/Character_Select.gd")
  41. add_child(but)
  42. but.pressed.connect(self.export)
  43. but.set_position(Vector2(x,y))
  44. but.set_button_icon(texture)
  45. x += 250
  46. if x > 1250:
  47. x = 100
  48. y += 250
  49. array.push_back(but)
  50. print(array)
  51. func export():
  52. var temp = self.get_instance_id()
  53. 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

连接信号时:

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

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

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

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

  1. def export(data):
  2. print(data)
  3. # ...

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

  1. for i in blahblahblah:
  2. # ...
  3. # 一些操作
  4. # ...
  5. but.pressed.connect(self.export.bind(i))
  6. # ...
  7. # 其他操作
  8. # ...

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

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

英文:

When you connect to the signal:

  1. 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:

  1. 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:

  1. func export(data):
  2. prints(data)
  3. # ...

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

  1. for i in blahblahblah:
  2. # ...
  3. # stuff
  4. # ...
  5. but.pressed.connect(self.export.bind(i))
  6. # ...
  7. # other stuff
  8. # ...

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:

确定