Skip to content

Commit

Permalink
Fix FindObject not working when specifying UClass outer (#732)
Browse files Browse the repository at this point in the history
* Add UClass outer support for FindObject

* Update FindObject documentation to include new changes and more examples

* Update Changelog.md
  • Loading branch information
GhostyPool authored Dec 31, 2024
1 parent 7b9e340 commit 154f502
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
11 changes: 8 additions & 3 deletions UE4SS/src/Mod/LuaMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2624,7 +2624,7 @@ No overload found for function 'LoadAsset'.
std::string error_overload_not_found{R"(
No overload found for function 'FindObject'.
Overloads:
#1: FindObject(UClass InClass, UObject InOuter, string Name, bool ExactClass)
#1: FindObject(UClass InClass, UObject|UClass InOuter, string Name, bool ExactClass)
#2: FindObject(string|FName|nil ClassName, string|FName|nil ObjectShortName, EObjectFlags RequiredFlags, EObjectFlags BannedFlags)
#3: FindObject(UClass|nil Class, string|FName|nil ObjectShortName, EObjectFlags RequiredFlags, EObjectFlags BannedFlags))"};

Expand Down Expand Up @@ -2691,9 +2691,14 @@ No overload found for function 'FindObject'.
auto& userdata = lua.get_userdata<LuaType::UE4SSBaseObject>(1, true);
std::string_view lua_object_name = userdata.get_object_name();
// TODO: Redo when there's a bette way of checking whether a lua object is derived from UObject
if (lua_object_name == "UObject" || lua_object_name == "UWorld" || lua_object_name == "AActor")
if (lua_object_name == "UObject" || lua_object_name == "UWorld" || lua_object_name == "AActor" || lua_object_name == "UClass")
{
in_outer = lua.get_userdata<LuaType::UObject>().get_remote_cpp_object();
if (lua_object_name == "UClass") {
in_outer = lua.get_userdata<LuaType::UClass>().get_remote_cpp_object();
}
else {
in_outer = lua.get_userdata<LuaType::UObject>().get_remote_cpp_object();
}
could_be_in_outer = true;
}
else if (lua_object_name == "FName")
Expand Down
2 changes: 2 additions & 0 deletions assets/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ Fixed `FText` not working as a parameter (in `RegisterHook`, etc.) ([UE4SS #422]

Fixed crash when calling UFunctions that take one or more 'out' params of type TArray<T>. ([UE4SS #477](https://github.com/UE4SS-RE/RE-UE4SS/pull/477))

Fixed `FindObject` not accepting UClass as a valid outer parameter. ([UE4SS #732](https://github.com/UE4SS-RE/RE-UE4SS/issues/732)) - GhostyPool

Fixed `RegisterProcessConsoleExecPostHook`. ([UE4SS #631](https://github.com/UE4SS-RE/RE-UE4SS/pull/631))

Fixed `FindFirstOf` return type annotation in `Types.lua` to signal that the return value will never be nil. ([UE4SS #652](https://github.com/UE4SS-RE/RE-UE4SS/issues/652))
Expand Down
14 changes: 10 additions & 4 deletions docs/lua-api/global-functions/findobject.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Overload #2 works the same way as `FindObject` in the [UE source](https://docs.u

| # | Type | Information |
|---|------|-------------|
| 1 | string\|FName\|nil | The short name of the class of the object |
| 1 | string\|FName\|UClass\|nil | The short name of the object's class or the UClass itself |
| 2 | string\|FName\|nil | The short name of the object itself |
| 3 | EObjectFlags | Any flags that the object cannot have. Uses \| as a seperator |
| 4 | EObjectFlags | Any flags that the object must have. Uses \| as a seperator |
Expand All @@ -22,7 +22,7 @@ Overload #2 works the same way as `FindObject` in the [UE source](https://docs.u
| # | Type | Information |
|---|------|-------------|
| 1 | UClass | The class to find |
| 2 | UObject | The outer to look inside. If this is null then param 3 should start with a package name |
| 2 | UObject\|UClass | The outer to look inside. If this is null then param 3 should start with a package name |
| 3 | string | The object path to search for an object, relative to param 2 |
| 4 | bool | Whether to require an exact match with the UClass parameter |

Expand All @@ -36,16 +36,22 @@ Overload #2 works the same way as `FindObject` in the [UE source](https://docs.u

```lua
-- SceneComponent instance called TransformComponent0
local Object = FindObject("SceneComponent", "TransformComponent0")
local Object = FindObject("SceneComponent", "TransformComponent0")
```

```lua
-- FirstPersonCharacter_C instance called FirstPersonCharacter_C_0
local Object = FindObject("FirstPersonCharacter_C", "FirstPersonCharacter_C_0", EObjectFlags.RF_NoFlags, EObjectFlags.RF_ClassDefaultObject)
```

Alternatively, you can use a UClass object:

```lua
local Object = FindObject(SceneComponentClass, "TransformComponent0")
```

## Example (overload #2)

```lua
local Object = FindObject(UClass, World, "Character", true)
```
```

0 comments on commit 154f502

Please sign in to comment.