Skip to content

Instantly share code, notes, and snippets.

View me2beats's full-sized avatar

Albert me2beats

  • Russia, Yekaterinburg
View GitHub Profile
@me2beats
me2beats / filesystem.gd
Created March 7, 2021 16:33
Godot FileSystem utils
class_name FileSystemU
const NodeU = preload("node.gd")
static func get_tree(plugin:EditorPlugin):
var dock = plugin.get_editor_interface().get_file_system_dock()
return NodeU.get_child_by_class_path(dock, ['VSplitContainer','Tree'])
static func get_path(item:TreeItem)->String:
var res = ""
@me2beats
me2beats / plugin.gd
Last active November 28, 2021 01:32
workaround for getting godot editor main screen
static func get_main_screen(plugin:EditorPlugin)->int:
var idx = -1
var base:Panel = plugin.get_editor_interface().get_base_control()
var button:ToolButton = find_node_by_class_path(
base, ['VBoxContainer', 'HBoxContainer', 'HBoxContainer', 'ToolButton'], false
)
if not button:
return idx
for b in button.get_parent().get_children():
@me2beats
me2beats / main.gd
Created December 30, 2020 00:09
gdscript banker's rounding (this is how python does rounding - kinda)
func bankers_round_py(num:float)->float:
var rounded = round(num)
if abs(num-rounded) == 0.5:
rounded = 2.0*round(num/2.0)
return rounded
@me2beats
me2beats / main.gd
Created December 29, 2020 19:37
gdscript banker's rounding (python like round)
func bankers_round(num:float)->float:
if fmod(num, 0.5):
return round(num)
var floor_ = floor(num)
if floor_==num:
return num
@me2beats
me2beats / main.gd
Created October 25, 2020 17:14
Node walk with max depth limit
class NodeWalkWithDepth:
var stack = []
var depth_stack = []
var current
var max_depth:int
func _init(node:Node, max_depth_:int):
stack.append(node)
depth_stack.append(0)
@me2beats
me2beats / node_walk.gd
Created October 25, 2020 02:25
test Godot custom iterators
class_name NodeWalk
extends StackIterator
func _init(node).(node):
pass
func _iter_(current):
for i in current.get_children():
stack.push_back(i)
@me2beats
me2beats / main.gd
Created October 7, 2020 07:28
Godot Engine GDScript snake2camel, snake2pascal, camel2snake, pascal2snake
func snake2camel(string:String)->String:
var result = PoolStringArray()
var prev_is_underscore = false
for ch in string:
if ch=='_':
prev_is_underscore = true
else:
if prev_is_underscore:
result.append(ch.to_upper())
@me2beats
me2beats / IntegerField.gd
Created September 1, 2020 13:53
Godot IntegerField (LineEdit filter text)
extends LineEdit
var regex: = RegEx.new()
var oldtext: = ""
func _ready():
regex.compile("^[0-9]*$")
connect("text_changed", self, "_on_LineEdit_text_changed")
func _on_LineEdit_text_changed(new_text:String):
@me2beats
me2beats / extend_context_menu.gd
Created August 31, 2020 21:45
simple example of how to extend godot right click context menu with a plugin
tool
extends EditorPlugin
func find_node_by_class(root : Node, cls:String):
var stack = [root]
while stack:
var node = stack.pop_back()
if node.get_class() == cls:
return node
@me2beats
me2beats / TreeUtils.gd
Last active December 21, 2021 00:37
godot TreeUtils
class_name TreeUtils
static func get_child_by_text(item:TreeItem, text: String, col = 0):
item = item.get_children()
while item:
if item.get_text(col) == text:
return item
item = item.get_next()