2
0

visual_script_func_nodes.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  1. /*************************************************************************/
  2. /* visual_script_func_nodes.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "visual_script_func_nodes.h"
  31. #include "core/config/engine.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/os/os.h"
  34. #include "scene/main/node.h"
  35. #include "scene/main/scene_tree.h"
  36. #include "visual_script_nodes.h"
  37. //////////////////////////////////////////
  38. ////////////////CALL//////////////////////
  39. //////////////////////////////////////////
  40. int VisualScriptFunctionCall::get_output_sequence_port_count() const {
  41. if ((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_builtin_method_const(basic_type, function))) {
  42. return 0;
  43. } else {
  44. return 1;
  45. }
  46. }
  47. bool VisualScriptFunctionCall::has_input_sequence_port() const {
  48. return !((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_builtin_method_const(basic_type, function)));
  49. }
  50. #ifdef TOOLS_ENABLED
  51. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  52. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene) {
  53. return nullptr;
  54. }
  55. Ref<Script> scr = p_current_node->get_script();
  56. if (scr.is_valid() && scr == script) {
  57. return p_current_node;
  58. }
  59. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  60. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  61. if (n) {
  62. return n;
  63. }
  64. }
  65. return nullptr;
  66. }
  67. #endif
  68. Node *VisualScriptFunctionCall::_get_base_node() const {
  69. #ifdef TOOLS_ENABLED
  70. Ref<Script> script = get_visual_script();
  71. if (!script.is_valid()) {
  72. return nullptr;
  73. }
  74. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  75. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  76. if (!scene_tree) {
  77. return nullptr;
  78. }
  79. Node *edited_scene = scene_tree->get_edited_scene_root();
  80. if (!edited_scene) {
  81. return nullptr;
  82. }
  83. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  84. if (!script_node) {
  85. return nullptr;
  86. }
  87. if (!script_node->has_node(base_path)) {
  88. return nullptr;
  89. }
  90. Node *path_to = script_node->get_node(base_path);
  91. return path_to;
  92. #else
  93. return nullptr;
  94. #endif
  95. }
  96. StringName VisualScriptFunctionCall::_get_base_type() const {
  97. if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  98. return get_visual_script()->get_instance_base_type();
  99. } else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  100. Node *path = _get_base_node();
  101. if (path) {
  102. return path->get_class();
  103. }
  104. }
  105. return base_type;
  106. }
  107. int VisualScriptFunctionCall::get_input_value_port_count() const {
  108. if (call_mode == CALL_MODE_BASIC_TYPE) {
  109. Vector<Variant::Type> types;
  110. int argc = Variant::get_builtin_method_argument_count(basic_type, function);
  111. for (int i = 0; i < argc; i++) {
  112. types.push_back(Variant::get_builtin_method_argument_type(basic_type, function, i));
  113. }
  114. return types.size() + (rpc_call_mode >= RPC_RELIABLE_TO_ID ? 1 : 0) + 1;
  115. } else {
  116. MethodBind *mb = ClassDB::get_method(_get_base_type(), function);
  117. if (mb) {
  118. int defaulted_args = mb->get_argument_count() < use_default_args ? mb->get_argument_count() : use_default_args;
  119. return mb->get_argument_count() + (call_mode == CALL_MODE_INSTANCE ? 1 : 0) + (rpc_call_mode >= RPC_RELIABLE_TO_ID ? 1 : 0) - defaulted_args;
  120. }
  121. int defaulted_args = method_cache.arguments.size() < use_default_args ? method_cache.arguments.size() : use_default_args;
  122. return method_cache.arguments.size() + (call_mode == CALL_MODE_INSTANCE ? 1 : 0) + (rpc_call_mode >= RPC_RELIABLE_TO_ID ? 1 : 0) - defaulted_args;
  123. }
  124. }
  125. int VisualScriptFunctionCall::get_output_value_port_count() const {
  126. if (call_mode == CALL_MODE_BASIC_TYPE) {
  127. bool returns = Variant::has_builtin_method_return_value(basic_type, function);
  128. return returns ? 1 : 0;
  129. } else {
  130. int ret;
  131. MethodBind *mb = ClassDB::get_method(_get_base_type(), function);
  132. if (mb) {
  133. ret = mb->has_return() ? 1 : 0;
  134. } else {
  135. ret = 1; //it is assumed that script always returns something
  136. }
  137. if (call_mode == CALL_MODE_INSTANCE) {
  138. ret++;
  139. }
  140. return ret;
  141. }
  142. }
  143. String VisualScriptFunctionCall::get_output_sequence_port_text(int p_port) const {
  144. return String();
  145. }
  146. PropertyInfo VisualScriptFunctionCall::get_input_value_port_info(int p_idx) const {
  147. if (call_mode == CALL_MODE_INSTANCE || call_mode == CALL_MODE_BASIC_TYPE) {
  148. if (p_idx == 0) {
  149. PropertyInfo pi;
  150. pi.type = (call_mode == CALL_MODE_INSTANCE ? Variant::OBJECT : basic_type);
  151. pi.name = (call_mode == CALL_MODE_INSTANCE ? String("instance") : Variant::get_type_name(basic_type).to_lower());
  152. return pi;
  153. } else {
  154. p_idx--;
  155. }
  156. }
  157. if (rpc_call_mode >= RPC_RELIABLE_TO_ID) {
  158. if (p_idx == 0) {
  159. return PropertyInfo(Variant::INT, "peer_id");
  160. } else {
  161. p_idx--;
  162. }
  163. }
  164. #ifdef DEBUG_METHODS_ENABLED
  165. if (call_mode == CALL_MODE_BASIC_TYPE) {
  166. return PropertyInfo(Variant::get_builtin_method_argument_type(basic_type, function, p_idx), Variant::get_builtin_method_argument_name(basic_type, function, p_idx));
  167. } else {
  168. MethodBind *mb = ClassDB::get_method(_get_base_type(), function);
  169. if (mb) {
  170. return mb->get_argument_info(p_idx);
  171. }
  172. if (p_idx >= 0 && p_idx < method_cache.arguments.size()) {
  173. return method_cache.arguments[p_idx];
  174. }
  175. return PropertyInfo();
  176. }
  177. #else
  178. return PropertyInfo();
  179. #endif
  180. }
  181. PropertyInfo VisualScriptFunctionCall::get_output_value_port_info(int p_idx) const {
  182. #ifdef DEBUG_METHODS_ENABLED
  183. if (call_mode == CALL_MODE_BASIC_TYPE) {
  184. return PropertyInfo(Variant::get_builtin_method_return_type(basic_type, function), "");
  185. } else {
  186. if (call_mode == CALL_MODE_INSTANCE) {
  187. if (p_idx == 0) {
  188. return PropertyInfo(Variant::OBJECT, "pass", PROPERTY_HINT_TYPE_STRING, get_base_type());
  189. } else {
  190. p_idx--;
  191. }
  192. }
  193. PropertyInfo ret;
  194. /*MethodBind *mb = ClassDB::get_method(_get_base_type(),function);
  195. if (mb) {
  196. ret = mb->get_argument_info(-1);
  197. } else {*/
  198. ret = method_cache.return_val;
  199. //}
  200. if (call_mode == CALL_MODE_INSTANCE) {
  201. ret.name = "return";
  202. } else {
  203. ret.name = "";
  204. }
  205. return ret;
  206. }
  207. #else
  208. return PropertyInfo();
  209. #endif
  210. }
  211. String VisualScriptFunctionCall::get_caption() const {
  212. return " " + String(function) + "()";
  213. }
  214. String VisualScriptFunctionCall::get_text() const {
  215. String text;
  216. if (call_mode == CALL_MODE_BASIC_TYPE) {
  217. text = vformat(TTR("On %s"), Variant::get_type_name(basic_type));
  218. } else if (call_mode == CALL_MODE_INSTANCE) {
  219. text = vformat(TTR("On %s"), base_type);
  220. } else if (call_mode == CALL_MODE_NODE_PATH) {
  221. text = "[" + String(base_path.simplified()) + "]";
  222. } else if (call_mode == CALL_MODE_SELF) {
  223. text = TTR("On Self");
  224. } else if (call_mode == CALL_MODE_SINGLETON) {
  225. text = String(singleton) + ":" + String(function) + "()";
  226. }
  227. if (rpc_call_mode) {
  228. text += " RPC";
  229. if (rpc_call_mode == RPC_UNRELIABLE || rpc_call_mode == RPC_UNRELIABLE_TO_ID) {
  230. text += " UNREL";
  231. }
  232. }
  233. return text;
  234. }
  235. void VisualScriptFunctionCall::set_basic_type(Variant::Type p_type) {
  236. if (basic_type == p_type) {
  237. return;
  238. }
  239. basic_type = p_type;
  240. notify_property_list_changed();
  241. ports_changed_notify();
  242. }
  243. Variant::Type VisualScriptFunctionCall::get_basic_type() const {
  244. return basic_type;
  245. }
  246. void VisualScriptFunctionCall::set_base_type(const StringName &p_type) {
  247. if (base_type == p_type) {
  248. return;
  249. }
  250. base_type = p_type;
  251. notify_property_list_changed();
  252. ports_changed_notify();
  253. }
  254. StringName VisualScriptFunctionCall::get_base_type() const {
  255. return base_type;
  256. }
  257. void VisualScriptFunctionCall::set_base_script(const String &p_path) {
  258. if (base_script == p_path) {
  259. return;
  260. }
  261. base_script = p_path;
  262. notify_property_list_changed();
  263. ports_changed_notify();
  264. }
  265. String VisualScriptFunctionCall::get_base_script() const {
  266. return base_script;
  267. }
  268. void VisualScriptFunctionCall::set_singleton(const StringName &p_type) {
  269. if (singleton == p_type) {
  270. return;
  271. }
  272. singleton = p_type;
  273. Object *obj = Engine::get_singleton()->get_singleton_object(singleton);
  274. if (obj) {
  275. base_type = obj->get_class();
  276. }
  277. notify_property_list_changed();
  278. ports_changed_notify();
  279. }
  280. StringName VisualScriptFunctionCall::get_singleton() const {
  281. return singleton;
  282. }
  283. void VisualScriptFunctionCall::_update_method_cache() {
  284. StringName type;
  285. Ref<Script> script;
  286. if (call_mode == CALL_MODE_NODE_PATH) {
  287. Node *node = _get_base_node();
  288. if (node) {
  289. type = node->get_class();
  290. base_type = type; //cache, too
  291. script = node->get_script();
  292. }
  293. } else if (call_mode == CALL_MODE_SELF) {
  294. if (get_visual_script().is_valid()) {
  295. type = get_visual_script()->get_instance_base_type();
  296. base_type = type; //cache, too
  297. script = get_visual_script();
  298. }
  299. } else if (call_mode == CALL_MODE_SINGLETON) {
  300. Object *obj = Engine::get_singleton()->get_singleton_object(singleton);
  301. if (obj) {
  302. type = obj->get_class();
  303. script = obj->get_script();
  304. }
  305. } else if (call_mode == CALL_MODE_INSTANCE) {
  306. type = base_type;
  307. if (!base_script.is_empty()) {
  308. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  309. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  310. }
  311. if (ResourceCache::has(base_script)) {
  312. script = Ref<Resource>(ResourceCache::get(base_script));
  313. } else {
  314. return;
  315. }
  316. }
  317. }
  318. MethodBind *mb = ClassDB::get_method(type, function);
  319. if (mb) {
  320. use_default_args = mb->get_default_argument_count();
  321. method_cache = MethodInfo();
  322. for (int i = 0; i < mb->get_argument_count(); i++) {
  323. #ifdef DEBUG_METHODS_ENABLED
  324. method_cache.arguments.push_back(mb->get_argument_info(i));
  325. #else
  326. method_cache.arguments.push_back(PropertyInfo());
  327. #endif
  328. }
  329. if (mb->is_const()) {
  330. method_cache.flags |= METHOD_FLAG_CONST;
  331. }
  332. #ifdef DEBUG_METHODS_ENABLED
  333. method_cache.return_val = mb->get_return_info();
  334. #endif
  335. if (mb->is_vararg()) {
  336. //for vararg just give it 10 arguments (should be enough for most use cases)
  337. for (int i = 0; i < 10; i++) {
  338. method_cache.arguments.push_back(PropertyInfo(Variant::NIL, "arg" + itos(i)));
  339. use_default_args++;
  340. }
  341. }
  342. } else if (script.is_valid() && script->has_method(function)) {
  343. method_cache = script->get_method_info(function);
  344. use_default_args = method_cache.default_arguments.size();
  345. }
  346. }
  347. void VisualScriptFunctionCall::set_function(const StringName &p_type) {
  348. if (function == p_type) {
  349. return;
  350. }
  351. function = p_type;
  352. if (call_mode == CALL_MODE_BASIC_TYPE) {
  353. use_default_args = Variant::get_builtin_method_default_arguments(basic_type, function).size();
  354. } else {
  355. //update all caches
  356. _update_method_cache();
  357. }
  358. notify_property_list_changed();
  359. ports_changed_notify();
  360. }
  361. StringName VisualScriptFunctionCall::get_function() const {
  362. return function;
  363. }
  364. void VisualScriptFunctionCall::set_base_path(const NodePath &p_type) {
  365. if (base_path == p_type) {
  366. return;
  367. }
  368. base_path = p_type;
  369. notify_property_list_changed();
  370. ports_changed_notify();
  371. }
  372. NodePath VisualScriptFunctionCall::get_base_path() const {
  373. return base_path;
  374. }
  375. void VisualScriptFunctionCall::set_call_mode(CallMode p_mode) {
  376. if (call_mode == p_mode) {
  377. return;
  378. }
  379. call_mode = p_mode;
  380. notify_property_list_changed();
  381. ports_changed_notify();
  382. }
  383. VisualScriptFunctionCall::CallMode VisualScriptFunctionCall::get_call_mode() const {
  384. return call_mode;
  385. }
  386. void VisualScriptFunctionCall::set_use_default_args(int p_amount) {
  387. if (use_default_args == p_amount) {
  388. return;
  389. }
  390. use_default_args = p_amount;
  391. ports_changed_notify();
  392. }
  393. void VisualScriptFunctionCall::set_rpc_call_mode(VisualScriptFunctionCall::RPCCallMode p_mode) {
  394. if (rpc_call_mode == p_mode) {
  395. return;
  396. }
  397. rpc_call_mode = p_mode;
  398. ports_changed_notify();
  399. notify_property_list_changed();
  400. }
  401. VisualScriptFunctionCall::RPCCallMode VisualScriptFunctionCall::get_rpc_call_mode() const {
  402. return rpc_call_mode;
  403. }
  404. int VisualScriptFunctionCall::get_use_default_args() const {
  405. return use_default_args;
  406. }
  407. void VisualScriptFunctionCall::set_validate(bool p_amount) {
  408. validate = p_amount;
  409. }
  410. bool VisualScriptFunctionCall::get_validate() const {
  411. return validate;
  412. }
  413. void VisualScriptFunctionCall::_set_argument_cache(const Dictionary &p_cache) {
  414. //so everything works in case all else fails
  415. method_cache = MethodInfo::from_dict(p_cache);
  416. }
  417. Dictionary VisualScriptFunctionCall::_get_argument_cache() const {
  418. return method_cache;
  419. }
  420. void VisualScriptFunctionCall::_validate_property(PropertyInfo &property) const {
  421. if (property.name == "base_type") {
  422. if (call_mode != CALL_MODE_INSTANCE) {
  423. property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  424. }
  425. }
  426. if (property.name == "base_script") {
  427. if (call_mode != CALL_MODE_INSTANCE) {
  428. property.usage = PROPERTY_USAGE_NONE;
  429. }
  430. }
  431. if (property.name == "basic_type") {
  432. if (call_mode != CALL_MODE_BASIC_TYPE) {
  433. property.usage = PROPERTY_USAGE_NONE;
  434. }
  435. }
  436. if (property.name == "singleton") {
  437. if (call_mode != CALL_MODE_SINGLETON) {
  438. property.usage = PROPERTY_USAGE_NONE;
  439. } else {
  440. List<Engine::Singleton> names;
  441. Engine::get_singleton()->get_singletons(&names);
  442. property.hint = PROPERTY_HINT_ENUM;
  443. String sl;
  444. for (const Engine::Singleton &E : names) {
  445. if (!sl.is_empty()) {
  446. sl += ",";
  447. }
  448. sl += E.name;
  449. }
  450. property.hint_string = sl;
  451. }
  452. }
  453. if (property.name == "node_path") {
  454. if (call_mode != CALL_MODE_NODE_PATH) {
  455. property.usage = PROPERTY_USAGE_NONE;
  456. } else {
  457. Node *bnode = _get_base_node();
  458. if (bnode) {
  459. property.hint_string = bnode->get_path(); //convert to long string
  460. }
  461. }
  462. }
  463. if (property.name == "function") {
  464. if (call_mode == CALL_MODE_BASIC_TYPE) {
  465. property.hint = PROPERTY_HINT_METHOD_OF_VARIANT_TYPE;
  466. property.hint_string = Variant::get_type_name(basic_type);
  467. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  468. property.hint = PROPERTY_HINT_METHOD_OF_SCRIPT;
  469. property.hint_string = itos(get_visual_script()->get_instance_id());
  470. } else if (call_mode == CALL_MODE_SINGLETON) {
  471. Object *obj = Engine::get_singleton()->get_singleton_object(singleton);
  472. if (obj) {
  473. property.hint = PROPERTY_HINT_METHOD_OF_INSTANCE;
  474. property.hint_string = itos(obj->get_instance_id());
  475. } else {
  476. property.hint = PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  477. property.hint_string = base_type; //should be cached
  478. }
  479. } else if (call_mode == CALL_MODE_INSTANCE) {
  480. property.hint = PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  481. property.hint_string = base_type;
  482. if (!base_script.is_empty()) {
  483. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  484. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  485. }
  486. if (ResourceCache::has(base_script)) {
  487. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  488. if (script.is_valid()) {
  489. property.hint = PROPERTY_HINT_METHOD_OF_SCRIPT;
  490. property.hint_string = itos(script->get_instance_id());
  491. }
  492. }
  493. }
  494. } else if (call_mode == CALL_MODE_NODE_PATH) {
  495. Node *node = _get_base_node();
  496. if (node) {
  497. property.hint = PROPERTY_HINT_METHOD_OF_INSTANCE;
  498. property.hint_string = itos(node->get_instance_id());
  499. } else {
  500. property.hint = PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  501. property.hint_string = get_base_type();
  502. }
  503. }
  504. }
  505. if (property.name == "use_default_args") {
  506. property.hint = PROPERTY_HINT_RANGE;
  507. int mc = 0;
  508. if (call_mode == CALL_MODE_BASIC_TYPE) {
  509. mc = Variant::get_builtin_method_default_arguments(basic_type, function).size();
  510. } else {
  511. MethodBind *mb = ClassDB::get_method(_get_base_type(), function);
  512. if (mb) {
  513. mc = mb->get_default_argument_count();
  514. }
  515. }
  516. if (mc == 0) {
  517. property.usage = PROPERTY_USAGE_NONE; //do not show
  518. } else {
  519. property.hint_string = "0," + itos(mc) + ",1";
  520. }
  521. }
  522. if (property.name == "rpc_call_mode") {
  523. if (call_mode == CALL_MODE_BASIC_TYPE) {
  524. property.usage = PROPERTY_USAGE_NONE;
  525. }
  526. }
  527. }
  528. void VisualScriptFunctionCall::_bind_methods() {
  529. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptFunctionCall::set_base_type);
  530. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptFunctionCall::get_base_type);
  531. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptFunctionCall::set_base_script);
  532. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptFunctionCall::get_base_script);
  533. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptFunctionCall::set_basic_type);
  534. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptFunctionCall::get_basic_type);
  535. ClassDB::bind_method(D_METHOD("set_singleton", "singleton"), &VisualScriptFunctionCall::set_singleton);
  536. ClassDB::bind_method(D_METHOD("get_singleton"), &VisualScriptFunctionCall::get_singleton);
  537. ClassDB::bind_method(D_METHOD("set_function", "function"), &VisualScriptFunctionCall::set_function);
  538. ClassDB::bind_method(D_METHOD("get_function"), &VisualScriptFunctionCall::get_function);
  539. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptFunctionCall::set_call_mode);
  540. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptFunctionCall::get_call_mode);
  541. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptFunctionCall::set_base_path);
  542. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptFunctionCall::get_base_path);
  543. ClassDB::bind_method(D_METHOD("set_use_default_args", "amount"), &VisualScriptFunctionCall::set_use_default_args);
  544. ClassDB::bind_method(D_METHOD("get_use_default_args"), &VisualScriptFunctionCall::get_use_default_args);
  545. ClassDB::bind_method(D_METHOD("_set_argument_cache", "argument_cache"), &VisualScriptFunctionCall::_set_argument_cache);
  546. ClassDB::bind_method(D_METHOD("_get_argument_cache"), &VisualScriptFunctionCall::_get_argument_cache);
  547. ClassDB::bind_method(D_METHOD("set_rpc_call_mode", "mode"), &VisualScriptFunctionCall::set_rpc_call_mode);
  548. ClassDB::bind_method(D_METHOD("get_rpc_call_mode"), &VisualScriptFunctionCall::get_rpc_call_mode);
  549. ClassDB::bind_method(D_METHOD("set_validate", "enable"), &VisualScriptFunctionCall::set_validate);
  550. ClassDB::bind_method(D_METHOD("get_validate"), &VisualScriptFunctionCall::get_validate);
  551. String bt;
  552. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  553. if (i > 0) {
  554. bt += ",";
  555. }
  556. bt += Variant::get_type_name(Variant::Type(i));
  557. }
  558. List<String> script_extensions;
  559. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  560. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  561. }
  562. String script_ext_hint;
  563. for (const String &E : script_extensions) {
  564. if (!script_ext_hint.is_empty()) {
  565. script_ext_hint += ",";
  566. }
  567. script_ext_hint += "*." + E;
  568. }
  569. ADD_PROPERTY(PropertyInfo(Variant::INT, "call_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type,Singleton"), "set_call_mode", "get_call_mode");
  570. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  571. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  572. ADD_PROPERTY(PropertyInfo(Variant::STRING, "singleton"), "set_singleton", "get_singleton");
  573. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  574. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  575. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "argument_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_argument_cache", "_get_argument_cache");
  576. ADD_PROPERTY(PropertyInfo(Variant::STRING, "function"), "set_function", "get_function"); //when set, if loaded properly, will override argument count.
  577. ADD_PROPERTY(PropertyInfo(Variant::INT, "use_default_args"), "set_use_default_args", "get_use_default_args");
  578. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "validate"), "set_validate", "get_validate");
  579. ADD_PROPERTY(PropertyInfo(Variant::INT, "rpc_call_mode", PROPERTY_HINT_ENUM, "Disabled,Reliable,Unreliable,ReliableToID,UnreliableToID"), "set_rpc_call_mode", "get_rpc_call_mode"); //when set, if loaded properly, will override argument count.
  580. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  581. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  582. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  583. BIND_ENUM_CONSTANT(CALL_MODE_BASIC_TYPE);
  584. BIND_ENUM_CONSTANT(CALL_MODE_SINGLETON);
  585. BIND_ENUM_CONSTANT(RPC_DISABLED);
  586. BIND_ENUM_CONSTANT(RPC_RELIABLE);
  587. BIND_ENUM_CONSTANT(RPC_UNRELIABLE);
  588. BIND_ENUM_CONSTANT(RPC_RELIABLE_TO_ID);
  589. BIND_ENUM_CONSTANT(RPC_UNRELIABLE_TO_ID);
  590. }
  591. class VisualScriptNodeInstanceFunctionCall : public VisualScriptNodeInstance {
  592. public:
  593. VisualScriptFunctionCall::CallMode call_mode;
  594. NodePath node_path;
  595. int input_args;
  596. bool validate;
  597. int returns;
  598. VisualScriptFunctionCall::RPCCallMode rpc_mode;
  599. StringName function;
  600. StringName singleton;
  601. VisualScriptFunctionCall *node;
  602. VisualScriptInstance *instance;
  603. //virtual int get_working_memory_size() const { return 0; }
  604. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  605. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  606. _FORCE_INLINE_ bool call_rpc(Object *p_base, const Variant **p_args, int p_argcount) {
  607. if (!p_base) {
  608. return false;
  609. }
  610. Node *node = Object::cast_to<Node>(p_base);
  611. if (!node) {
  612. return false;
  613. }
  614. int to_id = 0;
  615. //bool reliable = true;
  616. if (rpc_mode >= VisualScriptFunctionCall::RPC_RELIABLE_TO_ID) {
  617. to_id = *p_args[0];
  618. p_args += 1;
  619. p_argcount -= 1;
  620. //if (rpc_mode == VisualScriptFunctionCall::RPC_UNRELIABLE_TO_ID) {
  621. //reliable = false;
  622. //}
  623. }
  624. //else if (rpc_mode == VisualScriptFunctionCall::RPC_UNRELIABLE) {
  625. //reliable = false;
  626. //}
  627. // TODO reliable?
  628. node->rpcp(to_id, function, p_args, p_argcount);
  629. return true;
  630. }
  631. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Callable::CallError &r_error, String &r_error_str) {
  632. switch (call_mode) {
  633. case VisualScriptFunctionCall::CALL_MODE_SELF: {
  634. Object *object = instance->get_owner_ptr();
  635. if (rpc_mode) {
  636. call_rpc(object, p_inputs, input_args);
  637. } else if (returns) {
  638. *p_outputs[0] = object->call(function, p_inputs, input_args, r_error);
  639. } else {
  640. object->call(function, p_inputs, input_args, r_error);
  641. }
  642. } break;
  643. case VisualScriptFunctionCall::CALL_MODE_NODE_PATH: {
  644. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  645. if (!node) {
  646. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  647. r_error_str = "Base object is not a Node!";
  648. return 0;
  649. }
  650. Node *another = node->get_node(node_path);
  651. if (!another) {
  652. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  653. r_error_str = "Path does not lead Node!";
  654. return 0;
  655. }
  656. if (rpc_mode) {
  657. call_rpc(node, p_inputs, input_args);
  658. } else if (returns) {
  659. *p_outputs[0] = another->call(function, p_inputs, input_args, r_error);
  660. } else {
  661. another->call(function, p_inputs, input_args, r_error);
  662. }
  663. } break;
  664. case VisualScriptFunctionCall::CALL_MODE_INSTANCE:
  665. case VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE: {
  666. Variant v = *p_inputs[0];
  667. if (rpc_mode) {
  668. Object *obj = v;
  669. if (obj) {
  670. call_rpc(obj, p_inputs + 1, input_args - 1);
  671. }
  672. } else if (returns) {
  673. if (call_mode == VisualScriptFunctionCall::CALL_MODE_INSTANCE) {
  674. if (returns >= 2) {
  675. v.call(function, p_inputs + 1, input_args, *p_outputs[1], r_error);
  676. } else if (returns == 1) {
  677. Variant ret;
  678. v.call(function, p_inputs + 1, input_args, ret, r_error);
  679. } else {
  680. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  681. r_error_str = "Invalid returns count for call_mode == CALL_MODE_INSTANCE";
  682. return 0;
  683. }
  684. } else {
  685. v.call(function, p_inputs + 1, input_args, *p_outputs[0], r_error);
  686. }
  687. } else {
  688. Variant ret;
  689. v.call(function, p_inputs + 1, input_args, ret, r_error);
  690. }
  691. if (call_mode == VisualScriptFunctionCall::CALL_MODE_INSTANCE) {
  692. *p_outputs[0] = *p_inputs[0];
  693. }
  694. } break;
  695. case VisualScriptFunctionCall::CALL_MODE_SINGLETON: {
  696. Object *object = Engine::get_singleton()->get_singleton_object(singleton);
  697. if (!object) {
  698. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  699. r_error_str = "Invalid singleton name: '" + String(singleton) + "'";
  700. return 0;
  701. }
  702. if (rpc_mode) {
  703. call_rpc(object, p_inputs, input_args);
  704. } else if (returns) {
  705. *p_outputs[0] = object->call(function, p_inputs, input_args, r_error);
  706. } else {
  707. object->call(function, p_inputs, input_args, r_error);
  708. }
  709. } break;
  710. }
  711. if (!validate) {
  712. //ignore call errors if validation is disabled
  713. r_error.error = Callable::CallError::CALL_OK;
  714. r_error_str = String();
  715. }
  716. return 0;
  717. }
  718. };
  719. VisualScriptNodeInstance *VisualScriptFunctionCall::instantiate(VisualScriptInstance *p_instance) {
  720. VisualScriptNodeInstanceFunctionCall *instance = memnew(VisualScriptNodeInstanceFunctionCall);
  721. instance->node = this;
  722. instance->instance = p_instance;
  723. instance->singleton = singleton;
  724. instance->function = function;
  725. instance->call_mode = call_mode;
  726. instance->returns = get_output_value_port_count();
  727. instance->node_path = base_path;
  728. instance->input_args = get_input_value_port_count() - ((call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 1 : 0);
  729. instance->rpc_mode = rpc_call_mode;
  730. instance->validate = validate;
  731. return instance;
  732. }
  733. VisualScriptFunctionCall::TypeGuess VisualScriptFunctionCall::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  734. if (p_output == 0 && call_mode == CALL_MODE_INSTANCE) {
  735. return p_inputs[0];
  736. }
  737. return VisualScriptNode::guess_output_type(p_inputs, p_output);
  738. }
  739. VisualScriptFunctionCall::VisualScriptFunctionCall() {
  740. validate = true;
  741. call_mode = CALL_MODE_SELF;
  742. basic_type = Variant::NIL;
  743. use_default_args = 0;
  744. base_type = "Object";
  745. rpc_call_mode = RPC_DISABLED;
  746. }
  747. template <VisualScriptFunctionCall::CallMode cmode>
  748. static Ref<VisualScriptNode> create_function_call_node(const String &p_name) {
  749. Ref<VisualScriptFunctionCall> node;
  750. node.instantiate();
  751. node->set_call_mode(cmode);
  752. return node;
  753. }
  754. //////////////////////////////////////////
  755. ////////////////SET//////////////////////
  756. //////////////////////////////////////////
  757. int VisualScriptPropertySet::get_output_sequence_port_count() const {
  758. return 1;
  759. }
  760. bool VisualScriptPropertySet::has_input_sequence_port() const {
  761. return true;
  762. }
  763. Node *VisualScriptPropertySet::_get_base_node() const {
  764. #ifdef TOOLS_ENABLED
  765. Ref<Script> script = get_visual_script();
  766. if (!script.is_valid()) {
  767. return nullptr;
  768. }
  769. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  770. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  771. if (!scene_tree) {
  772. return nullptr;
  773. }
  774. Node *edited_scene = scene_tree->get_edited_scene_root();
  775. if (!edited_scene) {
  776. return nullptr;
  777. }
  778. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  779. if (!script_node) {
  780. return nullptr;
  781. }
  782. if (!script_node->has_node(base_path)) {
  783. return nullptr;
  784. }
  785. Node *path_to = script_node->get_node(base_path);
  786. return path_to;
  787. #else
  788. return nullptr;
  789. #endif
  790. }
  791. StringName VisualScriptPropertySet::_get_base_type() const {
  792. if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  793. return get_visual_script()->get_instance_base_type();
  794. } else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  795. Node *path = _get_base_node();
  796. if (path) {
  797. return path->get_class();
  798. }
  799. }
  800. return base_type;
  801. }
  802. int VisualScriptPropertySet::get_input_value_port_count() const {
  803. int pc = (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 2 : 1;
  804. return pc;
  805. }
  806. int VisualScriptPropertySet::get_output_value_port_count() const {
  807. return (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 1 : 0;
  808. }
  809. String VisualScriptPropertySet::get_output_sequence_port_text(int p_port) const {
  810. return String();
  811. }
  812. void VisualScriptPropertySet::_adjust_input_index(PropertyInfo &pinfo) const {
  813. if (index != StringName()) {
  814. Variant v;
  815. Callable::CallError ce;
  816. Variant::construct(pinfo.type, v, nullptr, 0, ce);
  817. Variant i = v.get(index);
  818. pinfo.type = i.get_type();
  819. }
  820. }
  821. PropertyInfo VisualScriptPropertySet::get_input_value_port_info(int p_idx) const {
  822. if (call_mode == CALL_MODE_INSTANCE || call_mode == CALL_MODE_BASIC_TYPE) {
  823. if (p_idx == 0) {
  824. PropertyInfo pi;
  825. pi.type = (call_mode == CALL_MODE_INSTANCE ? Variant::OBJECT : basic_type);
  826. pi.name = "instance";
  827. return pi;
  828. }
  829. }
  830. List<PropertyInfo> props;
  831. ClassDB::get_property_list(_get_base_type(), &props, false);
  832. for (const PropertyInfo &E : props) {
  833. if (E.name == property) {
  834. String detail_prop_name = property;
  835. if (index != StringName()) {
  836. detail_prop_name += "." + String(index);
  837. }
  838. PropertyInfo pinfo = PropertyInfo(E.type, detail_prop_name, E.hint, E.hint_string);
  839. _adjust_input_index(pinfo);
  840. return pinfo;
  841. }
  842. }
  843. PropertyInfo pinfo = type_cache;
  844. _adjust_input_index(pinfo);
  845. return pinfo;
  846. }
  847. PropertyInfo VisualScriptPropertySet::get_output_value_port_info(int p_idx) const {
  848. if (call_mode == CALL_MODE_BASIC_TYPE) {
  849. return PropertyInfo(basic_type, "pass");
  850. } else if (call_mode == CALL_MODE_INSTANCE) {
  851. return PropertyInfo(Variant::OBJECT, "pass", PROPERTY_HINT_TYPE_STRING, get_base_type());
  852. } else {
  853. return PropertyInfo();
  854. }
  855. }
  856. String VisualScriptPropertySet::get_caption() const {
  857. static const char *opname[ASSIGN_OP_MAX] = {
  858. TTRC("Set %s"),
  859. TTRC("Add %s"),
  860. TTRC("Subtract %s"),
  861. TTRC("Multiply %s"),
  862. TTRC("Divide %s"),
  863. TTRC("Mod %s"),
  864. TTRC("ShiftLeft %s"),
  865. TTRC("ShiftRight %s"),
  866. TTRC("BitAnd %s"),
  867. TTRC("BitOr %s"),
  868. TTRC("BitXor %s")
  869. };
  870. String prop = property;
  871. if (index != StringName()) {
  872. prop += "." + String(index);
  873. }
  874. return vformat(TTRGET(opname[assign_op]), prop);
  875. }
  876. String VisualScriptPropertySet::get_text() const {
  877. if (!has_input_sequence_port()) {
  878. return "";
  879. }
  880. if (call_mode == CALL_MODE_BASIC_TYPE) {
  881. return vformat(TTR("On %s"), Variant::get_type_name(basic_type));
  882. } else if (call_mode == CALL_MODE_INSTANCE) {
  883. return vformat(TTR("On %s"), base_type);
  884. } else if (call_mode == CALL_MODE_NODE_PATH) {
  885. return " [" + String(base_path.simplified()) + "]";
  886. } else {
  887. return TTR("On Self");
  888. }
  889. }
  890. void VisualScriptPropertySet::_update_base_type() {
  891. //cache it because this information may not be available on load
  892. if (call_mode == CALL_MODE_NODE_PATH) {
  893. Node *node = _get_base_node();
  894. if (node) {
  895. base_type = node->get_class();
  896. }
  897. } else if (call_mode == CALL_MODE_SELF) {
  898. if (get_visual_script().is_valid()) {
  899. base_type = get_visual_script()->get_instance_base_type();
  900. }
  901. }
  902. }
  903. void VisualScriptPropertySet::set_basic_type(Variant::Type p_type) {
  904. if (basic_type == p_type) {
  905. return;
  906. }
  907. basic_type = p_type;
  908. notify_property_list_changed();
  909. _update_base_type();
  910. ports_changed_notify();
  911. }
  912. Variant::Type VisualScriptPropertySet::get_basic_type() const {
  913. return basic_type;
  914. }
  915. void VisualScriptPropertySet::set_base_type(const StringName &p_type) {
  916. if (base_type == p_type) {
  917. return;
  918. }
  919. base_type = p_type;
  920. notify_property_list_changed();
  921. ports_changed_notify();
  922. }
  923. StringName VisualScriptPropertySet::get_base_type() const {
  924. return base_type;
  925. }
  926. void VisualScriptPropertySet::set_base_script(const String &p_path) {
  927. if (base_script == p_path) {
  928. return;
  929. }
  930. base_script = p_path;
  931. notify_property_list_changed();
  932. ports_changed_notify();
  933. }
  934. String VisualScriptPropertySet::get_base_script() const {
  935. return base_script;
  936. }
  937. void VisualScriptPropertySet::_update_cache() {
  938. if (!Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop())) {
  939. return;
  940. }
  941. if (!Engine::get_singleton()->is_editor_hint()) { //only update cache if editor exists, it's pointless otherwise
  942. return;
  943. }
  944. if (call_mode == CALL_MODE_BASIC_TYPE) {
  945. //not super efficient..
  946. Variant v;
  947. Callable::CallError ce;
  948. Variant::construct(basic_type, v, nullptr, 0, ce);
  949. List<PropertyInfo> pinfo;
  950. v.get_property_list(&pinfo);
  951. for (const PropertyInfo &E : pinfo) {
  952. if (E.name == property) {
  953. type_cache = E;
  954. }
  955. }
  956. } else {
  957. StringName type;
  958. Ref<Script> script;
  959. Node *node = nullptr;
  960. if (call_mode == CALL_MODE_NODE_PATH) {
  961. node = _get_base_node();
  962. if (node) {
  963. type = node->get_class();
  964. base_type = type; //cache, too
  965. script = node->get_script();
  966. }
  967. } else if (call_mode == CALL_MODE_SELF) {
  968. if (get_visual_script().is_valid()) {
  969. type = get_visual_script()->get_instance_base_type();
  970. base_type = type; //cache, too
  971. script = get_visual_script();
  972. }
  973. } else if (call_mode == CALL_MODE_INSTANCE) {
  974. type = base_type;
  975. if (!base_script.is_empty()) {
  976. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  977. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  978. }
  979. if (ResourceCache::has(base_script)) {
  980. script = Ref<Resource>(ResourceCache::get(base_script));
  981. } else {
  982. return;
  983. }
  984. }
  985. }
  986. List<PropertyInfo> pinfo;
  987. if (node) {
  988. node->get_property_list(&pinfo);
  989. } else {
  990. ClassDB::get_property_list(type, &pinfo);
  991. }
  992. if (script.is_valid()) {
  993. script->get_script_property_list(&pinfo);
  994. }
  995. for (const PropertyInfo &E : pinfo) {
  996. if (E.name == property) {
  997. type_cache = E;
  998. return;
  999. }
  1000. }
  1001. }
  1002. }
  1003. void VisualScriptPropertySet::set_property(const StringName &p_type) {
  1004. if (property == p_type) {
  1005. return;
  1006. }
  1007. property = p_type;
  1008. index = StringName();
  1009. _update_cache();
  1010. notify_property_list_changed();
  1011. ports_changed_notify();
  1012. }
  1013. StringName VisualScriptPropertySet::get_property() const {
  1014. return property;
  1015. }
  1016. void VisualScriptPropertySet::set_base_path(const NodePath &p_type) {
  1017. if (base_path == p_type) {
  1018. return;
  1019. }
  1020. base_path = p_type;
  1021. _update_base_type();
  1022. notify_property_list_changed();
  1023. ports_changed_notify();
  1024. }
  1025. NodePath VisualScriptPropertySet::get_base_path() const {
  1026. return base_path;
  1027. }
  1028. void VisualScriptPropertySet::set_call_mode(CallMode p_mode) {
  1029. if (call_mode == p_mode) {
  1030. return;
  1031. }
  1032. call_mode = p_mode;
  1033. _update_base_type();
  1034. notify_property_list_changed();
  1035. ports_changed_notify();
  1036. }
  1037. VisualScriptPropertySet::CallMode VisualScriptPropertySet::get_call_mode() const {
  1038. return call_mode;
  1039. }
  1040. void VisualScriptPropertySet::_set_type_cache(const Dictionary &p_type) {
  1041. type_cache = PropertyInfo::from_dict(p_type);
  1042. }
  1043. Dictionary VisualScriptPropertySet::_get_type_cache() const {
  1044. return type_cache;
  1045. }
  1046. void VisualScriptPropertySet::set_index(const StringName &p_type) {
  1047. if (index == p_type) {
  1048. return;
  1049. }
  1050. index = p_type;
  1051. _update_cache();
  1052. notify_property_list_changed();
  1053. ports_changed_notify();
  1054. }
  1055. StringName VisualScriptPropertySet::get_index() const {
  1056. return index;
  1057. }
  1058. void VisualScriptPropertySet::set_assign_op(AssignOp p_op) {
  1059. ERR_FAIL_INDEX(p_op, ASSIGN_OP_MAX);
  1060. if (assign_op == p_op) {
  1061. return;
  1062. }
  1063. assign_op = p_op;
  1064. _update_cache();
  1065. notify_property_list_changed();
  1066. ports_changed_notify();
  1067. }
  1068. VisualScriptPropertySet::AssignOp VisualScriptPropertySet::get_assign_op() const {
  1069. return assign_op;
  1070. }
  1071. void VisualScriptPropertySet::_validate_property(PropertyInfo &property) const {
  1072. if (property.name == "base_type") {
  1073. if (call_mode != CALL_MODE_INSTANCE) {
  1074. property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  1075. }
  1076. }
  1077. if (property.name == "base_script") {
  1078. if (call_mode != CALL_MODE_INSTANCE) {
  1079. property.usage = PROPERTY_USAGE_NONE;
  1080. }
  1081. }
  1082. if (property.name == "basic_type") {
  1083. if (call_mode != CALL_MODE_BASIC_TYPE) {
  1084. property.usage = PROPERTY_USAGE_NONE;
  1085. }
  1086. }
  1087. if (property.name == "node_path") {
  1088. if (call_mode != CALL_MODE_NODE_PATH) {
  1089. property.usage = PROPERTY_USAGE_NONE;
  1090. } else {
  1091. Node *bnode = _get_base_node();
  1092. if (bnode) {
  1093. property.hint_string = bnode->get_path(); //convert to long string
  1094. }
  1095. }
  1096. }
  1097. if (property.name == "property") {
  1098. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1099. property.hint = PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1100. property.hint_string = Variant::get_type_name(basic_type);
  1101. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1102. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1103. property.hint_string = itos(get_visual_script()->get_instance_id());
  1104. } else if (call_mode == CALL_MODE_INSTANCE) {
  1105. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1106. property.hint_string = base_type;
  1107. if (!base_script.is_empty()) {
  1108. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1109. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1110. }
  1111. if (ResourceCache::has(base_script)) {
  1112. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  1113. if (script.is_valid()) {
  1114. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1115. property.hint_string = itos(script->get_instance_id());
  1116. }
  1117. }
  1118. }
  1119. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1120. Node *node = _get_base_node();
  1121. if (node) {
  1122. property.hint = PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1123. property.hint_string = itos(node->get_instance_id());
  1124. } else {
  1125. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1126. property.hint_string = get_base_type();
  1127. }
  1128. }
  1129. }
  1130. if (property.name == "index") {
  1131. Callable::CallError ce;
  1132. Variant v;
  1133. Variant::construct(type_cache.type, v, nullptr, 0, ce);
  1134. List<PropertyInfo> plist;
  1135. v.get_property_list(&plist);
  1136. String options = "";
  1137. for (const PropertyInfo &E : plist) {
  1138. options += "," + E.name;
  1139. }
  1140. property.hint = PROPERTY_HINT_ENUM;
  1141. property.hint_string = options;
  1142. property.type = Variant::STRING;
  1143. if (options.is_empty()) {
  1144. property.usage = PROPERTY_USAGE_NONE; //hide if type has no usable index
  1145. }
  1146. }
  1147. }
  1148. void VisualScriptPropertySet::_bind_methods() {
  1149. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptPropertySet::set_base_type);
  1150. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptPropertySet::get_base_type);
  1151. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptPropertySet::set_base_script);
  1152. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptPropertySet::get_base_script);
  1153. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptPropertySet::set_basic_type);
  1154. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptPropertySet::get_basic_type);
  1155. ClassDB::bind_method(D_METHOD("_set_type_cache", "type_cache"), &VisualScriptPropertySet::_set_type_cache);
  1156. ClassDB::bind_method(D_METHOD("_get_type_cache"), &VisualScriptPropertySet::_get_type_cache);
  1157. ClassDB::bind_method(D_METHOD("set_property", "property"), &VisualScriptPropertySet::set_property);
  1158. ClassDB::bind_method(D_METHOD("get_property"), &VisualScriptPropertySet::get_property);
  1159. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptPropertySet::set_call_mode);
  1160. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptPropertySet::get_call_mode);
  1161. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptPropertySet::set_base_path);
  1162. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptPropertySet::get_base_path);
  1163. ClassDB::bind_method(D_METHOD("set_index", "index"), &VisualScriptPropertySet::set_index);
  1164. ClassDB::bind_method(D_METHOD("get_index"), &VisualScriptPropertySet::get_index);
  1165. ClassDB::bind_method(D_METHOD("set_assign_op", "assign_op"), &VisualScriptPropertySet::set_assign_op);
  1166. ClassDB::bind_method(D_METHOD("get_assign_op"), &VisualScriptPropertySet::get_assign_op);
  1167. String bt;
  1168. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1169. if (i > 0) {
  1170. bt += ",";
  1171. }
  1172. bt += Variant::get_type_name(Variant::Type(i));
  1173. }
  1174. List<String> script_extensions;
  1175. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1176. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1177. }
  1178. String script_ext_hint;
  1179. for (const String &E : script_extensions) {
  1180. if (!script_ext_hint.is_empty()) {
  1181. script_ext_hint += ",";
  1182. }
  1183. script_ext_hint += "*." + E;
  1184. }
  1185. ADD_PROPERTY(PropertyInfo(Variant::INT, "set_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type"), "set_call_mode", "get_call_mode");
  1186. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1187. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  1188. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_type_cache", "_get_type_cache");
  1189. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  1190. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  1191. ADD_PROPERTY(PropertyInfo(Variant::STRING, "property"), "set_property", "get_property");
  1192. ADD_PROPERTY(PropertyInfo(Variant::STRING, "index"), "set_index", "get_index");
  1193. ADD_PROPERTY(PropertyInfo(Variant::INT, "assign_op", PROPERTY_HINT_ENUM, "Assign,Add,Sub,Mul,Div,Mod,ShiftLeft,ShiftRight,BitAnd,BitOr,Bitxor"), "set_assign_op", "get_assign_op");
  1194. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  1195. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  1196. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  1197. BIND_ENUM_CONSTANT(CALL_MODE_BASIC_TYPE);
  1198. BIND_ENUM_CONSTANT(ASSIGN_OP_NONE);
  1199. BIND_ENUM_CONSTANT(ASSIGN_OP_ADD);
  1200. BIND_ENUM_CONSTANT(ASSIGN_OP_SUB);
  1201. BIND_ENUM_CONSTANT(ASSIGN_OP_MUL);
  1202. BIND_ENUM_CONSTANT(ASSIGN_OP_DIV);
  1203. BIND_ENUM_CONSTANT(ASSIGN_OP_MOD);
  1204. BIND_ENUM_CONSTANT(ASSIGN_OP_SHIFT_LEFT);
  1205. BIND_ENUM_CONSTANT(ASSIGN_OP_SHIFT_RIGHT);
  1206. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_AND);
  1207. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_OR);
  1208. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_XOR);
  1209. }
  1210. class VisualScriptNodeInstancePropertySet : public VisualScriptNodeInstance {
  1211. public:
  1212. VisualScriptPropertySet::CallMode call_mode;
  1213. NodePath node_path;
  1214. StringName property;
  1215. VisualScriptPropertySet *node;
  1216. VisualScriptInstance *instance;
  1217. VisualScriptPropertySet::AssignOp assign_op;
  1218. StringName index;
  1219. bool needs_get;
  1220. //virtual int get_working_memory_size() const { return 0; }
  1221. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1222. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1223. _FORCE_INLINE_ void _process_get(Variant &source, const Variant &p_argument, bool &valid) {
  1224. if (index != StringName() && assign_op == VisualScriptPropertySet::ASSIGN_OP_NONE) {
  1225. source.set_named(index, p_argument, valid);
  1226. } else {
  1227. Variant value;
  1228. if (index != StringName()) {
  1229. value = source.get_named(index, valid);
  1230. } else {
  1231. value = source;
  1232. }
  1233. switch (assign_op) {
  1234. case VisualScriptPropertySet::ASSIGN_OP_NONE: {
  1235. //should never get here
  1236. } break;
  1237. case VisualScriptPropertySet::ASSIGN_OP_ADD: {
  1238. value = Variant::evaluate(Variant::OP_ADD, value, p_argument);
  1239. } break;
  1240. case VisualScriptPropertySet::ASSIGN_OP_SUB: {
  1241. value = Variant::evaluate(Variant::OP_SUBTRACT, value, p_argument);
  1242. } break;
  1243. case VisualScriptPropertySet::ASSIGN_OP_MUL: {
  1244. value = Variant::evaluate(Variant::OP_MULTIPLY, value, p_argument);
  1245. } break;
  1246. case VisualScriptPropertySet::ASSIGN_OP_DIV: {
  1247. value = Variant::evaluate(Variant::OP_DIVIDE, value, p_argument);
  1248. } break;
  1249. case VisualScriptPropertySet::ASSIGN_OP_MOD: {
  1250. value = Variant::evaluate(Variant::OP_MODULE, value, p_argument);
  1251. } break;
  1252. case VisualScriptPropertySet::ASSIGN_OP_SHIFT_LEFT: {
  1253. value = Variant::evaluate(Variant::OP_SHIFT_LEFT, value, p_argument);
  1254. } break;
  1255. case VisualScriptPropertySet::ASSIGN_OP_SHIFT_RIGHT: {
  1256. value = Variant::evaluate(Variant::OP_SHIFT_RIGHT, value, p_argument);
  1257. } break;
  1258. case VisualScriptPropertySet::ASSIGN_OP_BIT_AND: {
  1259. value = Variant::evaluate(Variant::OP_BIT_AND, value, p_argument);
  1260. } break;
  1261. case VisualScriptPropertySet::ASSIGN_OP_BIT_OR: {
  1262. value = Variant::evaluate(Variant::OP_BIT_OR, value, p_argument);
  1263. } break;
  1264. case VisualScriptPropertySet::ASSIGN_OP_BIT_XOR: {
  1265. value = Variant::evaluate(Variant::OP_BIT_XOR, value, p_argument);
  1266. } break;
  1267. default: {
  1268. }
  1269. }
  1270. if (index != StringName()) {
  1271. source.set_named(index, value, valid);
  1272. } else {
  1273. source = value;
  1274. }
  1275. }
  1276. }
  1277. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Callable::CallError &r_error, String &r_error_str) {
  1278. switch (call_mode) {
  1279. case VisualScriptPropertySet::CALL_MODE_SELF: {
  1280. Object *object = instance->get_owner_ptr();
  1281. bool valid;
  1282. if (needs_get) {
  1283. Variant value = object->get(property, &valid);
  1284. _process_get(value, *p_inputs[0], valid);
  1285. object->set(property, value, &valid);
  1286. } else {
  1287. object->set(property, *p_inputs[0], &valid);
  1288. }
  1289. if (!valid) {
  1290. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1291. r_error_str = "Invalid set value '" + String(*p_inputs[0]) + "' on property '" + String(property) + "' of type " + object->get_class();
  1292. }
  1293. } break;
  1294. case VisualScriptPropertySet::CALL_MODE_NODE_PATH: {
  1295. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1296. if (!node) {
  1297. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1298. r_error_str = "Base object is not a Node!";
  1299. return 0;
  1300. }
  1301. Node *another = node->get_node(node_path);
  1302. if (!another) {
  1303. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1304. r_error_str = "Path does not lead Node!";
  1305. return 0;
  1306. }
  1307. bool valid;
  1308. if (needs_get) {
  1309. Variant value = another->get(property, &valid);
  1310. _process_get(value, *p_inputs[0], valid);
  1311. another->set(property, value, &valid);
  1312. } else {
  1313. another->set(property, *p_inputs[0], &valid);
  1314. }
  1315. if (!valid) {
  1316. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1317. r_error_str = "Invalid set value '" + String(*p_inputs[0]) + "' on property '" + String(property) + "' of type " + another->get_class();
  1318. }
  1319. } break;
  1320. case VisualScriptPropertySet::CALL_MODE_INSTANCE:
  1321. case VisualScriptPropertySet::CALL_MODE_BASIC_TYPE: {
  1322. Variant v = *p_inputs[0];
  1323. bool valid;
  1324. if (needs_get) {
  1325. Variant value = v.get_named(property, valid);
  1326. _process_get(value, *p_inputs[1], valid);
  1327. v.set_named(property, value, valid);
  1328. } else {
  1329. v.set_named(property, *p_inputs[1], valid);
  1330. }
  1331. if (!valid) {
  1332. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1333. r_error_str = "Invalid set value '" + String(*p_inputs[1]) + "' (" + Variant::get_type_name(p_inputs[1]->get_type()) + ") on property '" + String(property) + "' of type " + Variant::get_type_name(v.get_type());
  1334. }
  1335. *p_outputs[0] = v;
  1336. } break;
  1337. }
  1338. return 0;
  1339. }
  1340. };
  1341. VisualScriptNodeInstance *VisualScriptPropertySet::instantiate(VisualScriptInstance *p_instance) {
  1342. VisualScriptNodeInstancePropertySet *instance = memnew(VisualScriptNodeInstancePropertySet);
  1343. instance->node = this;
  1344. instance->instance = p_instance;
  1345. instance->property = property;
  1346. instance->call_mode = call_mode;
  1347. instance->node_path = base_path;
  1348. instance->assign_op = assign_op;
  1349. instance->index = index;
  1350. instance->needs_get = index != StringName() || assign_op != ASSIGN_OP_NONE;
  1351. return instance;
  1352. }
  1353. VisualScriptPropertySet::TypeGuess VisualScriptPropertySet::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  1354. if (p_output == 0 && call_mode == CALL_MODE_INSTANCE) {
  1355. return p_inputs[0];
  1356. }
  1357. return VisualScriptNode::guess_output_type(p_inputs, p_output);
  1358. }
  1359. VisualScriptPropertySet::VisualScriptPropertySet() {
  1360. assign_op = ASSIGN_OP_NONE;
  1361. call_mode = CALL_MODE_SELF;
  1362. base_type = "Object";
  1363. basic_type = Variant::NIL;
  1364. }
  1365. template <VisualScriptPropertySet::CallMode cmode>
  1366. static Ref<VisualScriptNode> create_property_set_node(const String &p_name) {
  1367. Ref<VisualScriptPropertySet> node;
  1368. node.instantiate();
  1369. node->set_call_mode(cmode);
  1370. return node;
  1371. }
  1372. //////////////////////////////////////////
  1373. ////////////////GET//////////////////////
  1374. //////////////////////////////////////////
  1375. int VisualScriptPropertyGet::get_output_sequence_port_count() const {
  1376. return 0; // (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?0:1;
  1377. }
  1378. bool VisualScriptPropertyGet::has_input_sequence_port() const {
  1379. return false; //(call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?false:true;
  1380. }
  1381. void VisualScriptPropertyGet::_update_base_type() {
  1382. //cache it because this information may not be available on load
  1383. if (call_mode == CALL_MODE_NODE_PATH) {
  1384. Node *node = _get_base_node();
  1385. if (node) {
  1386. base_type = node->get_class();
  1387. }
  1388. } else if (call_mode == CALL_MODE_SELF) {
  1389. if (get_visual_script().is_valid()) {
  1390. base_type = get_visual_script()->get_instance_base_type();
  1391. }
  1392. }
  1393. }
  1394. Node *VisualScriptPropertyGet::_get_base_node() const {
  1395. #ifdef TOOLS_ENABLED
  1396. Ref<Script> script = get_visual_script();
  1397. if (!script.is_valid()) {
  1398. return nullptr;
  1399. }
  1400. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  1401. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  1402. if (!scene_tree) {
  1403. return nullptr;
  1404. }
  1405. Node *edited_scene = scene_tree->get_edited_scene_root();
  1406. if (!edited_scene) {
  1407. return nullptr;
  1408. }
  1409. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  1410. if (!script_node) {
  1411. return nullptr;
  1412. }
  1413. if (!script_node->has_node(base_path)) {
  1414. return nullptr;
  1415. }
  1416. Node *path_to = script_node->get_node(base_path);
  1417. return path_to;
  1418. #else
  1419. return nullptr;
  1420. #endif
  1421. }
  1422. StringName VisualScriptPropertyGet::_get_base_type() const {
  1423. if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1424. return get_visual_script()->get_instance_base_type();
  1425. } else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  1426. Node *path = _get_base_node();
  1427. if (path) {
  1428. return path->get_class();
  1429. }
  1430. }
  1431. return base_type;
  1432. }
  1433. int VisualScriptPropertyGet::get_input_value_port_count() const {
  1434. return (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 1 : 0;
  1435. }
  1436. int VisualScriptPropertyGet::get_output_value_port_count() const {
  1437. int pc = (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 2 : 1;
  1438. return pc;
  1439. }
  1440. String VisualScriptPropertyGet::get_output_sequence_port_text(int p_port) const {
  1441. return String();
  1442. }
  1443. PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const {
  1444. if (call_mode == CALL_MODE_INSTANCE || call_mode == CALL_MODE_BASIC_TYPE) {
  1445. if (p_idx == 0) {
  1446. PropertyInfo pi;
  1447. pi.type = (call_mode == CALL_MODE_INSTANCE ? Variant::OBJECT : basic_type);
  1448. pi.name = (call_mode == CALL_MODE_INSTANCE ? String("instance") : Variant::get_type_name(basic_type).to_lower());
  1449. return pi;
  1450. }
  1451. }
  1452. return PropertyInfo();
  1453. }
  1454. PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const {
  1455. if (call_mode == CALL_MODE_BASIC_TYPE && p_idx == 0) {
  1456. return PropertyInfo(basic_type, "pass");
  1457. } else if (call_mode == CALL_MODE_INSTANCE && p_idx == 0) {
  1458. return PropertyInfo(Variant::OBJECT, "pass", PROPERTY_HINT_TYPE_STRING, get_base_type());
  1459. } else {
  1460. List<PropertyInfo> props;
  1461. ClassDB::get_property_list(_get_base_type(), &props, false);
  1462. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1463. if (E->get().name == property) {
  1464. PropertyInfo pinfo = PropertyInfo(E->get().type, String(property) + "." + String(index), E->get().hint, E->get().hint_string);
  1465. _adjust_input_index(pinfo);
  1466. return pinfo;
  1467. }
  1468. }
  1469. }
  1470. PropertyInfo pinfo = PropertyInfo(type_cache, "value");
  1471. _adjust_input_index(pinfo);
  1472. return pinfo;
  1473. }
  1474. String VisualScriptPropertyGet::get_caption() const {
  1475. String prop = property;
  1476. if (index != StringName()) {
  1477. prop += "." + String(index);
  1478. }
  1479. return vformat(TTR("Get %s"), prop);
  1480. }
  1481. String VisualScriptPropertyGet::get_text() const {
  1482. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1483. return vformat(TTR("On %s"), Variant::get_type_name(basic_type));
  1484. } else if (call_mode == CALL_MODE_INSTANCE) {
  1485. return vformat(TTR("On %s"), base_type);
  1486. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1487. return " [" + String(base_path.simplified()) + "]";
  1488. } else {
  1489. return TTR("On Self");
  1490. }
  1491. }
  1492. void VisualScriptPropertyGet::set_base_type(const StringName &p_type) {
  1493. if (base_type == p_type) {
  1494. return;
  1495. }
  1496. base_type = p_type;
  1497. notify_property_list_changed();
  1498. ports_changed_notify();
  1499. }
  1500. StringName VisualScriptPropertyGet::get_base_type() const {
  1501. return base_type;
  1502. }
  1503. void VisualScriptPropertyGet::set_base_script(const String &p_path) {
  1504. if (base_script == p_path) {
  1505. return;
  1506. }
  1507. base_script = p_path;
  1508. notify_property_list_changed();
  1509. ports_changed_notify();
  1510. }
  1511. String VisualScriptPropertyGet::get_base_script() const {
  1512. return base_script;
  1513. }
  1514. void VisualScriptPropertyGet::_update_cache() {
  1515. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1516. //not super efficient..
  1517. Variant v;
  1518. Callable::CallError ce;
  1519. Variant::construct(basic_type, v, nullptr, 0, ce);
  1520. List<PropertyInfo> pinfo;
  1521. v.get_property_list(&pinfo);
  1522. for (const PropertyInfo &E : pinfo) {
  1523. if (E.name == property) {
  1524. type_cache = E.type;
  1525. return;
  1526. }
  1527. }
  1528. } else {
  1529. StringName type;
  1530. Ref<Script> script;
  1531. Node *node = nullptr;
  1532. if (call_mode == CALL_MODE_NODE_PATH) {
  1533. node = _get_base_node();
  1534. if (node) {
  1535. type = node->get_class();
  1536. base_type = type; //cache, too
  1537. script = node->get_script();
  1538. }
  1539. } else if (call_mode == CALL_MODE_SELF) {
  1540. if (get_visual_script().is_valid()) {
  1541. type = get_visual_script()->get_instance_base_type();
  1542. base_type = type; //cache, too
  1543. script = get_visual_script();
  1544. }
  1545. } else if (call_mode == CALL_MODE_INSTANCE) {
  1546. type = base_type;
  1547. if (!base_script.is_empty()) {
  1548. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1549. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1550. }
  1551. if (ResourceCache::has(base_script)) {
  1552. script = Ref<Resource>(ResourceCache::get(base_script));
  1553. } else {
  1554. return;
  1555. }
  1556. }
  1557. }
  1558. bool valid = false;
  1559. Variant::Type type_ret;
  1560. type_ret = ClassDB::get_property_type(base_type, property, &valid);
  1561. if (valid) {
  1562. type_cache = type_ret;
  1563. return; //all dandy
  1564. }
  1565. if (node) {
  1566. Variant prop = node->get(property, &valid);
  1567. if (valid) {
  1568. type_cache = prop.get_type();
  1569. return; //all dandy again
  1570. }
  1571. }
  1572. if (script.is_valid()) {
  1573. type_ret = script->get_static_property_type(property, &valid);
  1574. if (valid) {
  1575. type_cache = type_ret;
  1576. return; //all dandy
  1577. }
  1578. }
  1579. }
  1580. }
  1581. void VisualScriptPropertyGet::set_property(const StringName &p_type) {
  1582. if (property == p_type) {
  1583. return;
  1584. }
  1585. property = p_type;
  1586. _update_cache();
  1587. notify_property_list_changed();
  1588. ports_changed_notify();
  1589. }
  1590. StringName VisualScriptPropertyGet::get_property() const {
  1591. return property;
  1592. }
  1593. void VisualScriptPropertyGet::set_base_path(const NodePath &p_type) {
  1594. if (base_path == p_type) {
  1595. return;
  1596. }
  1597. base_path = p_type;
  1598. notify_property_list_changed();
  1599. _update_base_type();
  1600. ports_changed_notify();
  1601. }
  1602. NodePath VisualScriptPropertyGet::get_base_path() const {
  1603. return base_path;
  1604. }
  1605. void VisualScriptPropertyGet::set_call_mode(CallMode p_mode) {
  1606. if (call_mode == p_mode) {
  1607. return;
  1608. }
  1609. call_mode = p_mode;
  1610. notify_property_list_changed();
  1611. _update_base_type();
  1612. ports_changed_notify();
  1613. }
  1614. VisualScriptPropertyGet::CallMode VisualScriptPropertyGet::get_call_mode() const {
  1615. return call_mode;
  1616. }
  1617. void VisualScriptPropertyGet::set_basic_type(Variant::Type p_type) {
  1618. if (basic_type == p_type) {
  1619. return;
  1620. }
  1621. basic_type = p_type;
  1622. notify_property_list_changed();
  1623. ports_changed_notify();
  1624. }
  1625. Variant::Type VisualScriptPropertyGet::get_basic_type() const {
  1626. return basic_type;
  1627. }
  1628. void VisualScriptPropertyGet::_set_type_cache(Variant::Type p_type) {
  1629. type_cache = p_type;
  1630. }
  1631. Variant::Type VisualScriptPropertyGet::_get_type_cache() const {
  1632. return type_cache;
  1633. }
  1634. void VisualScriptPropertyGet::_adjust_input_index(PropertyInfo &pinfo) const {
  1635. if (index != StringName()) {
  1636. Variant v;
  1637. Callable::CallError ce;
  1638. Variant::construct(pinfo.type, v, nullptr, 0, ce);
  1639. Variant i = v.get(index);
  1640. pinfo.type = i.get_type();
  1641. pinfo.name = String(property) + "." + index;
  1642. } else {
  1643. pinfo.name = String(property);
  1644. }
  1645. }
  1646. void VisualScriptPropertyGet::set_index(const StringName &p_type) {
  1647. if (index == p_type) {
  1648. return;
  1649. }
  1650. index = p_type;
  1651. _update_cache();
  1652. notify_property_list_changed();
  1653. ports_changed_notify();
  1654. }
  1655. StringName VisualScriptPropertyGet::get_index() const {
  1656. return index;
  1657. }
  1658. void VisualScriptPropertyGet::_validate_property(PropertyInfo &property) const {
  1659. if (property.name == "base_type") {
  1660. if (call_mode != CALL_MODE_INSTANCE) {
  1661. property.usage = PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL;
  1662. }
  1663. }
  1664. if (property.name == "base_script") {
  1665. if (call_mode != CALL_MODE_INSTANCE) {
  1666. property.usage = PROPERTY_USAGE_NONE;
  1667. }
  1668. }
  1669. if (property.name == "basic_type") {
  1670. if (call_mode != CALL_MODE_BASIC_TYPE) {
  1671. property.usage = PROPERTY_USAGE_NONE;
  1672. }
  1673. }
  1674. if (property.name == "node_path") {
  1675. if (call_mode != CALL_MODE_NODE_PATH) {
  1676. property.usage = PROPERTY_USAGE_NONE;
  1677. } else {
  1678. Node *bnode = _get_base_node();
  1679. if (bnode) {
  1680. property.hint_string = bnode->get_path(); //convert to long string
  1681. }
  1682. }
  1683. }
  1684. if (property.name == "property") {
  1685. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1686. property.hint = PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1687. property.hint_string = Variant::get_type_name(basic_type);
  1688. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1689. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1690. property.hint_string = itos(get_visual_script()->get_instance_id());
  1691. } else if (call_mode == CALL_MODE_INSTANCE) {
  1692. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1693. property.hint_string = base_type;
  1694. if (!base_script.is_empty()) {
  1695. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1696. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1697. }
  1698. if (ResourceCache::has(base_script)) {
  1699. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  1700. if (script.is_valid()) {
  1701. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1702. property.hint_string = itos(script->get_instance_id());
  1703. }
  1704. }
  1705. }
  1706. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1707. Node *node = _get_base_node();
  1708. if (node) {
  1709. property.hint = PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1710. property.hint_string = itos(node->get_instance_id());
  1711. } else {
  1712. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1713. property.hint_string = get_base_type();
  1714. }
  1715. }
  1716. }
  1717. if (property.name == "index") {
  1718. Callable::CallError ce;
  1719. Variant v;
  1720. Variant::construct(type_cache, v, nullptr, 0, ce);
  1721. List<PropertyInfo> plist;
  1722. v.get_property_list(&plist);
  1723. String options = "";
  1724. for (const PropertyInfo &E : plist) {
  1725. options += "," + E.name;
  1726. }
  1727. property.hint = PROPERTY_HINT_ENUM;
  1728. property.hint_string = options;
  1729. property.type = Variant::STRING;
  1730. if (options.is_empty()) {
  1731. property.usage = PROPERTY_USAGE_NONE; //hide if type has no usable index
  1732. }
  1733. }
  1734. }
  1735. void VisualScriptPropertyGet::_bind_methods() {
  1736. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptPropertyGet::set_base_type);
  1737. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptPropertyGet::get_base_type);
  1738. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptPropertyGet::set_base_script);
  1739. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptPropertyGet::get_base_script);
  1740. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptPropertyGet::set_basic_type);
  1741. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptPropertyGet::get_basic_type);
  1742. ClassDB::bind_method(D_METHOD("_set_type_cache", "type_cache"), &VisualScriptPropertyGet::_set_type_cache);
  1743. ClassDB::bind_method(D_METHOD("_get_type_cache"), &VisualScriptPropertyGet::_get_type_cache);
  1744. ClassDB::bind_method(D_METHOD("set_property", "property"), &VisualScriptPropertyGet::set_property);
  1745. ClassDB::bind_method(D_METHOD("get_property"), &VisualScriptPropertyGet::get_property);
  1746. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptPropertyGet::set_call_mode);
  1747. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptPropertyGet::get_call_mode);
  1748. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptPropertyGet::set_base_path);
  1749. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptPropertyGet::get_base_path);
  1750. ClassDB::bind_method(D_METHOD("set_index", "index"), &VisualScriptPropertyGet::set_index);
  1751. ClassDB::bind_method(D_METHOD("get_index"), &VisualScriptPropertyGet::get_index);
  1752. String bt;
  1753. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1754. if (i > 0) {
  1755. bt += ",";
  1756. }
  1757. bt += Variant::get_type_name(Variant::Type(i));
  1758. }
  1759. List<String> script_extensions;
  1760. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1761. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1762. }
  1763. String script_ext_hint;
  1764. for (const String &E : script_extensions) {
  1765. if (!script_ext_hint.is_empty()) {
  1766. script_ext_hint += ",";
  1767. }
  1768. script_ext_hint += "." + E;
  1769. }
  1770. ADD_PROPERTY(PropertyInfo(Variant::INT, "set_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type"), "set_call_mode", "get_call_mode");
  1771. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1772. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  1773. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_type_cache", "_get_type_cache");
  1774. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  1775. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  1776. ADD_PROPERTY(PropertyInfo(Variant::STRING, "property"), "set_property", "get_property");
  1777. ADD_PROPERTY(PropertyInfo(Variant::STRING, "index", PROPERTY_HINT_ENUM), "set_index", "get_index");
  1778. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  1779. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  1780. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  1781. BIND_ENUM_CONSTANT(CALL_MODE_BASIC_TYPE);
  1782. }
  1783. class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
  1784. public:
  1785. VisualScriptPropertyGet::CallMode call_mode;
  1786. NodePath node_path;
  1787. StringName property;
  1788. StringName index;
  1789. VisualScriptPropertyGet *node;
  1790. VisualScriptInstance *instance;
  1791. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Callable::CallError &r_error, String &r_error_str) {
  1792. switch (call_mode) {
  1793. case VisualScriptPropertyGet::CALL_MODE_SELF: {
  1794. Object *object = instance->get_owner_ptr();
  1795. bool valid;
  1796. *p_outputs[0] = object->get(property, &valid);
  1797. if (index != StringName()) {
  1798. *p_outputs[0] = p_outputs[0]->get_named(index, valid);
  1799. }
  1800. if (!valid) {
  1801. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1802. r_error_str = RTR("Invalid index property name.");
  1803. return 0;
  1804. }
  1805. } break;
  1806. case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
  1807. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1808. if (!node) {
  1809. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1810. r_error_str = RTR("Base object is not a Node!");
  1811. return 0;
  1812. }
  1813. Node *another = node->get_node(node_path);
  1814. if (!another) {
  1815. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1816. r_error_str = RTR("Path does not lead Node!");
  1817. return 0;
  1818. }
  1819. bool valid;
  1820. *p_outputs[0] = another->get(property, &valid);
  1821. if (index != StringName()) {
  1822. *p_outputs[0] = p_outputs[0]->get_named(index, valid);
  1823. }
  1824. if (!valid) {
  1825. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1826. r_error_str = vformat(RTR("Invalid index property name '%s' in node %s."), String(property), another->get_name());
  1827. return 0;
  1828. }
  1829. } break;
  1830. default: {
  1831. bool valid;
  1832. Variant v = *p_inputs[0];
  1833. *p_outputs[1] = v.get(property, &valid);
  1834. if (index != StringName()) {
  1835. *p_outputs[1] = p_outputs[1]->get_named(index, valid);
  1836. }
  1837. if (!valid) {
  1838. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1839. r_error_str = RTR("Invalid index property name.");
  1840. }
  1841. *p_outputs[0] = v;
  1842. };
  1843. }
  1844. return 0;
  1845. }
  1846. };
  1847. VisualScriptNodeInstance *VisualScriptPropertyGet::instantiate(VisualScriptInstance *p_instance) {
  1848. VisualScriptNodeInstancePropertyGet *instance = memnew(VisualScriptNodeInstancePropertyGet);
  1849. instance->node = this;
  1850. instance->instance = p_instance;
  1851. instance->property = property;
  1852. instance->call_mode = call_mode;
  1853. instance->node_path = base_path;
  1854. instance->index = index;
  1855. return instance;
  1856. }
  1857. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  1858. call_mode = CALL_MODE_SELF;
  1859. base_type = "Object";
  1860. basic_type = Variant::NIL;
  1861. type_cache = Variant::NIL;
  1862. }
  1863. template <VisualScriptPropertyGet::CallMode cmode>
  1864. static Ref<VisualScriptNode> create_property_get_node(const String &p_name) {
  1865. Ref<VisualScriptPropertyGet> node;
  1866. node.instantiate();
  1867. node->set_call_mode(cmode);
  1868. return node;
  1869. }
  1870. //////////////////////////////////////////
  1871. ////////////////EMIT//////////////////////
  1872. //////////////////////////////////////////
  1873. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1874. return 1;
  1875. }
  1876. bool VisualScriptEmitSignal::has_input_sequence_port() const {
  1877. return true;
  1878. }
  1879. int VisualScriptEmitSignal::get_input_value_port_count() const {
  1880. Ref<VisualScript> vs = get_visual_script();
  1881. if (vs.is_valid()) {
  1882. if (!vs->has_custom_signal(name)) {
  1883. return 0;
  1884. }
  1885. return vs->custom_signal_get_argument_count(name);
  1886. }
  1887. return 0;
  1888. }
  1889. int VisualScriptEmitSignal::get_output_value_port_count() const {
  1890. return 0;
  1891. }
  1892. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1893. return String();
  1894. }
  1895. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const {
  1896. Ref<VisualScript> vs = get_visual_script();
  1897. if (vs.is_valid()) {
  1898. if (!vs->has_custom_signal(name)) {
  1899. return PropertyInfo();
  1900. }
  1901. return PropertyInfo(vs->custom_signal_get_argument_type(name, p_idx), vs->custom_signal_get_argument_name(name, p_idx));
  1902. }
  1903. return PropertyInfo();
  1904. }
  1905. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const {
  1906. return PropertyInfo();
  1907. }
  1908. String VisualScriptEmitSignal::get_caption() const {
  1909. return vformat(TTR("Emit %s"), name);
  1910. }
  1911. void VisualScriptEmitSignal::set_signal(const StringName &p_type) {
  1912. if (name == p_type) {
  1913. return;
  1914. }
  1915. name = p_type;
  1916. notify_property_list_changed();
  1917. ports_changed_notify();
  1918. }
  1919. StringName VisualScriptEmitSignal::get_signal() const {
  1920. return name;
  1921. }
  1922. void VisualScriptEmitSignal::_validate_property(PropertyInfo &property) const {
  1923. if (property.name == "signal") {
  1924. property.hint = PROPERTY_HINT_ENUM;
  1925. List<StringName> sigs;
  1926. List<MethodInfo> base_sigs;
  1927. Ref<VisualScript> vs = get_visual_script();
  1928. if (vs.is_valid()) {
  1929. vs->get_custom_signal_list(&sigs);
  1930. ClassDB::get_signal_list(vs->get_instance_base_type(), &base_sigs);
  1931. }
  1932. String ml;
  1933. for (const StringName &E : sigs) {
  1934. if (!ml.is_empty()) {
  1935. ml += ",";
  1936. }
  1937. ml += E;
  1938. }
  1939. for (const MethodInfo &E : base_sigs) {
  1940. if (!ml.is_empty()) {
  1941. ml += ",";
  1942. }
  1943. ml += E.name;
  1944. }
  1945. property.hint_string = ml;
  1946. }
  1947. }
  1948. void VisualScriptEmitSignal::_bind_methods() {
  1949. ClassDB::bind_method(D_METHOD("set_signal", "name"), &VisualScriptEmitSignal::set_signal);
  1950. ClassDB::bind_method(D_METHOD("get_signal"), &VisualScriptEmitSignal::get_signal);
  1951. ADD_PROPERTY(PropertyInfo(Variant::STRING, "signal"), "set_signal", "get_signal");
  1952. }
  1953. class VisualScriptNodeInstanceEmitSignal : public VisualScriptNodeInstance {
  1954. public:
  1955. VisualScriptEmitSignal *node;
  1956. VisualScriptInstance *instance;
  1957. int argcount;
  1958. StringName name;
  1959. //virtual int get_working_memory_size() const { return 0; }
  1960. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1961. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1962. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Callable::CallError &r_error, String &r_error_str) {
  1963. Object *obj = instance->get_owner_ptr();
  1964. obj->emit_signal(name, p_inputs, argcount);
  1965. return 0;
  1966. }
  1967. };
  1968. VisualScriptNodeInstance *VisualScriptEmitSignal::instantiate(VisualScriptInstance *p_instance) {
  1969. VisualScriptNodeInstanceEmitSignal *instance = memnew(VisualScriptNodeInstanceEmitSignal);
  1970. instance->node = this;
  1971. instance->instance = p_instance;
  1972. instance->name = name;
  1973. instance->argcount = get_input_value_port_count();
  1974. return instance;
  1975. }
  1976. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1977. }
  1978. static Ref<VisualScriptNode> create_basic_type_call_node(const String &p_name) {
  1979. Vector<String> path = p_name.split("/");
  1980. ERR_FAIL_COND_V(path.size() < 4, Ref<VisualScriptNode>());
  1981. String base_type = path[2];
  1982. String method = path[3];
  1983. Ref<VisualScriptFunctionCall> node;
  1984. node.instantiate();
  1985. Variant::Type type = Variant::VARIANT_MAX;
  1986. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1987. if (Variant::get_type_name(Variant::Type(i)) == base_type) {
  1988. type = Variant::Type(i);
  1989. break;
  1990. }
  1991. }
  1992. ERR_FAIL_COND_V(type == Variant::VARIANT_MAX, Ref<VisualScriptNode>());
  1993. node->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1994. node->set_basic_type(type);
  1995. node->set_function(method);
  1996. return node;
  1997. }
  1998. void register_visual_script_func_nodes() {
  1999. VisualScriptLanguage::singleton->add_register_func("functions/call", create_node_generic<VisualScriptFunctionCall>);
  2000. VisualScriptLanguage::singleton->add_register_func("functions/set", create_node_generic<VisualScriptPropertySet>);
  2001. VisualScriptLanguage::singleton->add_register_func("functions/get", create_node_generic<VisualScriptPropertyGet>);
  2002. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_self",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  2003. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  2004. VisualScriptLanguage::singleton->add_register_func("functions/emit_signal", create_node_generic<VisualScriptEmitSignal>);
  2005. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  2006. Variant::Type t = Variant::Type(i);
  2007. String type_name = Variant::get_type_name(t);
  2008. Callable::CallError ce;
  2009. Variant vt;
  2010. Variant::construct(t, vt, nullptr, 0, ce);
  2011. List<MethodInfo> ml;
  2012. vt.get_method_list(&ml);
  2013. for (const MethodInfo &E : ml) {
  2014. VisualScriptLanguage::singleton->add_register_func("functions/by_type/" + type_name + "/" + E.name, create_basic_type_call_node);
  2015. }
  2016. }
  2017. }