Skip to content

Instantly share code, notes, and snippets.

View Yagich's full-sized avatar
💢

Yagich

💢
View GitHub Profile
@Yagich
Yagich / pick_meshinstance3d.gd
Created November 1, 2022 05:57
a godot 4 function to get MeshInstances under the cursor
func pick(in: Node3D) -> Array:
var camera = get_viewport().get_camera_3d()
var center_pos = get_viewport().size / 2.0
var mouse_pos = get_viewport().get_mouse_position()
var from = camera.project_ray_origin(center_pos)
var to = from + camera.project_ray_normal(mouse_pos) * 50.0
var pick_results: Array[MeshInstance3D] = []
for m in in.get_children():
m = m as MeshInstance3D
@Yagich
Yagich / DateHelper.gd
Created November 3, 2021 14:05
godot date/time helper class in GDScript
class_name DateHelper
static func date_add_days(date: Dictionary, days_to_add: int = 1) -> Dictionary:
assert(date.has("month"), "the date Dictionary must have a month")
assert(date.has("day"), "the date Dictionary must have a month")
assert(date.has("year"), "the date Dictionary must have a year")
var to_unix = OS.get_unix_time_from_datetime(date) + (86400 * days_to_add)
return OS.get_datetime_from_unix_time(to_unix)
@Yagich
Yagich / split.lua
Last active September 17, 2021 13:58
lua string splitting by delimiter, with limiting the result
function string:split(delimiter, max_matches)
-- Split a string by delimiter. The resulting table will be of size #max_matches.
max_matches = max_matches or #self
assert(max_matches > 0, "max_matches must be more than 0!")
local result = {}
local pos = 0
for vstart, vend in function() return string.find(self, delimiter, pos, true) end do
table.insert(result, string.sub(self, pos, vstart - 1))
pos = vend + 1
if #result >= max_matches - 1 then