Advertisement
metalx1000

Godot - Create Buttons Dynamically

Jan 16th, 2021
3,942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Control
  2. #Copyright Kris Occhipinti 01/16/2021
  3. #https://filmsbykris.com
  4. #Licensed under the GPLv3
  5.  
  6. func _ready():
  7.     var scrollbox = create_scrollbox()
  8.     var vbox = create_vbox(scrollbox)
  9.     for i in range(0,20):
  10.         add_button(vbox,"This is a button #" + str(i+1),"res://icon.png")
  11.        
  12. func create_scrollbox():
  13.     var box = ScrollContainer.new()
  14.     box.set_size(get_viewport_rect().size)
  15.     add_child(box)
  16.     return box
  17.  
  18. func create_vbox(parent):
  19.     var box = VBoxContainer.new()
  20.     box.set_h_size_flags(3)
  21.     parent.add_child(box)
  22.     return box
  23.    
  24. func add_button(parent,text,icon):
  25.     var button = Button.new()
  26.     button.set_h_size_flags(3)
  27.     button.text = text
  28.     button.set_meta("count",0)
  29.     button.connect("pressed",self,"button_press",[button])
  30.     button.icon = load(icon)
  31.     parent.add_child(button)
  32.    
  33.  
  34. func button_press(button):
  35.     var count = button.get_meta("count") + 1
  36.     button.set_meta("count",count)
  37.     button.text = "I've been pressed " + str(count) + " times"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement