Skip to content
This repository has been archived by the owner on Jul 22, 2023. It is now read-only.

Commit

Permalink
Add GetFullName() method on instances (#49)
Browse files Browse the repository at this point in the history
* implement instance:GetFullName()

* add entry to changelog

* use as_str
  • Loading branch information
jeparlefrancais authored May 11, 2021
1 parent 92390de commit 0ad5aaa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased Changes

* Added Instance:GetFullName() ([#49](https://github.com/rojo-rbx/remodel/pull/49))
* Added `Instance:FindFirstChildOfClass()` ([#50](https://github.com/rojo-rbx/remodel/pull/50))
* Added support for CFrame ([#48](https://github.com/rojo-rbx/remodel/pull/48))
* Added support for Vector3, and improved Vector3int16 ([#46](https://github.com/rojo-rbx/remodel/pull/46))
Expand Down
26 changes: 26 additions & 0 deletions src/roblox_api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,28 @@ impl LuaInstance {
Ok(child)
}

fn get_full_name(&self) -> rlua::Result<String> {
let tree = self.tree.lock().unwrap();

let instance = tree.get_by_ref(self.id).ok_or_else(|| {
rlua::Error::external("Cannot call GetFullName() on a destroyed instance")
})?;

let mut names = vec![instance.name.as_str()];
let mut current = instance.parent();

while let Some(parent_instance) = tree.get_by_ref(current) {
if current != tree.root_ref() && parent_instance.class != "DataModel" {
names.push(parent_instance.name.as_str());
}
current = parent_instance.parent();
}

names.reverse();

Ok(names.join("."))
}

fn get_descendants(&self) -> rlua::Result<Vec<LuaInstance>> {
let tree = self.tree.lock().unwrap();

Expand Down Expand Up @@ -373,6 +395,10 @@ impl UserData for LuaInstance {
this.find_first_child_of_class(&class)
});

methods.add_method("GetFullName", |_context, this, _args: ()| {
this.get_full_name()
});

methods.add_method("GetChildren", |_context, this, _args: ()| {
this.get_children()
});
Expand Down
21 changes: 21 additions & 0 deletions test-scripts/get-full-name.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local function assertString(result, expected)
assert(result == expected, ('expected `%s` but got `%s`'):format(expected, result))
end

local FOLDER_A_NAME = "foo"
local FOLDER_B_NAME = "bar"

local folderA = Instance.new("Folder")
folderA.Name = FOLDER_A_NAME

assertString(folderA:GetFullName(), FOLDER_A_NAME)

local folderB = Instance.new("Folder")
folderB.Name = FOLDER_B_NAME
folderB.Parent = folderA

assertString(folderB:GetFullName(), FOLDER_A_NAME .. '.' .. FOLDER_B_NAME)

local game = remodel.readPlaceFile("test-models/place-with-models.rbxlx")

assertString(game.Workspace.Baseplate:GetFullName(), "Workspace.Baseplate")

0 comments on commit 0ad5aaa

Please sign in to comment.