Files
Dawn-Godot/util/UUID.gd
2026-01-14 21:54:38 -06:00

23 lines
731 B
GDScript

static func uuidv4() -> String:
var random = RandomNumberGenerator.new()
random.randomize()
var bytes = PackedByteArray()
for i in range(16):
bytes.append(random.randi_range(0, 255))
# Set the version to 4 -- random
bytes[6] = (bytes[6] & 0x0F) | 0x40
# Set the variant to RFC 4122
bytes[8] = (bytes[8] & 0x3F) | 0x80
var hex_parts = []
for byte in bytes:
hex_parts.append(String("%02x" % byte))
return "%s%s%s%s-%s%s-%s%s-%s%s-%s%s%s%s%s%s" % [
hex_parts[0], hex_parts[1], hex_parts[2], hex_parts[3],
hex_parts[4], hex_parts[5],
hex_parts[6], hex_parts[7],
hex_parts[8], hex_parts[9],
hex_parts[10], hex_parts[11], hex_parts[12], hex_parts[13], hex_parts[14], hex_parts[15]
]