script_language.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /**************************************************************************/
  2. /* script_language.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 "script_language.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/core_string_names.h"
  33. #include "core/debugger/engine_debugger.h"
  34. #include "core/debugger/script_debugger.h"
  35. #include "core/variant/typed_array.h"
  36. #include <stdint.h>
  37. ScriptLanguage *ScriptServer::_languages[MAX_LANGUAGES];
  38. int ScriptServer::_language_count = 0;
  39. bool ScriptServer::scripting_enabled = true;
  40. bool ScriptServer::reload_scripts_on_save = false;
  41. bool ScriptServer::languages_finished = false;
  42. ScriptEditRequestFunction ScriptServer::edit_request_func = nullptr;
  43. void Script::_notification(int p_what) {
  44. switch (p_what) {
  45. case NOTIFICATION_POSTINITIALIZE: {
  46. if (EngineDebugger::is_active()) {
  47. EngineDebugger::get_script_debugger()->set_break_language(get_language());
  48. }
  49. } break;
  50. }
  51. }
  52. Variant Script::_get_property_default_value(const StringName &p_property) {
  53. Variant ret;
  54. get_property_default_value(p_property, ret);
  55. return ret;
  56. }
  57. TypedArray<Dictionary> Script::_get_script_property_list() {
  58. TypedArray<Dictionary> ret;
  59. List<PropertyInfo> list;
  60. get_script_property_list(&list);
  61. for (const PropertyInfo &E : list) {
  62. ret.append(E.operator Dictionary());
  63. }
  64. return ret;
  65. }
  66. TypedArray<Dictionary> Script::_get_script_method_list() {
  67. TypedArray<Dictionary> ret;
  68. List<MethodInfo> list;
  69. get_script_method_list(&list);
  70. for (const MethodInfo &E : list) {
  71. ret.append(E.operator Dictionary());
  72. }
  73. return ret;
  74. }
  75. TypedArray<Dictionary> Script::_get_script_signal_list() {
  76. TypedArray<Dictionary> ret;
  77. List<MethodInfo> list;
  78. get_script_signal_list(&list);
  79. for (const MethodInfo &E : list) {
  80. ret.append(E.operator Dictionary());
  81. }
  82. return ret;
  83. }
  84. Dictionary Script::_get_script_constant_map() {
  85. Dictionary ret;
  86. HashMap<StringName, Variant> map;
  87. get_constants(&map);
  88. for (const KeyValue<StringName, Variant> &E : map) {
  89. ret[E.key] = E.value;
  90. }
  91. return ret;
  92. }
  93. #ifdef TOOLS_ENABLED
  94. PropertyInfo Script::get_class_category() const {
  95. String path = get_path();
  96. String scr_name;
  97. if (is_built_in()) {
  98. if (get_name().is_empty()) {
  99. scr_name = TTR("Built-in script");
  100. } else {
  101. scr_name = vformat("%s (%s)", get_name(), TTR("Built-in"));
  102. }
  103. } else {
  104. if (get_name().is_empty()) {
  105. scr_name = path.get_file();
  106. } else {
  107. scr_name = get_name();
  108. }
  109. }
  110. return PropertyInfo(Variant::NIL, scr_name, PROPERTY_HINT_NONE, path, PROPERTY_USAGE_CATEGORY);
  111. }
  112. #endif // TOOLS_ENABLED
  113. void Script::_bind_methods() {
  114. ClassDB::bind_method(D_METHOD("can_instantiate"), &Script::can_instantiate);
  115. //ClassDB::bind_method(D_METHOD("instance_create","base_object"),&Script::instance_create);
  116. ClassDB::bind_method(D_METHOD("instance_has", "base_object"), &Script::instance_has);
  117. ClassDB::bind_method(D_METHOD("has_source_code"), &Script::has_source_code);
  118. ClassDB::bind_method(D_METHOD("get_source_code"), &Script::get_source_code);
  119. ClassDB::bind_method(D_METHOD("set_source_code", "source"), &Script::set_source_code);
  120. ClassDB::bind_method(D_METHOD("reload", "keep_state"), &Script::reload, DEFVAL(false));
  121. ClassDB::bind_method(D_METHOD("get_base_script"), &Script::get_base_script);
  122. ClassDB::bind_method(D_METHOD("get_instance_base_type"), &Script::get_instance_base_type);
  123. ClassDB::bind_method(D_METHOD("has_script_signal", "signal_name"), &Script::has_script_signal);
  124. ClassDB::bind_method(D_METHOD("get_script_property_list"), &Script::_get_script_property_list);
  125. ClassDB::bind_method(D_METHOD("get_script_method_list"), &Script::_get_script_method_list);
  126. ClassDB::bind_method(D_METHOD("get_script_signal_list"), &Script::_get_script_signal_list);
  127. ClassDB::bind_method(D_METHOD("get_script_constant_map"), &Script::_get_script_constant_map);
  128. ClassDB::bind_method(D_METHOD("get_property_default_value", "property"), &Script::_get_property_default_value);
  129. ClassDB::bind_method(D_METHOD("is_tool"), &Script::is_tool);
  130. ADD_PROPERTY(PropertyInfo(Variant::STRING, "source_code", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_source_code", "get_source_code");
  131. }
  132. void ScriptServer::set_scripting_enabled(bool p_enabled) {
  133. scripting_enabled = p_enabled;
  134. }
  135. bool ScriptServer::is_scripting_enabled() {
  136. return scripting_enabled;
  137. }
  138. ScriptLanguage *ScriptServer::get_language(int p_idx) {
  139. ERR_FAIL_INDEX_V(p_idx, _language_count, nullptr);
  140. return _languages[p_idx];
  141. }
  142. void ScriptServer::register_language(ScriptLanguage *p_language) {
  143. ERR_FAIL_NULL(p_language);
  144. ERR_FAIL_COND(_language_count >= MAX_LANGUAGES);
  145. _languages[_language_count++] = p_language;
  146. }
  147. void ScriptServer::unregister_language(const ScriptLanguage *p_language) {
  148. for (int i = 0; i < _language_count; i++) {
  149. if (_languages[i] == p_language) {
  150. _language_count--;
  151. if (i < _language_count) {
  152. SWAP(_languages[i], _languages[_language_count]);
  153. }
  154. return;
  155. }
  156. }
  157. }
  158. void ScriptServer::init_languages() {
  159. { // Load global classes.
  160. global_classes_clear();
  161. #ifndef DISABLE_DEPRECATED
  162. if (ProjectSettings::get_singleton()->has_setting("_global_script_classes")) {
  163. Array script_classes = GLOBAL_GET("_global_script_classes");
  164. for (int i = 0; i < script_classes.size(); i++) {
  165. Dictionary c = script_classes[i];
  166. if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base")) {
  167. continue;
  168. }
  169. add_global_class(c["class"], c["base"], c["language"], c["path"]);
  170. }
  171. ProjectSettings::get_singleton()->clear("_global_script_classes");
  172. }
  173. #endif
  174. Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
  175. for (int i = 0; i < script_classes.size(); i++) {
  176. Dictionary c = script_classes[i];
  177. if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base")) {
  178. continue;
  179. }
  180. add_global_class(c["class"], c["base"], c["language"], c["path"]);
  181. }
  182. }
  183. for (int i = 0; i < _language_count; i++) {
  184. _languages[i]->init();
  185. }
  186. }
  187. void ScriptServer::finish_languages() {
  188. for (int i = 0; i < _language_count; i++) {
  189. _languages[i]->finish();
  190. }
  191. global_classes_clear();
  192. languages_finished = true;
  193. }
  194. void ScriptServer::set_reload_scripts_on_save(bool p_enable) {
  195. reload_scripts_on_save = p_enable;
  196. }
  197. bool ScriptServer::is_reload_scripts_on_save_enabled() {
  198. return reload_scripts_on_save;
  199. }
  200. void ScriptServer::thread_enter() {
  201. for (int i = 0; i < _language_count; i++) {
  202. _languages[i]->thread_enter();
  203. }
  204. }
  205. void ScriptServer::thread_exit() {
  206. for (int i = 0; i < _language_count; i++) {
  207. _languages[i]->thread_exit();
  208. }
  209. }
  210. HashMap<StringName, ScriptServer::GlobalScriptClass> ScriptServer::global_classes;
  211. HashMap<StringName, Vector<StringName>> ScriptServer::inheriters_cache;
  212. bool ScriptServer::inheriters_cache_dirty = true;
  213. void ScriptServer::global_classes_clear() {
  214. global_classes.clear();
  215. inheriters_cache.clear();
  216. }
  217. void ScriptServer::add_global_class(const StringName &p_class, const StringName &p_base, const StringName &p_language, const String &p_path) {
  218. ERR_FAIL_COND_MSG(p_class == p_base || (global_classes.has(p_base) && get_global_class_native_base(p_base) == p_class), "Cyclic inheritance in script class.");
  219. GlobalScriptClass g;
  220. g.language = p_language;
  221. g.path = p_path;
  222. g.base = p_base;
  223. global_classes[p_class] = g;
  224. inheriters_cache_dirty = true;
  225. }
  226. void ScriptServer::remove_global_class(const StringName &p_class) {
  227. global_classes.erase(p_class);
  228. inheriters_cache_dirty = true;
  229. }
  230. void ScriptServer::get_inheriters_list(const StringName &p_base_type, List<StringName> *r_classes) {
  231. if (inheriters_cache_dirty) {
  232. inheriters_cache.clear();
  233. for (const KeyValue<StringName, GlobalScriptClass> &K : global_classes) {
  234. if (!inheriters_cache.has(K.value.base)) {
  235. inheriters_cache[K.value.base] = Vector<StringName>();
  236. }
  237. inheriters_cache[K.value.base].push_back(K.key);
  238. }
  239. for (KeyValue<StringName, Vector<StringName>> &K : inheriters_cache) {
  240. K.value.sort_custom<StringName::AlphCompare>();
  241. }
  242. inheriters_cache_dirty = false;
  243. }
  244. if (!inheriters_cache.has(p_base_type)) {
  245. return;
  246. }
  247. const Vector<StringName> &v = inheriters_cache[p_base_type];
  248. for (int i = 0; i < v.size(); i++) {
  249. r_classes->push_back(v[i]);
  250. }
  251. }
  252. void ScriptServer::remove_global_class_by_path(const String &p_path) {
  253. for (const KeyValue<StringName, GlobalScriptClass> &kv : global_classes) {
  254. if (kv.value.path == p_path) {
  255. global_classes.erase(kv.key);
  256. inheriters_cache_dirty = true;
  257. return;
  258. }
  259. }
  260. }
  261. bool ScriptServer::is_global_class(const StringName &p_class) {
  262. return global_classes.has(p_class);
  263. }
  264. StringName ScriptServer::get_global_class_language(const StringName &p_class) {
  265. ERR_FAIL_COND_V(!global_classes.has(p_class), StringName());
  266. return global_classes[p_class].language;
  267. }
  268. String ScriptServer::get_global_class_path(const String &p_class) {
  269. ERR_FAIL_COND_V(!global_classes.has(p_class), String());
  270. return global_classes[p_class].path;
  271. }
  272. StringName ScriptServer::get_global_class_base(const String &p_class) {
  273. ERR_FAIL_COND_V(!global_classes.has(p_class), String());
  274. return global_classes[p_class].base;
  275. }
  276. StringName ScriptServer::get_global_class_native_base(const String &p_class) {
  277. ERR_FAIL_COND_V(!global_classes.has(p_class), String());
  278. String base = global_classes[p_class].base;
  279. while (global_classes.has(base)) {
  280. base = global_classes[base].base;
  281. }
  282. return base;
  283. }
  284. void ScriptServer::get_global_class_list(List<StringName> *r_global_classes) {
  285. List<StringName> classes;
  286. for (const KeyValue<StringName, GlobalScriptClass> &E : global_classes) {
  287. classes.push_back(E.key);
  288. }
  289. classes.sort_custom<StringName::AlphCompare>();
  290. for (const StringName &E : classes) {
  291. r_global_classes->push_back(E);
  292. }
  293. }
  294. void ScriptServer::save_global_classes() {
  295. Dictionary class_icons;
  296. Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
  297. for (int i = 0; i < script_classes.size(); i++) {
  298. Dictionary d = script_classes[i];
  299. if (!d.has("name") || !d.has("icon")) {
  300. continue;
  301. }
  302. class_icons[d["name"]] = d["icon"];
  303. }
  304. List<StringName> gc;
  305. get_global_class_list(&gc);
  306. Array gcarr;
  307. for (const StringName &E : gc) {
  308. Dictionary d;
  309. d["class"] = E;
  310. d["language"] = global_classes[E].language;
  311. d["path"] = global_classes[E].path;
  312. d["base"] = global_classes[E].base;
  313. d["icon"] = class_icons.get(E, "");
  314. gcarr.push_back(d);
  315. }
  316. ProjectSettings::get_singleton()->store_global_class_list(gcarr);
  317. }
  318. ////////////////////
  319. Variant ScriptInstance::call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  320. return callp(p_method, p_args, p_argcount, r_error);
  321. }
  322. void ScriptInstance::get_property_state(List<Pair<StringName, Variant>> &state) {
  323. List<PropertyInfo> pinfo;
  324. get_property_list(&pinfo);
  325. for (const PropertyInfo &E : pinfo) {
  326. if (E.usage & PROPERTY_USAGE_STORAGE) {
  327. Pair<StringName, Variant> p;
  328. p.first = E.name;
  329. if (get(p.first, p.second)) {
  330. state.push_back(p);
  331. }
  332. }
  333. }
  334. }
  335. void ScriptInstance::property_set_fallback(const StringName &, const Variant &, bool *r_valid) {
  336. if (r_valid) {
  337. *r_valid = false;
  338. }
  339. }
  340. Variant ScriptInstance::property_get_fallback(const StringName &, bool *r_valid) {
  341. if (r_valid) {
  342. *r_valid = false;
  343. }
  344. return Variant();
  345. }
  346. ScriptInstance::~ScriptInstance() {
  347. }
  348. ScriptCodeCompletionCache *ScriptCodeCompletionCache::singleton = nullptr;
  349. ScriptCodeCompletionCache::ScriptCodeCompletionCache() {
  350. singleton = this;
  351. }
  352. void ScriptLanguage::get_core_type_words(List<String> *p_core_type_words) const {
  353. p_core_type_words->push_back("String");
  354. p_core_type_words->push_back("Vector2");
  355. p_core_type_words->push_back("Vector2i");
  356. p_core_type_words->push_back("Rect2");
  357. p_core_type_words->push_back("Rect2i");
  358. p_core_type_words->push_back("Vector3");
  359. p_core_type_words->push_back("Vector3i");
  360. p_core_type_words->push_back("Transform2D");
  361. p_core_type_words->push_back("Vector4");
  362. p_core_type_words->push_back("Vector4i");
  363. p_core_type_words->push_back("Plane");
  364. p_core_type_words->push_back("Quaternion");
  365. p_core_type_words->push_back("AABB");
  366. p_core_type_words->push_back("Basis");
  367. p_core_type_words->push_back("Transform3D");
  368. p_core_type_words->push_back("Projection");
  369. p_core_type_words->push_back("Color");
  370. p_core_type_words->push_back("StringName");
  371. p_core_type_words->push_back("NodePath");
  372. p_core_type_words->push_back("RID");
  373. p_core_type_words->push_back("Callable");
  374. p_core_type_words->push_back("Signal");
  375. p_core_type_words->push_back("Dictionary");
  376. p_core_type_words->push_back("Array");
  377. p_core_type_words->push_back("PackedByteArray");
  378. p_core_type_words->push_back("PackedInt32Array");
  379. p_core_type_words->push_back("PackedInt64Array");
  380. p_core_type_words->push_back("PackedFloat32Array");
  381. p_core_type_words->push_back("PackedFloat64Array");
  382. p_core_type_words->push_back("PackedStringArray");
  383. p_core_type_words->push_back("PackedVector2Array");
  384. p_core_type_words->push_back("PackedVector3Array");
  385. p_core_type_words->push_back("PackedColorArray");
  386. }
  387. void ScriptLanguage::frame() {
  388. }
  389. bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  390. if (script->is_placeholder_fallback_enabled()) {
  391. return false;
  392. }
  393. if (values.has(p_name)) {
  394. Variant defval;
  395. if (script->get_property_default_value(p_name, defval)) {
  396. // The evaluate function ensures that a NIL variant is equal to e.g. an empty Resource.
  397. // Simply doing defval == p_value does not do this.
  398. if (Variant::evaluate(Variant::OP_EQUAL, defval, p_value)) {
  399. values.erase(p_name);
  400. return true;
  401. }
  402. }
  403. values[p_name] = p_value;
  404. return true;
  405. } else {
  406. Variant defval;
  407. if (script->get_property_default_value(p_name, defval)) {
  408. if (Variant::evaluate(Variant::OP_NOT_EQUAL, defval, p_value)) {
  409. values[p_name] = p_value;
  410. }
  411. return true;
  412. }
  413. }
  414. return false;
  415. }
  416. bool PlaceHolderScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  417. if (values.has(p_name)) {
  418. r_ret = values[p_name];
  419. return true;
  420. }
  421. if (constants.has(p_name)) {
  422. r_ret = constants[p_name];
  423. return true;
  424. }
  425. if (!script->is_placeholder_fallback_enabled()) {
  426. Variant defval;
  427. if (script->get_property_default_value(p_name, defval)) {
  428. r_ret = defval;
  429. return true;
  430. }
  431. }
  432. return false;
  433. }
  434. void PlaceHolderScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  435. if (script->is_placeholder_fallback_enabled()) {
  436. for (const PropertyInfo &E : properties) {
  437. p_properties->push_back(E);
  438. }
  439. } else {
  440. for (const PropertyInfo &E : properties) {
  441. PropertyInfo pinfo = E;
  442. if (!values.has(pinfo.name)) {
  443. pinfo.usage |= PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE;
  444. }
  445. p_properties->push_back(E);
  446. }
  447. }
  448. }
  449. Variant::Type PlaceHolderScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  450. if (values.has(p_name)) {
  451. if (r_is_valid) {
  452. *r_is_valid = true;
  453. }
  454. return values[p_name].get_type();
  455. }
  456. if (constants.has(p_name)) {
  457. if (r_is_valid) {
  458. *r_is_valid = true;
  459. }
  460. return constants[p_name].get_type();
  461. }
  462. if (r_is_valid) {
  463. *r_is_valid = false;
  464. }
  465. return Variant::NIL;
  466. }
  467. void PlaceHolderScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  468. if (script->is_placeholder_fallback_enabled()) {
  469. return;
  470. }
  471. if (script.is_valid()) {
  472. script->get_script_method_list(p_list);
  473. }
  474. }
  475. bool PlaceHolderScriptInstance::has_method(const StringName &p_method) const {
  476. if (script->is_placeholder_fallback_enabled()) {
  477. return false;
  478. }
  479. if (script.is_valid()) {
  480. return script->has_method(p_method);
  481. }
  482. return false;
  483. }
  484. void PlaceHolderScriptInstance::update(const List<PropertyInfo> &p_properties, const HashMap<StringName, Variant> &p_values) {
  485. HashSet<StringName> new_values;
  486. for (const PropertyInfo &E : p_properties) {
  487. StringName n = E.name;
  488. new_values.insert(n);
  489. if (!values.has(n) || values[n].get_type() != E.type) {
  490. if (p_values.has(n)) {
  491. values[n] = p_values[n];
  492. }
  493. }
  494. }
  495. properties = p_properties;
  496. List<StringName> to_remove;
  497. for (KeyValue<StringName, Variant> &E : values) {
  498. if (!new_values.has(E.key)) {
  499. to_remove.push_back(E.key);
  500. }
  501. Variant defval;
  502. if (script->get_property_default_value(E.key, defval)) {
  503. //remove because it's the same as the default value
  504. if (defval == E.value) {
  505. to_remove.push_back(E.key);
  506. }
  507. }
  508. }
  509. while (to_remove.size()) {
  510. values.erase(to_remove.front()->get());
  511. to_remove.pop_front();
  512. }
  513. if (owner && owner->get_script_instance() == this) {
  514. owner->notify_property_list_changed();
  515. }
  516. //change notify
  517. constants.clear();
  518. script->get_constants(&constants);
  519. }
  520. void PlaceHolderScriptInstance::property_set_fallback(const StringName &p_name, const Variant &p_value, bool *r_valid) {
  521. if (script->is_placeholder_fallback_enabled()) {
  522. HashMap<StringName, Variant>::Iterator E = values.find(p_name);
  523. if (E) {
  524. E->value = p_value;
  525. } else {
  526. values.insert(p_name, p_value);
  527. }
  528. bool found = false;
  529. for (const PropertyInfo &F : properties) {
  530. if (F.name == p_name) {
  531. found = true;
  532. break;
  533. }
  534. }
  535. if (!found) {
  536. properties.push_back(PropertyInfo(p_value.get_type(), p_name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_SCRIPT_VARIABLE));
  537. }
  538. }
  539. if (r_valid) {
  540. *r_valid = false; // Cannot change the value in either case
  541. }
  542. }
  543. Variant PlaceHolderScriptInstance::property_get_fallback(const StringName &p_name, bool *r_valid) {
  544. if (script->is_placeholder_fallback_enabled()) {
  545. HashMap<StringName, Variant>::ConstIterator E = values.find(p_name);
  546. if (E) {
  547. if (r_valid) {
  548. *r_valid = true;
  549. }
  550. return E->value;
  551. }
  552. E = constants.find(p_name);
  553. if (E) {
  554. if (r_valid) {
  555. *r_valid = true;
  556. }
  557. return E->value;
  558. }
  559. }
  560. if (r_valid) {
  561. *r_valid = false;
  562. }
  563. return Variant();
  564. }
  565. PlaceHolderScriptInstance::PlaceHolderScriptInstance(ScriptLanguage *p_language, Ref<Script> p_script, Object *p_owner) :
  566. owner(p_owner),
  567. language(p_language),
  568. script(p_script) {
  569. }
  570. PlaceHolderScriptInstance::~PlaceHolderScriptInstance() {
  571. if (script.is_valid()) {
  572. script->_placeholder_erased(this);
  573. }
  574. }