@@ -62,29 +62,37 @@ function Entity:__eq(right)
6262end
6363
6464--- @param handle number
65- --- @param opts ? { expectedType ?: eEntityType , noassert : boolean }
65+ --- @param opts ? { expectedType ?: eEntityType , noassert ? : boolean }
6666--- @return Entity
6767function Entity .new (handle , opts )
68- opts = opts or {}
69- if not (opts .noassert and Game .IsScriptHandle (handle )) then
70- error (" Attempt to create an Entity instance from an invalid entity script handle" , 2 )
68+ opts = opts or {}
69+ handle = handle or 0
70+ if (not opts .noassert ) then
71+ if (not Game .IsScriptHandle (handle )) then
72+ error (" Attempt to create an Entity instance from an invalid entity script handle" , 2 )
73+ end
7174 end
7275
7376 local expectedType = opts .expectedType
74- if (expectedType and ENTITY .GET_ENTITY_TYPE (handle ) ~= expectedType ) then
75- local type_str = EnumToString (Enums .eEntityType , expectedType )
76- error (_F (" The handle provided does not match the expected entity type (%s)." , type_str ))
77+ if (expectedType ) then
78+ if (ENTITY .GET_ENTITY_TYPE (handle ) ~= expectedType ) then
79+ local type_str = EnumToString (Enums .eEntityType , expectedType )
80+ error (_F (" The handle provided does not match the expected entity type (%s)." , type_str ))
81+ end
7782 end
7883
7984 --- @type Entity
80- --- @diagnostic disable-next-line
81- local instance = setmetatable ({}, Entity )
82- instance .m_handle = handle
85+ --- @diagnostic disable-next-line : param-type-mismatch
86+ local instance = setmetatable ({ m_handle = handle }, Entity )
8387
8488 if (handle ~= 0 ) then
85- instance .m_modelhash = Game .GetEntityModel (handle )
86- instance .m_ptr = memory .handle_to_ptr (handle )
87- instance .m_internal = instance :Resolve ()
89+ ThreadManager :Run (function ()
90+ --- @diagnostic disable : invisible
91+ instance .m_modelhash = Game .GetEntityModel (handle )
92+ instance .m_ptr = memory .handle_to_ptr (handle )
93+ instance .m_internal = instance :Resolve ()
94+ --- @diagnostic enable : invisible
95+ end )
8896 end
8997
9098 return instance
@@ -169,10 +177,10 @@ function Entity:Resolve()
169177 end
170178
171179 -- **DO NOT REMOVE. THIS IS NOT USELESS CODE.**
172- -- We have to do this because `Self` is static, it inherits from `Entity` but doesn't have a constructor
180+ -- We have to do this because `LocalPlayer` inherits from `Entity` but doesn't have a constructor
173181 -- and doesn't store handles or hashes as members (because they can change on player switch).
174182
175- local hndl = self :GetHandle () -- This is overridden in `Self ` to always invoke `PLAYER.PLAYER_PED_ID()`
183+ local hndl = self :GetHandle () -- This is overridden in `LocalPlayer ` to always invoke `PLAYER.PLAYER_PED_ID()`
176184 local ent_type = Game .GetEntityType (hndl )
177185 if (ent_type == Enums .eEntityType .Ped ) then
178186 self .m_internal = CPed (hndl )
@@ -185,6 +193,7 @@ function Entity:Resolve()
185193 return self .m_internal
186194end
187195
196+ --- @return nil
188197function Entity :Destroy ()
189198 self .m_handle = nil
190199 self .m_modelhash = nil
0 commit comments