From 1c814285c62b0296601c65341de7233675f91082 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 25 Oct 2023 11:10:24 +0200 Subject: [PATCH] Implement GetDebugId instance method --- CHANGELOG.md | 6 ++++++ src/roblox/instance/base.rs | 3 +++ src/tests.rs | 1 + tests/roblox/instance/methods/GetDebugId.luau | 11 +++++++++++ types/roblox.luau | 1 + 5 files changed, 22 insertions(+) create mode 100644 tests/roblox/instance/methods/GetDebugId.luau diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b434d9..80005e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added + +- Added the `GetDebugId` instance method to the `roblox` built-in. This will return the internal id used by the instance, and as the name implies, it should be primarily used for _debugging_ purposes and cases where you need a globally unique identifier for an instance. It is guaranteed to be a 32-digit hexadecimal string. + ## `0.7.9` - October 21st, 2023 ### Added diff --git a/src/roblox/instance/base.rs b/src/roblox/instance/base.rs index c377064..594952e 100644 --- a/src/roblox/instance/base.rs +++ b/src/roblox/instance/base.rs @@ -49,6 +49,9 @@ pub fn add_methods<'lua, M: LuaUserDataMethods<'lua, Instance>>(m: &mut M) { ensure_not_destroyed(this)?; this.get_full_name().into_lua(lua) }); + m.add_method("GetDebugId", |lua, this, ()| { + this.dom_ref.to_string().into_lua(lua) + }); m.add_method("FindFirstAncestor", |lua, this, name: String| { ensure_not_destroyed(this)?; this.find_ancestor(|child| child.name == name).into_lua(lua) diff --git a/src/tests.rs b/src/tests.rs index c4162fd..a773fda 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -178,6 +178,7 @@ create_tests! { roblox_instance_methods_find_first_child_of_class: "roblox/instance/methods/FindFirstChildOfClass", roblox_instance_methods_find_first_child_which_is_a: "roblox/instance/methods/FindFirstChildWhichIsA", roblox_instance_methods_get_children: "roblox/instance/methods/GetChildren", + roblox_instance_methods_get_debug_id: "roblox/instance/methods/GetDebugId", roblox_instance_methods_get_descendants: "roblox/instance/methods/GetDescendants", roblox_instance_methods_get_full_name: "roblox/instance/methods/GetFullName", roblox_instance_methods_is_a: "roblox/instance/methods/IsA", diff --git a/tests/roblox/instance/methods/GetDebugId.luau b/tests/roblox/instance/methods/GetDebugId.luau new file mode 100644 index 0000000..a4b1143 --- /dev/null +++ b/tests/roblox/instance/methods/GetDebugId.luau @@ -0,0 +1,11 @@ +local roblox = require("@lune/roblox") + +local part = roblox.Instance.new("Part") + +local id = part:GetDebugId() +assert(type(id) == "string", "GetDebugId should return a string") +assert(#id == 32, "GetDebugId should return a string with length 32") +assert( + string.match(id, "^[0-9A-Fa-f]+$"), + "GetDebugId should return a string with only hexadecimal characters" +) diff --git a/types/roblox.luau b/types/roblox.luau index 6a769db..b79ad60 100644 --- a/types/roblox.luau +++ b/types/roblox.luau @@ -145,6 +145,7 @@ type InstanceMetatable = { ClearAllChildren: (self: Instance) -> (), GetChildren: (self: Instance) -> { Instance }, + GetDebugId: (self: Instance) -> string, GetDescendants: (self: Instance) -> { Instance }, GetFullName: (self: Instance) -> string,