object.cpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  1. /**************************************************************************/
  2. /* object.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "object.h"
  31. #include "core/extension/gdextension_manager.h"
  32. #include "core/io/resource.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/message_queue.h"
  35. #include "core/object/script_language.h"
  36. #include "core/os/os.h"
  37. #include "core/string/print_string.h"
  38. #include "core/string/translation_server.h"
  39. #include "core/variant/typed_array.h"
  40. #ifdef DEBUG_ENABLED
  41. struct _ObjectDebugLock {
  42. ObjectID obj_id;
  43. _ObjectDebugLock(Object *p_obj) {
  44. obj_id = p_obj->get_instance_id();
  45. p_obj->_lock_index.ref();
  46. }
  47. ~_ObjectDebugLock() {
  48. Object *obj_ptr = ObjectDB::get_instance(obj_id);
  49. if (likely(obj_ptr)) {
  50. obj_ptr->_lock_index.unref();
  51. }
  52. }
  53. };
  54. #define OBJ_DEBUG_LOCK _ObjectDebugLock _debug_lock(this);
  55. #else
  56. #define OBJ_DEBUG_LOCK
  57. #endif
  58. PropertyInfo::operator Dictionary() const {
  59. Dictionary d;
  60. d["name"] = name;
  61. d["class_name"] = class_name;
  62. d["type"] = type;
  63. d["hint"] = hint;
  64. d["hint_string"] = hint_string;
  65. d["usage"] = usage;
  66. return d;
  67. }
  68. PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) {
  69. PropertyInfo pi;
  70. if (p_dict.has("type")) {
  71. pi.type = Variant::Type(int(p_dict["type"]));
  72. }
  73. if (p_dict.has("name")) {
  74. pi.name = p_dict["name"];
  75. }
  76. if (p_dict.has("class_name")) {
  77. pi.class_name = p_dict["class_name"];
  78. }
  79. if (p_dict.has("hint")) {
  80. pi.hint = PropertyHint(int(p_dict["hint"]));
  81. }
  82. if (p_dict.has("hint_string")) {
  83. pi.hint_string = p_dict["hint_string"];
  84. }
  85. if (p_dict.has("usage")) {
  86. pi.usage = p_dict["usage"];
  87. }
  88. return pi;
  89. }
  90. TypedArray<Dictionary> convert_property_list(const List<PropertyInfo> *p_list) {
  91. TypedArray<Dictionary> va;
  92. for (const List<PropertyInfo>::Element *E = p_list->front(); E; E = E->next()) {
  93. va.push_back(Dictionary(E->get()));
  94. }
  95. return va;
  96. }
  97. MethodInfo::operator Dictionary() const {
  98. Dictionary d;
  99. d["name"] = name;
  100. d["args"] = convert_property_list(&arguments);
  101. Array da;
  102. for (int i = 0; i < default_arguments.size(); i++) {
  103. da.push_back(default_arguments[i]);
  104. }
  105. d["default_args"] = da;
  106. d["flags"] = flags;
  107. d["id"] = id;
  108. Dictionary r = return_val;
  109. d["return"] = r;
  110. return d;
  111. }
  112. MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) {
  113. MethodInfo mi;
  114. if (p_dict.has("name")) {
  115. mi.name = p_dict["name"];
  116. }
  117. Array args;
  118. if (p_dict.has("args")) {
  119. args = p_dict["args"];
  120. }
  121. for (const Variant &arg : args) {
  122. Dictionary d = arg;
  123. mi.arguments.push_back(PropertyInfo::from_dict(d));
  124. }
  125. Array defargs;
  126. if (p_dict.has("default_args")) {
  127. defargs = p_dict["default_args"];
  128. }
  129. for (const Variant &defarg : defargs) {
  130. mi.default_arguments.push_back(defarg);
  131. }
  132. if (p_dict.has("return")) {
  133. mi.return_val = PropertyInfo::from_dict(p_dict["return"]);
  134. }
  135. if (p_dict.has("flags")) {
  136. mi.flags = p_dict["flags"];
  137. }
  138. return mi;
  139. }
  140. uint32_t MethodInfo::get_compatibility_hash() const {
  141. bool has_return = (return_val.type != Variant::NIL) || (return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
  142. uint32_t hash = hash_murmur3_one_32(has_return);
  143. hash = hash_murmur3_one_32(arguments.size(), hash);
  144. if (has_return) {
  145. hash = hash_murmur3_one_32(return_val.type, hash);
  146. if (return_val.class_name != StringName()) {
  147. hash = hash_murmur3_one_32(return_val.class_name.hash(), hash);
  148. }
  149. }
  150. for (const PropertyInfo &arg : arguments) {
  151. hash = hash_murmur3_one_32(arg.type, hash);
  152. if (arg.class_name != StringName()) {
  153. hash = hash_murmur3_one_32(arg.class_name.hash(), hash);
  154. }
  155. }
  156. hash = hash_murmur3_one_32(default_arguments.size(), hash);
  157. for (const Variant &v : default_arguments) {
  158. hash = hash_murmur3_one_32(v.hash(), hash);
  159. }
  160. hash = hash_murmur3_one_32(flags & METHOD_FLAG_CONST ? 1 : 0, hash);
  161. hash = hash_murmur3_one_32(flags & METHOD_FLAG_VARARG ? 1 : 0, hash);
  162. return hash_fmix32(hash);
  163. }
  164. Object::Connection::operator Variant() const {
  165. Dictionary d;
  166. d["signal"] = signal;
  167. d["callable"] = callable;
  168. d["flags"] = flags;
  169. return d;
  170. }
  171. bool Object::Connection::operator<(const Connection &p_conn) const {
  172. if (signal == p_conn.signal) {
  173. return callable < p_conn.callable;
  174. } else {
  175. return signal < p_conn.signal;
  176. }
  177. }
  178. Object::Connection::Connection(const Variant &p_variant) {
  179. Dictionary d = p_variant;
  180. if (d.has("signal")) {
  181. signal = d["signal"];
  182. }
  183. if (d.has("callable")) {
  184. callable = d["callable"];
  185. }
  186. if (d.has("flags")) {
  187. flags = d["flags"];
  188. }
  189. }
  190. bool Object::_predelete() {
  191. _predelete_ok = 1;
  192. notification(NOTIFICATION_PREDELETE, true);
  193. if (_predelete_ok) {
  194. _class_name_ptr = nullptr; // Must restore, so constructors/destructors have proper class name access at each stage.
  195. notification(NOTIFICATION_PREDELETE_CLEANUP, true);
  196. }
  197. return _predelete_ok;
  198. }
  199. void Object::cancel_free() {
  200. _predelete_ok = false;
  201. }
  202. void Object::_initialize() {
  203. _class_name_ptr = _get_class_namev(); // Set the direct pointer, which is much faster to obtain, but can only happen after _initialize.
  204. _initialize_classv();
  205. _class_name_ptr = nullptr; // May have been called from a constructor.
  206. }
  207. void Object::_postinitialize() {
  208. notification(NOTIFICATION_POSTINITIALIZE);
  209. }
  210. void Object::get_valid_parents_static(List<String> *p_parents) {
  211. }
  212. void Object::_get_valid_parents_static(List<String> *p_parents) {
  213. }
  214. void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  215. #ifdef TOOLS_ENABLED
  216. _edited = true;
  217. #endif
  218. if (script_instance) {
  219. if (script_instance->set(p_name, p_value)) {
  220. if (r_valid) {
  221. *r_valid = true;
  222. }
  223. return;
  224. }
  225. }
  226. if (_extension && _extension->set) {
  227. if (_extension->set(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionConstVariantPtr)&p_value)) {
  228. if (r_valid) {
  229. *r_valid = true;
  230. }
  231. return;
  232. }
  233. }
  234. // Try built-in setter.
  235. {
  236. if (ClassDB::set_property(this, p_name, p_value, r_valid)) {
  237. return;
  238. }
  239. }
  240. if (p_name == CoreStringName(script)) {
  241. set_script(p_value);
  242. if (r_valid) {
  243. *r_valid = true;
  244. }
  245. return;
  246. } else {
  247. Variant **V = metadata_properties.getptr(p_name);
  248. if (V) {
  249. **V = p_value;
  250. if (r_valid) {
  251. *r_valid = true;
  252. }
  253. return;
  254. } else if (p_name.operator String().begins_with("metadata/")) {
  255. // Must exist, otherwise duplicate() will not work.
  256. set_meta(p_name.operator String().replace_first("metadata/", ""), p_value);
  257. if (r_valid) {
  258. *r_valid = true;
  259. }
  260. return;
  261. }
  262. }
  263. #ifdef TOOLS_ENABLED
  264. if (script_instance) {
  265. bool valid;
  266. script_instance->property_set_fallback(p_name, p_value, &valid);
  267. if (valid) {
  268. if (r_valid) {
  269. *r_valid = true;
  270. }
  271. return;
  272. }
  273. }
  274. #endif
  275. // Something inside the object... :|
  276. bool success = _setv(p_name, p_value);
  277. if (success) {
  278. if (r_valid) {
  279. *r_valid = true;
  280. }
  281. return;
  282. }
  283. if (r_valid) {
  284. *r_valid = false;
  285. }
  286. }
  287. Variant Object::get(const StringName &p_name, bool *r_valid) const {
  288. Variant ret;
  289. if (script_instance) {
  290. if (script_instance->get(p_name, ret)) {
  291. if (r_valid) {
  292. *r_valid = true;
  293. }
  294. return ret;
  295. }
  296. }
  297. if (_extension && _extension->get) {
  298. if (_extension->get(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret)) {
  299. if (r_valid) {
  300. *r_valid = true;
  301. }
  302. return ret;
  303. }
  304. }
  305. // Try built-in getter.
  306. {
  307. if (ClassDB::get_property(const_cast<Object *>(this), p_name, ret)) {
  308. if (r_valid) {
  309. *r_valid = true;
  310. }
  311. return ret;
  312. }
  313. }
  314. if (p_name == CoreStringName(script)) {
  315. ret = get_script();
  316. if (r_valid) {
  317. *r_valid = true;
  318. }
  319. return ret;
  320. }
  321. const Variant *const *V = metadata_properties.getptr(p_name);
  322. if (V) {
  323. ret = **V;
  324. if (r_valid) {
  325. *r_valid = true;
  326. }
  327. return ret;
  328. } else {
  329. #ifdef TOOLS_ENABLED
  330. if (script_instance) {
  331. bool valid;
  332. ret = script_instance->property_get_fallback(p_name, &valid);
  333. if (valid) {
  334. if (r_valid) {
  335. *r_valid = true;
  336. }
  337. return ret;
  338. }
  339. }
  340. #endif
  341. // Something inside the object... :|
  342. bool success = _getv(p_name, ret);
  343. if (success) {
  344. if (r_valid) {
  345. *r_valid = true;
  346. }
  347. return ret;
  348. }
  349. if (r_valid) {
  350. *r_valid = false;
  351. }
  352. return Variant();
  353. }
  354. }
  355. void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid) {
  356. if (p_names.is_empty()) {
  357. if (r_valid) {
  358. *r_valid = false;
  359. }
  360. return;
  361. }
  362. if (p_names.size() == 1) {
  363. set(p_names[0], p_value, r_valid);
  364. return;
  365. }
  366. bool valid = false;
  367. if (!r_valid) {
  368. r_valid = &valid;
  369. }
  370. List<Variant> value_stack;
  371. value_stack.push_back(get(p_names[0], r_valid));
  372. if (!*r_valid) {
  373. value_stack.clear();
  374. return;
  375. }
  376. for (int i = 1; i < p_names.size() - 1; i++) {
  377. value_stack.push_back(value_stack.back()->get().get_named(p_names[i], valid));
  378. if (r_valid) {
  379. *r_valid = valid;
  380. }
  381. if (!valid) {
  382. value_stack.clear();
  383. return;
  384. }
  385. }
  386. value_stack.push_back(p_value); // p_names[p_names.size() - 1]
  387. for (int i = p_names.size() - 1; i > 0; i--) {
  388. value_stack.back()->prev()->get().set_named(p_names[i], value_stack.back()->get(), valid);
  389. value_stack.pop_back();
  390. if (r_valid) {
  391. *r_valid = valid;
  392. }
  393. if (!valid) {
  394. value_stack.clear();
  395. return;
  396. }
  397. }
  398. set(p_names[0], value_stack.back()->get(), r_valid);
  399. value_stack.pop_back();
  400. ERR_FAIL_COND(!value_stack.is_empty());
  401. }
  402. Variant Object::get_indexed(const Vector<StringName> &p_names, bool *r_valid) const {
  403. if (p_names.is_empty()) {
  404. if (r_valid) {
  405. *r_valid = false;
  406. }
  407. return Variant();
  408. }
  409. bool valid = false;
  410. Variant current_value = get(p_names[0], &valid);
  411. for (int i = 1; i < p_names.size(); i++) {
  412. current_value = current_value.get_named(p_names[i], valid);
  413. if (!valid) {
  414. break;
  415. }
  416. }
  417. if (r_valid) {
  418. *r_valid = valid;
  419. }
  420. return current_value;
  421. }
  422. void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) const {
  423. if (script_instance && p_reversed) {
  424. script_instance->get_property_list(p_list);
  425. }
  426. if (_extension) {
  427. const ObjectGDExtension *current_extension = _extension;
  428. while (current_extension) {
  429. p_list->push_back(PropertyInfo(Variant::NIL, current_extension->class_name, PROPERTY_HINT_NONE, current_extension->class_name, PROPERTY_USAGE_CATEGORY));
  430. ClassDB::get_property_list(current_extension->class_name, p_list, true, this);
  431. if (current_extension->get_property_list) {
  432. #ifdef TOOLS_ENABLED
  433. // If this is a placeholder, we can't call into the GDExtension on the parent class,
  434. // because we don't have a real instance of the class to give it.
  435. if (likely(!_extension->is_placeholder)) {
  436. #endif
  437. uint32_t pcount;
  438. const GDExtensionPropertyInfo *pinfo = current_extension->get_property_list(_extension_instance, &pcount);
  439. for (uint32_t i = 0; i < pcount; i++) {
  440. p_list->push_back(PropertyInfo(pinfo[i]));
  441. }
  442. if (current_extension->free_property_list2) {
  443. current_extension->free_property_list2(_extension_instance, pinfo, pcount);
  444. }
  445. #ifndef DISABLE_DEPRECATED
  446. else if (current_extension->free_property_list) {
  447. current_extension->free_property_list(_extension_instance, pinfo);
  448. }
  449. #endif // DISABLE_DEPRECATED
  450. #ifdef TOOLS_ENABLED
  451. }
  452. #endif
  453. }
  454. current_extension = current_extension->parent;
  455. }
  456. }
  457. _get_property_listv(p_list, p_reversed);
  458. if (!is_class("Script")) { // can still be set, but this is for user-friendliness
  459. p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NEVER_DUPLICATE));
  460. }
  461. if (script_instance && !p_reversed) {
  462. script_instance->get_property_list(p_list);
  463. }
  464. for (const KeyValue<StringName, Variant> &K : metadata) {
  465. PropertyInfo pi = PropertyInfo(K.value.get_type(), "metadata/" + K.key.operator String());
  466. if (K.value.get_type() == Variant::OBJECT) {
  467. pi.hint = PROPERTY_HINT_RESOURCE_TYPE;
  468. Object *obj = K.value;
  469. if (Object::cast_to<Script>(obj)) {
  470. pi.hint_string = "Script";
  471. pi.usage |= PROPERTY_USAGE_NEVER_DUPLICATE;
  472. } else {
  473. pi.hint_string = "Resource";
  474. }
  475. }
  476. p_list->push_back(pi);
  477. }
  478. }
  479. void Object::validate_property(PropertyInfo &p_property) const {
  480. _validate_propertyv(p_property);
  481. if (_extension && _extension->validate_property) {
  482. // GDExtension uses a StringName rather than a String for property name.
  483. StringName prop_name = p_property.name;
  484. GDExtensionPropertyInfo gdext_prop = {
  485. (GDExtensionVariantType)p_property.type,
  486. &prop_name,
  487. &p_property.class_name,
  488. (uint32_t)p_property.hint,
  489. &p_property.hint_string,
  490. p_property.usage,
  491. };
  492. if (_extension->validate_property(_extension_instance, &gdext_prop)) {
  493. p_property.type = (Variant::Type)gdext_prop.type;
  494. p_property.name = *reinterpret_cast<StringName *>(gdext_prop.name);
  495. p_property.class_name = *reinterpret_cast<StringName *>(gdext_prop.class_name);
  496. p_property.hint = (PropertyHint)gdext_prop.hint;
  497. p_property.hint_string = *reinterpret_cast<String *>(gdext_prop.hint_string);
  498. p_property.usage = gdext_prop.usage;
  499. };
  500. }
  501. if (script_instance) { // Call it last to allow user altering already validated properties.
  502. script_instance->validate_property(p_property);
  503. }
  504. }
  505. bool Object::property_can_revert(const StringName &p_name) const {
  506. if (script_instance) {
  507. if (script_instance->property_can_revert(p_name)) {
  508. return true;
  509. }
  510. }
  511. if (_extension && _extension->property_can_revert) {
  512. if (_extension->property_can_revert(_extension_instance, (GDExtensionConstStringNamePtr)&p_name)) {
  513. return true;
  514. }
  515. }
  516. return _property_can_revertv(p_name);
  517. }
  518. Variant Object::property_get_revert(const StringName &p_name) const {
  519. Variant ret;
  520. if (script_instance) {
  521. if (script_instance->property_get_revert(p_name, ret)) {
  522. return ret;
  523. }
  524. }
  525. if (_extension && _extension->property_get_revert) {
  526. if (_extension->property_get_revert(_extension_instance, (GDExtensionConstStringNamePtr)&p_name, (GDExtensionVariantPtr)&ret)) {
  527. return ret;
  528. }
  529. }
  530. if (_property_get_revertv(p_name, ret)) {
  531. return ret;
  532. }
  533. return Variant();
  534. }
  535. void Object::get_method_list(List<MethodInfo> *p_list) const {
  536. ClassDB::get_method_list(get_class_name(), p_list);
  537. if (script_instance) {
  538. script_instance->get_method_list(p_list);
  539. }
  540. }
  541. Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  542. if (p_argcount < 1) {
  543. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  544. r_error.expected = 1;
  545. return Variant();
  546. }
  547. if (!p_args[0]->is_string()) {
  548. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  549. r_error.argument = 0;
  550. r_error.expected = Variant::STRING_NAME;
  551. return Variant();
  552. }
  553. StringName method = *p_args[0];
  554. return callp(method, &p_args[1], p_argcount - 1, r_error);
  555. }
  556. Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  557. if (p_argcount < 1) {
  558. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  559. r_error.expected = 1;
  560. return Variant();
  561. }
  562. if (!p_args[0]->is_string()) {
  563. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  564. r_error.argument = 0;
  565. r_error.expected = Variant::STRING_NAME;
  566. return Variant();
  567. }
  568. r_error.error = Callable::CallError::CALL_OK;
  569. StringName method = *p_args[0];
  570. MessageQueue::get_singleton()->push_callp(get_instance_id(), method, &p_args[1], p_argcount - 1, true);
  571. return Variant();
  572. }
  573. bool Object::has_method(const StringName &p_method) const {
  574. if (p_method == CoreStringName(free_)) {
  575. return true;
  576. }
  577. if (script_instance && script_instance->has_method(p_method)) {
  578. return true;
  579. }
  580. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  581. if (method != nullptr) {
  582. return true;
  583. }
  584. const Script *scr = Object::cast_to<Script>(this);
  585. if (scr != nullptr) {
  586. return scr->has_static_method(p_method);
  587. }
  588. return false;
  589. }
  590. int Object::_get_method_argument_count_bind(const StringName &p_method) const {
  591. return get_method_argument_count(p_method);
  592. }
  593. int Object::get_method_argument_count(const StringName &p_method, bool *r_is_valid) const {
  594. if (p_method == CoreStringName(free_)) {
  595. if (r_is_valid) {
  596. *r_is_valid = true;
  597. }
  598. return 0;
  599. }
  600. if (script_instance) {
  601. bool valid = false;
  602. int ret = script_instance->get_method_argument_count(p_method, &valid);
  603. if (valid) {
  604. if (r_is_valid) {
  605. *r_is_valid = true;
  606. }
  607. return ret;
  608. }
  609. }
  610. {
  611. bool valid = false;
  612. int ret = ClassDB::get_method_argument_count(get_class_name(), p_method, &valid);
  613. if (valid) {
  614. if (r_is_valid) {
  615. *r_is_valid = true;
  616. }
  617. return ret;
  618. }
  619. }
  620. const Script *scr = Object::cast_to<Script>(this);
  621. while (scr != nullptr) {
  622. bool valid = false;
  623. int ret = scr->get_script_method_argument_count(p_method, &valid);
  624. if (valid) {
  625. if (r_is_valid) {
  626. *r_is_valid = true;
  627. }
  628. return ret;
  629. }
  630. scr = scr->get_base_script().ptr();
  631. }
  632. if (r_is_valid) {
  633. *r_is_valid = false;
  634. }
  635. return 0;
  636. }
  637. Variant Object::getvar(const Variant &p_key, bool *r_valid) const {
  638. if (r_valid) {
  639. *r_valid = false;
  640. }
  641. if (p_key.is_string()) {
  642. return get(p_key, r_valid);
  643. }
  644. return Variant();
  645. }
  646. void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) {
  647. if (r_valid) {
  648. *r_valid = false;
  649. }
  650. if (p_key.is_string()) {
  651. return set(p_key, p_value, r_valid);
  652. }
  653. }
  654. Variant Object::callv(const StringName &p_method, const Array &p_args) {
  655. const Variant **argptrs = nullptr;
  656. if (p_args.size() > 0) {
  657. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_args.size());
  658. for (int i = 0; i < p_args.size(); i++) {
  659. argptrs[i] = &p_args[i];
  660. }
  661. }
  662. Callable::CallError ce;
  663. const Variant ret = callp(p_method, argptrs, p_args.size(), ce);
  664. if (ce.error != Callable::CallError::CALL_OK) {
  665. ERR_FAIL_V_MSG(Variant(), vformat("Error calling method from 'callv': %s.", Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce)));
  666. }
  667. return ret;
  668. }
  669. Variant Object::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  670. r_error.error = Callable::CallError::CALL_OK;
  671. if (p_method == CoreStringName(free_)) {
  672. //free must be here, before anything, always ready
  673. #ifdef DEBUG_ENABLED
  674. if (p_argcount != 0) {
  675. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  676. r_error.expected = 0;
  677. return Variant();
  678. }
  679. if (is_ref_counted()) {
  680. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  681. ERR_FAIL_V_MSG(Variant(), "Can't free a RefCounted object.");
  682. }
  683. if (_lock_index.get() > 1) {
  684. r_error.argument = 0;
  685. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  686. ERR_FAIL_V_MSG(Variant(), "Object is locked and can't be freed.");
  687. }
  688. #endif
  689. //must be here, must be before everything,
  690. memdelete(this);
  691. r_error.error = Callable::CallError::CALL_OK;
  692. return Variant();
  693. }
  694. Variant ret;
  695. OBJ_DEBUG_LOCK
  696. if (script_instance) {
  697. ret = script_instance->callp(p_method, p_args, p_argcount, r_error);
  698. // Force jump table.
  699. switch (r_error.error) {
  700. case Callable::CallError::CALL_OK:
  701. return ret;
  702. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  703. break;
  704. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  705. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  706. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  707. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  708. return ret;
  709. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  710. }
  711. }
  712. }
  713. //extension does not need this, because all methods are registered in MethodBind
  714. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  715. if (method) {
  716. ret = method->call(this, p_args, p_argcount, r_error);
  717. } else {
  718. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  719. }
  720. return ret;
  721. }
  722. Variant Object::call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  723. r_error.error = Callable::CallError::CALL_OK;
  724. if (p_method == CoreStringName(free_)) {
  725. // Free is not const, so fail.
  726. r_error.error = Callable::CallError::CALL_ERROR_METHOD_NOT_CONST;
  727. return Variant();
  728. }
  729. Variant ret;
  730. OBJ_DEBUG_LOCK
  731. if (script_instance) {
  732. ret = script_instance->call_const(p_method, p_args, p_argcount, r_error);
  733. //force jumptable
  734. switch (r_error.error) {
  735. case Callable::CallError::CALL_OK:
  736. return ret;
  737. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  738. break;
  739. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  740. break;
  741. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  742. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  743. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  744. return ret;
  745. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: {
  746. }
  747. }
  748. }
  749. //extension does not need this, because all methods are registered in MethodBind
  750. MethodBind *method = ClassDB::get_method(get_class_name(), p_method);
  751. if (method) {
  752. if (!method->is_const()) {
  753. r_error.error = Callable::CallError::CALL_ERROR_METHOD_NOT_CONST;
  754. return ret;
  755. }
  756. ret = method->call(this, p_args, p_argcount, r_error);
  757. } else {
  758. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  759. }
  760. return ret;
  761. }
  762. void Object::notification(int p_notification, bool p_reversed) {
  763. if (p_reversed) {
  764. if (script_instance) {
  765. script_instance->notification(p_notification, p_reversed);
  766. }
  767. } else {
  768. _notificationv(p_notification, p_reversed);
  769. }
  770. if (_extension) {
  771. if (_extension->notification2) {
  772. _extension->notification2(_extension_instance, p_notification, static_cast<GDExtensionBool>(p_reversed));
  773. #ifndef DISABLE_DEPRECATED
  774. } else if (_extension->notification) {
  775. _extension->notification(_extension_instance, p_notification);
  776. #endif // DISABLE_DEPRECATED
  777. }
  778. }
  779. if (p_reversed) {
  780. _notificationv(p_notification, p_reversed);
  781. } else {
  782. if (script_instance) {
  783. script_instance->notification(p_notification, p_reversed);
  784. }
  785. }
  786. }
  787. String Object::to_string() {
  788. // Keep this method in sync with `Node::to_string`.
  789. if (script_instance) {
  790. bool valid;
  791. String ret = script_instance->to_string(&valid);
  792. if (valid) {
  793. return ret;
  794. }
  795. }
  796. if (_extension && _extension->to_string) {
  797. String ret;
  798. GDExtensionBool is_valid;
  799. _extension->to_string(_extension_instance, &is_valid, &ret);
  800. return ret;
  801. }
  802. return "<" + get_class() + "#" + itos(get_instance_id()) + ">";
  803. }
  804. void Object::set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance) {
  805. //this function is not meant to be used in any of these ways
  806. ERR_FAIL_COND(p_script.is_null());
  807. ERR_FAIL_NULL(p_instance);
  808. ERR_FAIL_COND(script_instance != nullptr || !script.is_null());
  809. script = p_script;
  810. script_instance = p_instance;
  811. }
  812. void Object::set_script(const Variant &p_script) {
  813. if (script == p_script) {
  814. return;
  815. }
  816. Ref<Script> s = p_script;
  817. if (!p_script.is_null()) {
  818. ERR_FAIL_COND_MSG(s.is_null(), "Cannot set object script. Parameter should be null or a reference to a valid script.");
  819. ERR_FAIL_COND_MSG(s->is_abstract(), vformat("Cannot set object script. Script '%s' should not be abstract.", s->get_path()));
  820. }
  821. script = p_script;
  822. if (script_instance) {
  823. memdelete(script_instance);
  824. script_instance = nullptr;
  825. }
  826. if (s.is_valid()) {
  827. if (s->can_instantiate()) {
  828. OBJ_DEBUG_LOCK
  829. script_instance = s->instance_create(this);
  830. } else if (Engine::get_singleton()->is_editor_hint()) {
  831. OBJ_DEBUG_LOCK
  832. script_instance = s->placeholder_instance_create(this);
  833. }
  834. }
  835. notify_property_list_changed(); //scripts may add variables, so refresh is desired
  836. emit_signal(CoreStringName(script_changed));
  837. }
  838. void Object::set_script_instance(ScriptInstance *p_instance) {
  839. if (script_instance == p_instance) {
  840. return;
  841. }
  842. if (script_instance) {
  843. memdelete(script_instance);
  844. }
  845. script_instance = p_instance;
  846. if (p_instance) {
  847. script = p_instance->get_script();
  848. } else {
  849. script = Variant();
  850. }
  851. }
  852. Variant Object::get_script() const {
  853. return script;
  854. }
  855. bool Object::has_meta(const StringName &p_name) const {
  856. return metadata.has(p_name);
  857. }
  858. void Object::set_meta(const StringName &p_name, const Variant &p_value) {
  859. if (p_value.get_type() == Variant::NIL) {
  860. if (metadata.has(p_name)) {
  861. metadata.erase(p_name);
  862. const String &sname = p_name;
  863. metadata_properties.erase("metadata/" + sname);
  864. if (!sname.begins_with("_")) {
  865. // Metadata starting with _ don't show up in the inspector, so no need to update.
  866. notify_property_list_changed();
  867. }
  868. }
  869. return;
  870. }
  871. HashMap<StringName, Variant>::Iterator E = metadata.find(p_name);
  872. if (E) {
  873. E->value = p_value;
  874. } else {
  875. ERR_FAIL_COND_MSG(!p_name.operator String().is_valid_ascii_identifier(), vformat("Invalid metadata identifier: '%s'.", p_name));
  876. Variant *V = &metadata.insert(p_name, p_value)->value;
  877. const String &sname = p_name;
  878. metadata_properties["metadata/" + sname] = V;
  879. if (!sname.begins_with("_")) {
  880. notify_property_list_changed();
  881. }
  882. }
  883. }
  884. Variant Object::get_meta(const StringName &p_name, const Variant &p_default) const {
  885. if (!metadata.has(p_name)) {
  886. if (p_default != Variant()) {
  887. return p_default;
  888. } else {
  889. ERR_FAIL_V_MSG(Variant(), vformat("The object does not have any 'meta' values with the key '%s'.", p_name));
  890. }
  891. }
  892. return metadata[p_name];
  893. }
  894. void Object::remove_meta(const StringName &p_name) {
  895. set_meta(p_name, Variant());
  896. }
  897. void Object::merge_meta_from(const Object *p_src) {
  898. List<StringName> meta_keys;
  899. p_src->get_meta_list(&meta_keys);
  900. for (const StringName &key : meta_keys) {
  901. set_meta(key, p_src->get_meta(key));
  902. }
  903. }
  904. TypedArray<Dictionary> Object::_get_property_list_bind() const {
  905. List<PropertyInfo> lpi;
  906. get_property_list(&lpi);
  907. return convert_property_list(&lpi);
  908. }
  909. TypedArray<Dictionary> Object::_get_method_list_bind() const {
  910. List<MethodInfo> ml;
  911. get_method_list(&ml);
  912. TypedArray<Dictionary> ret;
  913. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  914. Dictionary d = E->get();
  915. //va.push_back(d);
  916. ret.push_back(d);
  917. }
  918. return ret;
  919. }
  920. TypedArray<StringName> Object::_get_meta_list_bind() const {
  921. TypedArray<StringName> _metaret;
  922. for (const KeyValue<StringName, Variant> &K : metadata) {
  923. _metaret.push_back(K.key);
  924. }
  925. return _metaret;
  926. }
  927. void Object::get_meta_list(List<StringName> *p_list) const {
  928. for (const KeyValue<StringName, Variant> &K : metadata) {
  929. p_list->push_back(K.key);
  930. }
  931. }
  932. void Object::add_user_signal(const MethodInfo &p_signal) {
  933. ERR_FAIL_COND_MSG(p_signal.name.is_empty(), "Signal name cannot be empty.");
  934. ERR_FAIL_COND_MSG(ClassDB::has_signal(get_class_name(), p_signal.name), vformat("User signal's name conflicts with a built-in signal of '%s'.", get_class_name()));
  935. ERR_FAIL_COND_MSG(signal_map.has(p_signal.name), vformat("Trying to add already existing signal '%s'.", p_signal.name));
  936. SignalData s;
  937. s.user = p_signal;
  938. signal_map[p_signal.name] = s;
  939. }
  940. bool Object::_has_user_signal(const StringName &p_name) const {
  941. if (!signal_map.has(p_name)) {
  942. return false;
  943. }
  944. return signal_map[p_name].user.name.length() > 0;
  945. }
  946. void Object::_remove_user_signal(const StringName &p_name) {
  947. SignalData *s = signal_map.getptr(p_name);
  948. ERR_FAIL_NULL_MSG(s, "Provided signal does not exist.");
  949. ERR_FAIL_COND_MSG(!s->removable, "Signal is not removable (not added with add_user_signal).");
  950. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  951. Object *target = slot_kv.key.get_object();
  952. if (likely(target)) {
  953. target->connections.erase(slot_kv.value.cE);
  954. }
  955. }
  956. signal_map.erase(p_name);
  957. }
  958. Error Object::_emit_signal(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  959. if (unlikely(p_argcount < 1)) {
  960. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  961. r_error.expected = 1;
  962. ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
  963. }
  964. if (unlikely(!p_args[0]->is_string())) {
  965. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  966. r_error.argument = 0;
  967. r_error.expected = Variant::STRING_NAME;
  968. ERR_FAIL_V(Error::ERR_INVALID_PARAMETER);
  969. }
  970. r_error.error = Callable::CallError::CALL_OK;
  971. StringName signal = *p_args[0];
  972. const Variant **args = nullptr;
  973. int argc = p_argcount - 1;
  974. if (argc) {
  975. args = &p_args[1];
  976. }
  977. return emit_signalp(signal, args, argc);
  978. }
  979. Error Object::emit_signalp(const StringName &p_name, const Variant **p_args, int p_argcount) {
  980. if (_block_signals) {
  981. return ERR_CANT_ACQUIRE_RESOURCE; //no emit, signals blocked
  982. }
  983. SignalData *s = signal_map.getptr(p_name);
  984. if (!s) {
  985. #ifdef DEBUG_ENABLED
  986. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_name);
  987. //check in script
  988. ERR_FAIL_COND_V_MSG(!signal_is_valid && !script.is_null() && !Ref<Script>(script)->has_script_signal(p_name), ERR_UNAVAILABLE, vformat("Can't emit non-existing signal \"%s\".", p_name));
  989. #endif
  990. //not connected? just return
  991. return ERR_UNAVAILABLE;
  992. }
  993. // If this is a ref-counted object, prevent it from being destroyed during signal emission,
  994. // which is needed in certain edge cases; e.g., https://github.com/godotengine/godot/issues/73889.
  995. Ref<RefCounted> rc = Ref<RefCounted>(Object::cast_to<RefCounted>(this));
  996. // Ensure that disconnecting the signal or even deleting the object
  997. // will not affect the signal calling.
  998. Callable *slot_callables = (Callable *)alloca(sizeof(Callable) * s->slot_map.size());
  999. uint32_t *slot_flags = (uint32_t *)alloca(sizeof(uint32_t) * s->slot_map.size());
  1000. uint32_t slot_count = 0;
  1001. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1002. memnew_placement(&slot_callables[slot_count], Callable(slot_kv.value.conn.callable));
  1003. slot_flags[slot_count] = slot_kv.value.conn.flags;
  1004. ++slot_count;
  1005. }
  1006. DEV_ASSERT(slot_count == s->slot_map.size());
  1007. // Disconnect all one-shot connections before emitting to prevent recursion.
  1008. for (uint32_t i = 0; i < slot_count; ++i) {
  1009. bool disconnect = slot_flags[i] & CONNECT_ONE_SHOT;
  1010. #ifdef TOOLS_ENABLED
  1011. if (disconnect && (slot_flags[i] & CONNECT_PERSIST) && Engine::get_singleton()->is_editor_hint()) {
  1012. // This signal was connected from the editor, and is being edited. Just don't disconnect for now.
  1013. disconnect = false;
  1014. }
  1015. #endif
  1016. if (disconnect) {
  1017. _disconnect(p_name, slot_callables[i]);
  1018. }
  1019. }
  1020. OBJ_DEBUG_LOCK
  1021. Error err = OK;
  1022. for (uint32_t i = 0; i < slot_count; ++i) {
  1023. const Callable &callable = slot_callables[i];
  1024. const uint32_t &flags = slot_flags[i];
  1025. if (!callable.is_valid()) {
  1026. // Target might have been deleted during signal callback, this is expected and OK.
  1027. continue;
  1028. }
  1029. const Variant **args = p_args;
  1030. int argc = p_argcount;
  1031. if (flags & CONNECT_DEFERRED) {
  1032. MessageQueue::get_singleton()->push_callablep(callable, args, argc, true);
  1033. } else {
  1034. Callable::CallError ce;
  1035. _emitting = true;
  1036. Variant ret;
  1037. callable.callp(args, argc, ret, ce);
  1038. _emitting = false;
  1039. if (ce.error != Callable::CallError::CALL_OK) {
  1040. #ifdef DEBUG_ENABLED
  1041. if (flags & CONNECT_PERSIST && Engine::get_singleton()->is_editor_hint() && (script.is_null() || !Ref<Script>(script)->is_tool())) {
  1042. continue;
  1043. }
  1044. #endif
  1045. Object *target = callable.get_object();
  1046. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD && target && !ClassDB::class_exists(target->get_class_name())) {
  1047. //most likely object is not initialized yet, do not throw error.
  1048. } else {
  1049. ERR_PRINT(vformat("Error calling from signal '%s' to callable: %s.", String(p_name), Variant::get_callable_error_text(callable, args, argc, ce)));
  1050. err = ERR_METHOD_NOT_FOUND;
  1051. }
  1052. }
  1053. }
  1054. }
  1055. for (uint32_t i = 0; i < slot_count; ++i) {
  1056. slot_callables[i].~Callable();
  1057. }
  1058. return err;
  1059. }
  1060. void Object::_add_user_signal(const String &p_name, const Array &p_args) {
  1061. // this version of add_user_signal is meant to be used from scripts or external apis
  1062. // without access to ADD_SIGNAL in bind_methods
  1063. // added events are per instance, as opposed to the other ones, which are global
  1064. MethodInfo mi;
  1065. mi.name = p_name;
  1066. for (const Variant &arg : p_args) {
  1067. Dictionary d = arg;
  1068. PropertyInfo param;
  1069. if (d.has("name")) {
  1070. param.name = d["name"];
  1071. }
  1072. if (d.has("type")) {
  1073. param.type = (Variant::Type)(int)d["type"];
  1074. }
  1075. mi.arguments.push_back(param);
  1076. }
  1077. add_user_signal(mi);
  1078. if (signal_map.has(p_name)) {
  1079. signal_map.getptr(p_name)->removable = true;
  1080. }
  1081. }
  1082. TypedArray<Dictionary> Object::_get_signal_list() const {
  1083. List<MethodInfo> signal_list;
  1084. get_signal_list(&signal_list);
  1085. TypedArray<Dictionary> ret;
  1086. for (const MethodInfo &E : signal_list) {
  1087. ret.push_back(Dictionary(E));
  1088. }
  1089. return ret;
  1090. }
  1091. TypedArray<Dictionary> Object::_get_signal_connection_list(const StringName &p_signal) const {
  1092. List<Connection> conns;
  1093. get_all_signal_connections(&conns);
  1094. TypedArray<Dictionary> ret;
  1095. for (const Connection &c : conns) {
  1096. if (c.signal.get_name() == p_signal) {
  1097. ret.push_back(c);
  1098. }
  1099. }
  1100. return ret;
  1101. }
  1102. TypedArray<Dictionary> Object::_get_incoming_connections() const {
  1103. TypedArray<Dictionary> ret;
  1104. for (const Object::Connection &connection : connections) {
  1105. ret.push_back(connection);
  1106. }
  1107. return ret;
  1108. }
  1109. bool Object::has_signal(const StringName &p_name) const {
  1110. if (!script.is_null()) {
  1111. Ref<Script> scr = script;
  1112. if (scr.is_valid() && scr->has_script_signal(p_name)) {
  1113. return true;
  1114. }
  1115. }
  1116. if (ClassDB::has_signal(get_class_name(), p_name)) {
  1117. return true;
  1118. }
  1119. if (_has_user_signal(p_name)) {
  1120. return true;
  1121. }
  1122. return false;
  1123. }
  1124. void Object::get_signal_list(List<MethodInfo> *p_signals) const {
  1125. if (!script.is_null()) {
  1126. Ref<Script> scr = script;
  1127. if (scr.is_valid()) {
  1128. scr->get_script_signal_list(p_signals);
  1129. }
  1130. }
  1131. ClassDB::get_signal_list(get_class_name(), p_signals);
  1132. //find maybe usersignals?
  1133. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1134. if (!E.value.user.name.is_empty()) {
  1135. //user signal
  1136. p_signals->push_back(E.value.user);
  1137. }
  1138. }
  1139. }
  1140. void Object::get_all_signal_connections(List<Connection> *p_connections) const {
  1141. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1142. const SignalData *s = &E.value;
  1143. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1144. p_connections->push_back(slot_kv.value.conn);
  1145. }
  1146. }
  1147. }
  1148. void Object::get_signal_connection_list(const StringName &p_signal, List<Connection> *p_connections) const {
  1149. const SignalData *s = signal_map.getptr(p_signal);
  1150. if (!s) {
  1151. return; //nothing
  1152. }
  1153. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1154. p_connections->push_back(slot_kv.value.conn);
  1155. }
  1156. }
  1157. int Object::get_persistent_signal_connection_count() const {
  1158. int count = 0;
  1159. for (const KeyValue<StringName, SignalData> &E : signal_map) {
  1160. const SignalData *s = &E.value;
  1161. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1162. if (slot_kv.value.conn.flags & CONNECT_PERSIST) {
  1163. count += 1;
  1164. }
  1165. }
  1166. }
  1167. return count;
  1168. }
  1169. void Object::get_signals_connected_to_this(List<Connection> *p_connections) const {
  1170. for (const Connection &E : connections) {
  1171. p_connections->push_back(E);
  1172. }
  1173. }
  1174. Error Object::connect(const StringName &p_signal, const Callable &p_callable, uint32_t p_flags) {
  1175. ERR_FAIL_COND_V_MSG(p_callable.is_null(), ERR_INVALID_PARAMETER, vformat("Cannot connect to '%s': the provided callable is null.", p_signal));
  1176. if (p_callable.is_standard()) {
  1177. // FIXME: This branch should probably removed in favor of the `is_valid()` branch, but there exist some classes
  1178. // that call `connect()` before they are fully registered with ClassDB. Until all such classes can be found
  1179. // and registered soon enough this branch is needed to allow `connect()` to succeed.
  1180. ERR_FAIL_NULL_V_MSG(p_callable.get_object(), ERR_INVALID_PARAMETER, vformat("Cannot connect to '%s' to callable '%s': the callable object is null.", p_signal, p_callable));
  1181. } else {
  1182. ERR_FAIL_COND_V_MSG(!p_callable.is_valid(), ERR_INVALID_PARAMETER, vformat("Cannot connect to '%s': the provided callable is not valid: '%s'.", p_signal, p_callable));
  1183. }
  1184. SignalData *s = signal_map.getptr(p_signal);
  1185. if (!s) {
  1186. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1187. //check in script
  1188. if (!signal_is_valid && !script.is_null()) {
  1189. if (Ref<Script>(script)->has_script_signal(p_signal)) {
  1190. signal_is_valid = true;
  1191. }
  1192. #ifdef TOOLS_ENABLED
  1193. else {
  1194. //allow connecting signals anyway if script is invalid, see issue #17070
  1195. if (!Ref<Script>(script)->is_valid()) {
  1196. signal_is_valid = true;
  1197. }
  1198. }
  1199. #endif
  1200. }
  1201. ERR_FAIL_COND_V_MSG(!signal_is_valid, ERR_INVALID_PARAMETER, vformat("In Object of type '%s': Attempt to connect nonexistent signal '%s' to callable '%s'.", String(get_class()), p_signal, p_callable));
  1202. signal_map[p_signal] = SignalData();
  1203. s = &signal_map[p_signal];
  1204. }
  1205. //compare with the base callable, so binds can be ignored
  1206. if (s->slot_map.has(*p_callable.get_base_comparator())) {
  1207. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1208. s->slot_map[*p_callable.get_base_comparator()].reference_count++;
  1209. return OK;
  1210. } else {
  1211. ERR_FAIL_V_MSG(ERR_INVALID_PARAMETER, vformat("Signal '%s' is already connected to given callable '%s' in that object.", p_signal, p_callable));
  1212. }
  1213. }
  1214. Object *target_object = p_callable.get_object();
  1215. SignalData::Slot slot;
  1216. Connection conn;
  1217. conn.callable = p_callable;
  1218. conn.signal = ::Signal(this, p_signal);
  1219. conn.flags = p_flags;
  1220. slot.conn = conn;
  1221. if (target_object) {
  1222. slot.cE = target_object->connections.push_back(conn);
  1223. }
  1224. if (p_flags & CONNECT_REFERENCE_COUNTED) {
  1225. slot.reference_count = 1;
  1226. }
  1227. //use callable version as key, so binds can be ignored
  1228. s->slot_map[*p_callable.get_base_comparator()] = slot;
  1229. return OK;
  1230. }
  1231. bool Object::is_connected(const StringName &p_signal, const Callable &p_callable) const {
  1232. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, vformat("Cannot determine if connected to '%s': the provided callable is null.", p_signal)); // Should use `is_null`, see note in `connect` about the use of `is_valid`.
  1233. const SignalData *s = signal_map.getptr(p_signal);
  1234. if (!s) {
  1235. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1236. if (signal_is_valid) {
  1237. return false;
  1238. }
  1239. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1240. return false;
  1241. }
  1242. ERR_FAIL_V_MSG(false, vformat("Nonexistent signal: '%s'.", p_signal));
  1243. }
  1244. return s->slot_map.has(*p_callable.get_base_comparator());
  1245. }
  1246. bool Object::has_connections(const StringName &p_signal) const {
  1247. const SignalData *s = signal_map.getptr(p_signal);
  1248. if (!s) {
  1249. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal);
  1250. if (signal_is_valid) {
  1251. return false;
  1252. }
  1253. if (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal)) {
  1254. return false;
  1255. }
  1256. ERR_FAIL_V_MSG(false, vformat("Nonexistent signal: '%s'.", p_signal));
  1257. }
  1258. return !s->slot_map.is_empty();
  1259. }
  1260. void Object::disconnect(const StringName &p_signal, const Callable &p_callable) {
  1261. _disconnect(p_signal, p_callable);
  1262. }
  1263. bool Object::_disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force) {
  1264. ERR_FAIL_COND_V_MSG(p_callable.is_null(), false, vformat("Cannot disconnect from '%s': the provided callable is null.", p_signal)); // Should use `is_null`, see note in `connect` about the use of `is_valid`.
  1265. SignalData *s = signal_map.getptr(p_signal);
  1266. if (!s) {
  1267. bool signal_is_valid = ClassDB::has_signal(get_class_name(), p_signal) ||
  1268. (!script.is_null() && Ref<Script>(script)->has_script_signal(p_signal));
  1269. ERR_FAIL_COND_V_MSG(signal_is_valid, false, vformat("Attempt to disconnect a nonexistent connection from '%s'. Signal: '%s', callable: '%s'.", to_string(), p_signal, p_callable));
  1270. }
  1271. ERR_FAIL_NULL_V_MSG(s, false, vformat("Disconnecting nonexistent signal '%s' in '%s'.", p_signal, to_string()));
  1272. ERR_FAIL_COND_V_MSG(!s->slot_map.has(*p_callable.get_base_comparator()), false, vformat("Attempt to disconnect a nonexistent connection from '%s'. Signal: '%s', callable: '%s'.", to_string(), p_signal, p_callable));
  1273. SignalData::Slot *slot = &s->slot_map[*p_callable.get_base_comparator()];
  1274. if (!p_force) {
  1275. slot->reference_count--; // by default is zero, if it was not referenced it will go below it
  1276. if (slot->reference_count > 0) {
  1277. return false;
  1278. }
  1279. }
  1280. if (slot->cE) {
  1281. Object *target_object = p_callable.get_object();
  1282. if (target_object) {
  1283. target_object->connections.erase(slot->cE);
  1284. }
  1285. }
  1286. s->slot_map.erase(*p_callable.get_base_comparator());
  1287. if (s->slot_map.is_empty() && ClassDB::has_signal(get_class_name(), p_signal)) {
  1288. //not user signal, delete
  1289. signal_map.erase(p_signal);
  1290. }
  1291. return true;
  1292. }
  1293. void Object::_set_bind(const StringName &p_set, const Variant &p_value) {
  1294. set(p_set, p_value);
  1295. }
  1296. Variant Object::_get_bind(const StringName &p_name) const {
  1297. return get(p_name);
  1298. }
  1299. void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) {
  1300. set_indexed(p_name.get_as_property_path().get_subnames(), p_value);
  1301. }
  1302. Variant Object::_get_indexed_bind(const NodePath &p_name) const {
  1303. return get_indexed(p_name.get_as_property_path().get_subnames());
  1304. }
  1305. void Object::initialize_class() {
  1306. static bool initialized = false;
  1307. if (initialized) {
  1308. return;
  1309. }
  1310. ClassDB::_add_class<Object>();
  1311. _bind_methods();
  1312. _bind_compatibility_methods();
  1313. initialized = true;
  1314. }
  1315. StringName Object::get_translation_domain() const {
  1316. return _translation_domain;
  1317. }
  1318. void Object::set_translation_domain(const StringName &p_domain) {
  1319. _translation_domain = p_domain;
  1320. }
  1321. String Object::tr(const StringName &p_message, const StringName &p_context) const {
  1322. if (!_can_translate || !TranslationServer::get_singleton()) {
  1323. return p_message;
  1324. }
  1325. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
  1326. return domain->translate(p_message, p_context);
  1327. }
  1328. String Object::tr_n(const StringName &p_message, const StringName &p_message_plural, int p_n, const StringName &p_context) const {
  1329. if (!_can_translate || !TranslationServer::get_singleton()) {
  1330. // Return message based on English plural rule if translation is not possible.
  1331. if (p_n == 1) {
  1332. return p_message;
  1333. }
  1334. return p_message_plural;
  1335. }
  1336. const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(get_translation_domain());
  1337. return domain->translate_plural(p_message, p_message_plural, p_n, p_context);
  1338. }
  1339. void Object::_clear_internal_resource_paths(const Variant &p_var) {
  1340. switch (p_var.get_type()) {
  1341. case Variant::OBJECT: {
  1342. Ref<Resource> r = p_var;
  1343. if (r.is_null()) {
  1344. return;
  1345. }
  1346. if (!r->is_built_in()) {
  1347. return; //not an internal resource
  1348. }
  1349. Object *object = p_var;
  1350. if (!object) {
  1351. return;
  1352. }
  1353. r->set_path("");
  1354. r->clear_internal_resource_paths();
  1355. } break;
  1356. case Variant::ARRAY: {
  1357. Array a = p_var;
  1358. for (const Variant &var : a) {
  1359. _clear_internal_resource_paths(var);
  1360. }
  1361. } break;
  1362. case Variant::DICTIONARY: {
  1363. Dictionary d = p_var;
  1364. List<Variant> keys;
  1365. d.get_key_list(&keys);
  1366. for (const Variant &E : keys) {
  1367. _clear_internal_resource_paths(E);
  1368. _clear_internal_resource_paths(d[E]);
  1369. }
  1370. } break;
  1371. default: {
  1372. }
  1373. }
  1374. }
  1375. #ifdef TOOLS_ENABLED
  1376. void Object::editor_set_section_unfold(const String &p_section, bool p_unfolded) {
  1377. set_edited(true);
  1378. if (p_unfolded) {
  1379. editor_section_folding.insert(p_section);
  1380. } else {
  1381. editor_section_folding.erase(p_section);
  1382. }
  1383. }
  1384. bool Object::editor_is_section_unfolded(const String &p_section) {
  1385. return editor_section_folding.has(p_section);
  1386. }
  1387. #endif
  1388. void Object::clear_internal_resource_paths() {
  1389. List<PropertyInfo> pinfo;
  1390. get_property_list(&pinfo);
  1391. for (const PropertyInfo &E : pinfo) {
  1392. _clear_internal_resource_paths(get(E.name));
  1393. }
  1394. }
  1395. void Object::notify_property_list_changed() {
  1396. emit_signal(CoreStringName(property_list_changed));
  1397. }
  1398. void Object::_bind_methods() {
  1399. ClassDB::bind_method(D_METHOD("get_class"), &Object::get_class);
  1400. ClassDB::bind_method(D_METHOD("is_class", "class"), &Object::is_class);
  1401. ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind);
  1402. ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind);
  1403. ClassDB::bind_method(D_METHOD("set_indexed", "property_path", "value"), &Object::_set_indexed_bind);
  1404. ClassDB::bind_method(D_METHOD("get_indexed", "property_path"), &Object::_get_indexed_bind);
  1405. ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind);
  1406. ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind);
  1407. ClassDB::bind_method(D_METHOD("property_can_revert", "property"), &Object::property_can_revert);
  1408. ClassDB::bind_method(D_METHOD("property_get_revert", "property"), &Object::property_get_revert);
  1409. ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false));
  1410. ClassDB::bind_method(D_METHOD("to_string"), &Object::to_string);
  1411. ClassDB::bind_method(D_METHOD("get_instance_id"), &Object::get_instance_id);
  1412. ClassDB::bind_method(D_METHOD("set_script", "script"), &Object::set_script);
  1413. ClassDB::bind_method(D_METHOD("get_script"), &Object::get_script);
  1414. ClassDB::bind_method(D_METHOD("set_meta", "name", "value"), &Object::set_meta);
  1415. ClassDB::bind_method(D_METHOD("remove_meta", "name"), &Object::remove_meta);
  1416. ClassDB::bind_method(D_METHOD("get_meta", "name", "default"), &Object::get_meta, DEFVAL(Variant()));
  1417. ClassDB::bind_method(D_METHOD("has_meta", "name"), &Object::has_meta);
  1418. ClassDB::bind_method(D_METHOD("get_meta_list"), &Object::_get_meta_list_bind);
  1419. ClassDB::bind_method(D_METHOD("add_user_signal", "signal", "arguments"), &Object::_add_user_signal, DEFVAL(Array()));
  1420. ClassDB::bind_method(D_METHOD("has_user_signal", "signal"), &Object::_has_user_signal);
  1421. ClassDB::bind_method(D_METHOD("remove_user_signal", "signal"), &Object::_remove_user_signal);
  1422. {
  1423. MethodInfo mi;
  1424. mi.name = "emit_signal";
  1425. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "signal"));
  1426. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "emit_signal", &Object::_emit_signal, mi, varray(), false);
  1427. }
  1428. {
  1429. MethodInfo mi;
  1430. mi.name = "call";
  1431. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1432. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call", &Object::_call_bind, mi);
  1433. }
  1434. {
  1435. MethodInfo mi;
  1436. mi.name = "call_deferred";
  1437. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1438. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_deferred", &Object::_call_deferred_bind, mi, varray(), false);
  1439. }
  1440. ClassDB::bind_method(D_METHOD("set_deferred", "property", "value"), &Object::set_deferred);
  1441. ClassDB::bind_method(D_METHOD("callv", "method", "arg_array"), &Object::callv);
  1442. ClassDB::bind_method(D_METHOD("has_method", "method"), &Object::has_method);
  1443. ClassDB::bind_method(D_METHOD("get_method_argument_count", "method"), &Object::_get_method_argument_count_bind);
  1444. ClassDB::bind_method(D_METHOD("has_signal", "signal"), &Object::has_signal);
  1445. ClassDB::bind_method(D_METHOD("get_signal_list"), &Object::_get_signal_list);
  1446. ClassDB::bind_method(D_METHOD("get_signal_connection_list", "signal"), &Object::_get_signal_connection_list);
  1447. ClassDB::bind_method(D_METHOD("get_incoming_connections"), &Object::_get_incoming_connections);
  1448. ClassDB::bind_method(D_METHOD("connect", "signal", "callable", "flags"), &Object::connect, DEFVAL(0));
  1449. ClassDB::bind_method(D_METHOD("disconnect", "signal", "callable"), &Object::disconnect);
  1450. ClassDB::bind_method(D_METHOD("is_connected", "signal", "callable"), &Object::is_connected);
  1451. ClassDB::bind_method(D_METHOD("has_connections", "signal"), &Object::has_connections);
  1452. ClassDB::bind_method(D_METHOD("set_block_signals", "enable"), &Object::set_block_signals);
  1453. ClassDB::bind_method(D_METHOD("is_blocking_signals"), &Object::is_blocking_signals);
  1454. ClassDB::bind_method(D_METHOD("notify_property_list_changed"), &Object::notify_property_list_changed);
  1455. ClassDB::bind_method(D_METHOD("set_message_translation", "enable"), &Object::set_message_translation);
  1456. ClassDB::bind_method(D_METHOD("can_translate_messages"), &Object::can_translate_messages);
  1457. ClassDB::bind_method(D_METHOD("tr", "message", "context"), &Object::tr, DEFVAL(StringName()));
  1458. ClassDB::bind_method(D_METHOD("tr_n", "message", "plural_message", "n", "context"), &Object::tr_n, DEFVAL(StringName()));
  1459. ClassDB::bind_method(D_METHOD("get_translation_domain"), &Object::get_translation_domain);
  1460. ClassDB::bind_method(D_METHOD("set_translation_domain", "domain"), &Object::set_translation_domain);
  1461. ClassDB::bind_method(D_METHOD("is_queued_for_deletion"), &Object::is_queued_for_deletion);
  1462. ClassDB::bind_method(D_METHOD("cancel_free"), &Object::cancel_free);
  1463. ClassDB::add_virtual_method("Object", MethodInfo("free"), false);
  1464. ADD_SIGNAL(MethodInfo("script_changed"));
  1465. ADD_SIGNAL(MethodInfo("property_list_changed"));
  1466. #define BIND_OBJ_CORE_METHOD(m_method) \
  1467. ::ClassDB::add_virtual_method(get_class_static(), m_method, true, Vector<String>(), true);
  1468. BIND_OBJ_CORE_METHOD(MethodInfo("_init"));
  1469. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::STRING, "_to_string"));
  1470. {
  1471. MethodInfo mi("_notification");
  1472. mi.arguments.push_back(PropertyInfo(Variant::INT, "what"));
  1473. mi.arguments_metadata.push_back(GodotTypeInfo::Metadata::METADATA_INT_IS_INT32);
  1474. BIND_OBJ_CORE_METHOD(mi);
  1475. }
  1476. {
  1477. MethodInfo mi("_set");
  1478. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "property"));
  1479. mi.arguments.push_back(PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1480. mi.return_val.type = Variant::BOOL;
  1481. BIND_OBJ_CORE_METHOD(mi);
  1482. }
  1483. #ifdef TOOLS_ENABLED
  1484. {
  1485. MethodInfo mi("_get");
  1486. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "property"));
  1487. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1488. BIND_OBJ_CORE_METHOD(mi);
  1489. }
  1490. {
  1491. MethodInfo mi("_get_property_list");
  1492. mi.return_val.type = Variant::ARRAY;
  1493. mi.return_val.hint = PROPERTY_HINT_ARRAY_TYPE;
  1494. mi.return_val.hint_string = "Dictionary";
  1495. BIND_OBJ_CORE_METHOD(mi);
  1496. }
  1497. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::NIL, "_validate_property", PropertyInfo(Variant::DICTIONARY, "property")));
  1498. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_property_can_revert", PropertyInfo(Variant::STRING_NAME, "property")));
  1499. {
  1500. MethodInfo mi("_property_get_revert");
  1501. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "property"));
  1502. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1503. BIND_OBJ_CORE_METHOD(mi);
  1504. }
  1505. // These are actually `Variant` methods, but that doesn't matter since scripts can't inherit built-in types.
  1506. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_iter_init", PropertyInfo(Variant::ARRAY, "iter")));
  1507. BIND_OBJ_CORE_METHOD(MethodInfo(Variant::BOOL, "_iter_next", PropertyInfo(Variant::ARRAY, "iter")));
  1508. {
  1509. MethodInfo mi("_iter_get");
  1510. mi.arguments.push_back(PropertyInfo(Variant::NIL, "iter", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1511. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1512. BIND_OBJ_CORE_METHOD(mi);
  1513. }
  1514. #endif
  1515. BIND_CONSTANT(NOTIFICATION_POSTINITIALIZE);
  1516. BIND_CONSTANT(NOTIFICATION_PREDELETE);
  1517. BIND_CONSTANT(NOTIFICATION_EXTENSION_RELOADED);
  1518. BIND_ENUM_CONSTANT(CONNECT_DEFERRED);
  1519. BIND_ENUM_CONSTANT(CONNECT_PERSIST);
  1520. BIND_ENUM_CONSTANT(CONNECT_ONE_SHOT);
  1521. BIND_ENUM_CONSTANT(CONNECT_REFERENCE_COUNTED);
  1522. }
  1523. void Object::set_deferred(const StringName &p_property, const Variant &p_value) {
  1524. MessageQueue::get_singleton()->push_set(this, p_property, p_value);
  1525. }
  1526. void Object::set_block_signals(bool p_block) {
  1527. _block_signals = p_block;
  1528. }
  1529. bool Object::is_blocking_signals() const {
  1530. return _block_signals;
  1531. }
  1532. Variant::Type Object::get_static_property_type(const StringName &p_property, bool *r_valid) const {
  1533. bool valid;
  1534. Variant::Type t = ClassDB::get_property_type(get_class_name(), p_property, &valid);
  1535. if (valid) {
  1536. if (r_valid) {
  1537. *r_valid = true;
  1538. }
  1539. return t;
  1540. }
  1541. if (get_script_instance()) {
  1542. return get_script_instance()->get_property_type(p_property, r_valid);
  1543. }
  1544. if (r_valid) {
  1545. *r_valid = false;
  1546. }
  1547. return Variant::NIL;
  1548. }
  1549. Variant::Type Object::get_static_property_type_indexed(const Vector<StringName> &p_path, bool *r_valid) const {
  1550. if (p_path.size() == 0) {
  1551. if (r_valid) {
  1552. *r_valid = false;
  1553. }
  1554. return Variant::NIL;
  1555. }
  1556. bool valid = false;
  1557. Variant::Type t = get_static_property_type(p_path[0], &valid);
  1558. if (!valid) {
  1559. if (r_valid) {
  1560. *r_valid = false;
  1561. }
  1562. return Variant::NIL;
  1563. }
  1564. Callable::CallError ce;
  1565. Variant check;
  1566. Variant::construct(t, check, nullptr, 0, ce);
  1567. for (int i = 1; i < p_path.size(); i++) {
  1568. if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) {
  1569. // We cannot be sure about the type of properties this type can have
  1570. if (r_valid) {
  1571. *r_valid = false;
  1572. }
  1573. return Variant::NIL;
  1574. }
  1575. check = check.get_named(p_path[i], valid);
  1576. if (!valid) {
  1577. if (r_valid) {
  1578. *r_valid = false;
  1579. }
  1580. return Variant::NIL;
  1581. }
  1582. }
  1583. if (r_valid) {
  1584. *r_valid = true;
  1585. }
  1586. return check.get_type();
  1587. }
  1588. bool Object::is_queued_for_deletion() const {
  1589. return _is_queued_for_deletion;
  1590. }
  1591. #ifdef TOOLS_ENABLED
  1592. void Object::set_edited(bool p_edited) {
  1593. _edited = p_edited;
  1594. _edited_version++;
  1595. }
  1596. bool Object::is_edited() const {
  1597. return _edited;
  1598. }
  1599. uint32_t Object::get_edited_version() const {
  1600. return _edited_version;
  1601. }
  1602. #endif
  1603. StringName Object::get_class_name_for_extension(const GDExtension *p_library) const {
  1604. #ifdef TOOLS_ENABLED
  1605. // If this is the library this extension comes from and it's a placeholder, we
  1606. // have to return the closest native parent's class name, so that it doesn't try to
  1607. // use this like the real object.
  1608. if (unlikely(_extension && _extension->library == p_library && _extension->is_placeholder)) {
  1609. const StringName *class_name = _get_class_namev();
  1610. return *class_name;
  1611. }
  1612. #endif
  1613. // Only return the class name per the extension if it matches the given p_library.
  1614. if (_extension && _extension->library == p_library) {
  1615. return _extension->class_name;
  1616. }
  1617. // Extensions only have wrapper classes for classes exposed in ClassDB.
  1618. const StringName *class_name = _get_class_namev();
  1619. if (ClassDB::is_class_exposed(*class_name)) {
  1620. return *class_name;
  1621. }
  1622. // Find the nearest parent class that's exposed.
  1623. StringName parent_class = ClassDB::get_parent_class(*class_name);
  1624. while (parent_class != StringName()) {
  1625. if (ClassDB::is_class_exposed(parent_class)) {
  1626. return parent_class;
  1627. }
  1628. parent_class = ClassDB::get_parent_class(parent_class);
  1629. }
  1630. return SNAME("Object");
  1631. }
  1632. void Object::set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1633. // This is only meant to be used on creation by the binder, but we also
  1634. // need to account for reloading (where the 'binding' will be cleared).
  1635. ERR_FAIL_COND(_instance_bindings != nullptr && _instance_bindings[0].binding != nullptr);
  1636. if (_instance_bindings == nullptr) {
  1637. _instance_bindings = (InstanceBinding *)memalloc(sizeof(InstanceBinding));
  1638. _instance_binding_count = 1;
  1639. }
  1640. _instance_bindings[0].binding = p_binding;
  1641. _instance_bindings[0].free_callback = p_callbacks->free_callback;
  1642. _instance_bindings[0].reference_callback = p_callbacks->reference_callback;
  1643. _instance_bindings[0].token = p_token;
  1644. }
  1645. void *Object::get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
  1646. void *binding = nullptr;
  1647. MutexLock instance_binding_lock(_instance_binding_mutex);
  1648. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1649. if (_instance_bindings[i].token == p_token) {
  1650. binding = _instance_bindings[i].binding;
  1651. break;
  1652. }
  1653. }
  1654. if (unlikely(!binding && p_callbacks)) {
  1655. uint32_t current_size = next_power_of_2(_instance_binding_count);
  1656. uint32_t new_size = next_power_of_2(_instance_binding_count + 1);
  1657. if (current_size == 0 || new_size > current_size) {
  1658. _instance_bindings = (InstanceBinding *)memrealloc(_instance_bindings, new_size * sizeof(InstanceBinding));
  1659. }
  1660. _instance_bindings[_instance_binding_count].free_callback = p_callbacks->free_callback;
  1661. _instance_bindings[_instance_binding_count].reference_callback = p_callbacks->reference_callback;
  1662. _instance_bindings[_instance_binding_count].token = p_token;
  1663. binding = p_callbacks->create_callback(p_token, this);
  1664. _instance_bindings[_instance_binding_count].binding = binding;
  1665. #ifdef TOOLS_ENABLED
  1666. if (!_extension && Engine::get_singleton()->is_extension_reloading_enabled()) {
  1667. GDExtensionManager::get_singleton()->track_instance_binding(p_token, this);
  1668. }
  1669. #endif
  1670. _instance_binding_count++;
  1671. }
  1672. return binding;
  1673. }
  1674. bool Object::has_instance_binding(void *p_token) {
  1675. bool found = false;
  1676. MutexLock instance_binding_lock(_instance_binding_mutex);
  1677. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1678. if (_instance_bindings[i].token == p_token) {
  1679. found = true;
  1680. break;
  1681. }
  1682. }
  1683. return found;
  1684. }
  1685. void Object::free_instance_binding(void *p_token) {
  1686. bool found = false;
  1687. MutexLock instance_binding_lock(_instance_binding_mutex);
  1688. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1689. if (!found && _instance_bindings[i].token == p_token) {
  1690. if (_instance_bindings[i].free_callback) {
  1691. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1692. }
  1693. found = true;
  1694. }
  1695. if (found) {
  1696. if (i + 1 < _instance_binding_count) {
  1697. _instance_bindings[i] = _instance_bindings[i + 1];
  1698. } else {
  1699. _instance_bindings[i] = { nullptr };
  1700. }
  1701. }
  1702. }
  1703. if (found) {
  1704. _instance_binding_count--;
  1705. }
  1706. }
  1707. #ifdef TOOLS_ENABLED
  1708. void Object::clear_internal_extension() {
  1709. ERR_FAIL_NULL(_extension);
  1710. // Free the instance inside the GDExtension.
  1711. if (_extension->free_instance) {
  1712. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1713. }
  1714. _extension = nullptr;
  1715. _extension_instance = nullptr;
  1716. // Clear the instance bindings.
  1717. _instance_binding_mutex.lock();
  1718. if (_instance_bindings) {
  1719. if (_instance_bindings[0].free_callback) {
  1720. _instance_bindings[0].free_callback(_instance_bindings[0].token, this, _instance_bindings[0].binding);
  1721. }
  1722. _instance_bindings[0].binding = nullptr;
  1723. _instance_bindings[0].token = nullptr;
  1724. _instance_bindings[0].free_callback = nullptr;
  1725. _instance_bindings[0].reference_callback = nullptr;
  1726. }
  1727. _instance_binding_mutex.unlock();
  1728. // Clear the virtual methods.
  1729. while (virtual_method_list) {
  1730. (*virtual_method_list->method) = nullptr;
  1731. (*virtual_method_list->initialized) = false;
  1732. virtual_method_list = virtual_method_list->next;
  1733. }
  1734. }
  1735. void Object::reset_internal_extension(ObjectGDExtension *p_extension) {
  1736. ERR_FAIL_COND(_extension != nullptr);
  1737. if (p_extension) {
  1738. _extension_instance = p_extension->recreate_instance ? p_extension->recreate_instance(p_extension->class_userdata, (GDExtensionObjectPtr)this) : nullptr;
  1739. ERR_FAIL_NULL_MSG(_extension_instance, "Unable to recreate GDExtension instance - does this extension support hot reloading?");
  1740. _extension = p_extension;
  1741. }
  1742. }
  1743. #endif
  1744. void Object::_construct_object(bool p_reference) {
  1745. type_is_reference = p_reference;
  1746. _instance_id = ObjectDB::add_instance(this);
  1747. #ifdef DEBUG_ENABLED
  1748. _lock_index.init(1);
  1749. #endif
  1750. }
  1751. Object::Object(bool p_reference) {
  1752. _construct_object(p_reference);
  1753. }
  1754. Object::Object() {
  1755. _construct_object(false);
  1756. }
  1757. void Object::detach_from_objectdb() {
  1758. if (_instance_id != ObjectID()) {
  1759. ObjectDB::remove_instance(this);
  1760. _instance_id = ObjectID();
  1761. }
  1762. }
  1763. Object::~Object() {
  1764. if (script_instance) {
  1765. memdelete(script_instance);
  1766. }
  1767. script_instance = nullptr;
  1768. if (_extension) {
  1769. #ifdef TOOLS_ENABLED
  1770. if (_extension->untrack_instance) {
  1771. _extension->untrack_instance(_extension->tracking_userdata, this);
  1772. }
  1773. #endif
  1774. if (_extension->free_instance) {
  1775. _extension->free_instance(_extension->class_userdata, _extension_instance);
  1776. }
  1777. _extension = nullptr;
  1778. _extension_instance = nullptr;
  1779. }
  1780. #ifdef TOOLS_ENABLED
  1781. else if (_instance_bindings != nullptr) {
  1782. Engine *engine = Engine::get_singleton();
  1783. GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton();
  1784. if (engine && gdextension_manager && engine->is_extension_reloading_enabled()) {
  1785. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1786. gdextension_manager->untrack_instance_binding(_instance_bindings[i].token, this);
  1787. }
  1788. }
  1789. }
  1790. #endif
  1791. if (_emitting) {
  1792. //@todo this may need to actually reach the debugger prioritarily somehow because it may crash before
  1793. ERR_PRINT(vformat("Object '%s' was freed or unreferenced while a signal is being emitted from it. Try connecting to the signal using 'CONNECT_DEFERRED' flag, or use queue_free() to free the object (if this object is a Node) to avoid this error and potential crashes.", to_string()));
  1794. }
  1795. // Drop all connections to the signals of this object.
  1796. while (signal_map.size()) {
  1797. // Avoid regular iteration so erasing is safe.
  1798. KeyValue<StringName, SignalData> &E = *signal_map.begin();
  1799. SignalData *s = &E.value;
  1800. for (const KeyValue<Callable, SignalData::Slot> &slot_kv : s->slot_map) {
  1801. Object *target = slot_kv.value.conn.callable.get_object();
  1802. if (likely(target)) {
  1803. target->connections.erase(slot_kv.value.cE);
  1804. }
  1805. }
  1806. signal_map.erase(E.key);
  1807. }
  1808. // Disconnect signals that connect to this object.
  1809. while (connections.size()) {
  1810. Connection c = connections.front()->get();
  1811. Object *obj = c.callable.get_object();
  1812. bool disconnected = false;
  1813. if (likely(obj)) {
  1814. disconnected = c.signal.get_object()->_disconnect(c.signal.get_name(), c.callable, true);
  1815. }
  1816. if (unlikely(!disconnected)) {
  1817. // If the disconnect has failed, abandon the connection to avoid getting trapped in an infinite loop here.
  1818. connections.pop_front();
  1819. }
  1820. }
  1821. if (_instance_id != ObjectID()) {
  1822. ObjectDB::remove_instance(this);
  1823. _instance_id = ObjectID();
  1824. }
  1825. _predelete_ok = 2;
  1826. if (_instance_bindings != nullptr) {
  1827. for (uint32_t i = 0; i < _instance_binding_count; i++) {
  1828. if (_instance_bindings[i].free_callback) {
  1829. _instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
  1830. }
  1831. }
  1832. memfree(_instance_bindings);
  1833. }
  1834. }
  1835. bool predelete_handler(Object *p_object) {
  1836. return p_object->_predelete();
  1837. }
  1838. void postinitialize_handler(Object *p_object) {
  1839. p_object->_initialize();
  1840. p_object->_postinitialize();
  1841. }
  1842. void ObjectDB::debug_objects(DebugFunc p_func) {
  1843. spin_lock.lock();
  1844. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1845. if (object_slots[i].validator) {
  1846. p_func(object_slots[i].object);
  1847. count--;
  1848. }
  1849. }
  1850. spin_lock.unlock();
  1851. }
  1852. #ifdef TOOLS_ENABLED
  1853. void Object::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1854. const String pf = p_function;
  1855. if (p_idx == 0) {
  1856. if (pf == "connect" || pf == "is_connected" || pf == "disconnect" || pf == "emit_signal" || pf == "has_signal") {
  1857. List<MethodInfo> signals;
  1858. get_signal_list(&signals);
  1859. for (const MethodInfo &E : signals) {
  1860. r_options->push_back(E.name.quote());
  1861. }
  1862. } else if (pf == "call" || pf == "call_deferred" || pf == "callv" || pf == "has_method") {
  1863. List<MethodInfo> methods;
  1864. get_method_list(&methods);
  1865. for (const MethodInfo &E : methods) {
  1866. if (E.name.begins_with("_") && !(E.flags & METHOD_FLAG_VIRTUAL)) {
  1867. continue;
  1868. }
  1869. r_options->push_back(E.name.quote());
  1870. }
  1871. } else if (pf == "set" || pf == "set_deferred" || pf == "get") {
  1872. List<PropertyInfo> properties;
  1873. get_property_list(&properties);
  1874. for (const PropertyInfo &E : properties) {
  1875. if (E.usage & PROPERTY_USAGE_DEFAULT && !(E.usage & PROPERTY_USAGE_INTERNAL)) {
  1876. r_options->push_back(E.name.quote());
  1877. }
  1878. }
  1879. } else if (pf == "set_meta" || pf == "get_meta" || pf == "has_meta" || pf == "remove_meta") {
  1880. for (const KeyValue<StringName, Variant> &K : metadata) {
  1881. r_options->push_back(String(K.key).quote());
  1882. }
  1883. }
  1884. } else if (p_idx == 2) {
  1885. if (pf == "connect") {
  1886. // Ideally, the constants should be inferred by the parameter.
  1887. // But a parameter's PropertyInfo does not store the enum they come from, so this will do for now.
  1888. List<StringName> constants;
  1889. ClassDB::get_enum_constants("Object", "ConnectFlags", &constants);
  1890. for (const StringName &E : constants) {
  1891. r_options->push_back(String(E));
  1892. }
  1893. }
  1894. }
  1895. }
  1896. #endif
  1897. SpinLock ObjectDB::spin_lock;
  1898. uint32_t ObjectDB::slot_count = 0;
  1899. uint32_t ObjectDB::slot_max = 0;
  1900. ObjectDB::ObjectSlot *ObjectDB::object_slots = nullptr;
  1901. uint64_t ObjectDB::validator_counter = 0;
  1902. int ObjectDB::get_object_count() {
  1903. return slot_count;
  1904. }
  1905. ObjectID ObjectDB::add_instance(Object *p_object) {
  1906. spin_lock.lock();
  1907. if (unlikely(slot_count == slot_max)) {
  1908. CRASH_COND(slot_count == (1 << OBJECTDB_SLOT_MAX_COUNT_BITS));
  1909. uint32_t new_slot_max = slot_max > 0 ? slot_max * 2 : 1;
  1910. object_slots = (ObjectSlot *)memrealloc(object_slots, sizeof(ObjectSlot) * new_slot_max);
  1911. for (uint32_t i = slot_max; i < new_slot_max; i++) {
  1912. object_slots[i].object = nullptr;
  1913. object_slots[i].is_ref_counted = false;
  1914. object_slots[i].next_free = i;
  1915. object_slots[i].validator = 0;
  1916. }
  1917. slot_max = new_slot_max;
  1918. }
  1919. uint32_t slot = object_slots[slot_count].next_free;
  1920. if (object_slots[slot].object != nullptr) {
  1921. spin_lock.unlock();
  1922. ERR_FAIL_COND_V(object_slots[slot].object != nullptr, ObjectID());
  1923. }
  1924. object_slots[slot].object = p_object;
  1925. object_slots[slot].is_ref_counted = p_object->is_ref_counted();
  1926. validator_counter = (validator_counter + 1) & OBJECTDB_VALIDATOR_MASK;
  1927. if (unlikely(validator_counter == 0)) {
  1928. validator_counter = 1;
  1929. }
  1930. object_slots[slot].validator = validator_counter;
  1931. uint64_t id = validator_counter;
  1932. id <<= OBJECTDB_SLOT_MAX_COUNT_BITS;
  1933. id |= uint64_t(slot);
  1934. if (p_object->is_ref_counted()) {
  1935. id |= OBJECTDB_REFERENCE_BIT;
  1936. }
  1937. slot_count++;
  1938. spin_lock.unlock();
  1939. return ObjectID(id);
  1940. }
  1941. void ObjectDB::remove_instance(Object *p_object) {
  1942. uint64_t t = p_object->get_instance_id();
  1943. uint32_t slot = t & OBJECTDB_SLOT_MAX_COUNT_MASK; //slot is always valid on valid object
  1944. spin_lock.lock();
  1945. #ifdef DEBUG_ENABLED
  1946. if (object_slots[slot].object != p_object) {
  1947. spin_lock.unlock();
  1948. ERR_FAIL_COND(object_slots[slot].object != p_object);
  1949. }
  1950. {
  1951. uint64_t validator = (t >> OBJECTDB_SLOT_MAX_COUNT_BITS) & OBJECTDB_VALIDATOR_MASK;
  1952. if (object_slots[slot].validator != validator) {
  1953. spin_lock.unlock();
  1954. ERR_FAIL_COND(object_slots[slot].validator != validator);
  1955. }
  1956. }
  1957. #endif
  1958. //decrease slot count
  1959. slot_count--;
  1960. //set the free slot properly
  1961. object_slots[slot_count].next_free = slot;
  1962. //invalidate, so checks against it fail
  1963. object_slots[slot].validator = 0;
  1964. object_slots[slot].is_ref_counted = false;
  1965. object_slots[slot].object = nullptr;
  1966. spin_lock.unlock();
  1967. }
  1968. void ObjectDB::setup() {
  1969. //nothing to do now
  1970. }
  1971. void ObjectDB::cleanup() {
  1972. spin_lock.lock();
  1973. if (slot_count > 0) {
  1974. WARN_PRINT("ObjectDB instances leaked at exit (run with --verbose for details).");
  1975. if (OS::get_singleton()->is_stdout_verbose()) {
  1976. // Ensure calling the native classes because if a leaked instance has a script
  1977. // that overrides any of those methods, it'd not be OK to call them at this point,
  1978. // now the scripting languages have already been terminated.
  1979. MethodBind *node_get_path = ClassDB::get_method("Node", "get_path");
  1980. MethodBind *resource_get_path = ClassDB::get_method("Resource", "get_path");
  1981. Callable::CallError call_error;
  1982. for (uint32_t i = 0, count = slot_count; i < slot_max && count != 0; i++) {
  1983. if (object_slots[i].validator) {
  1984. Object *obj = object_slots[i].object;
  1985. String extra_info;
  1986. if (obj->is_class("Node")) {
  1987. extra_info = " - Node path: " + String(node_get_path->call(obj, nullptr, 0, call_error));
  1988. }
  1989. if (obj->is_class("Resource")) {
  1990. extra_info = " - Resource path: " + String(resource_get_path->call(obj, nullptr, 0, call_error));
  1991. }
  1992. uint64_t id = uint64_t(i) | (uint64_t(object_slots[i].validator) << OBJECTDB_SLOT_MAX_COUNT_BITS) | (object_slots[i].is_ref_counted ? OBJECTDB_REFERENCE_BIT : 0);
  1993. DEV_ASSERT(id == (uint64_t)obj->get_instance_id()); // We could just use the id from the object, but this check may help catching memory corruption catastrophes.
  1994. print_line("Leaked instance: " + String(obj->get_class()) + ":" + uitos(id) + extra_info);
  1995. count--;
  1996. }
  1997. }
  1998. print_line("Hint: Leaked instances typically happen when nodes are removed from the scene tree (with `remove_child()`) but not freed (with `free()` or `queue_free()`).");
  1999. }
  2000. }
  2001. if (object_slots) {
  2002. memfree(object_slots);
  2003. }
  2004. spin_lock.unlock();
  2005. }