visual_script_func_nodes.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 = String("On ") + Variant::get_type_name(basic_type);
  218. } else if (call_mode == CALL_MODE_INSTANCE) {
  219. text = String("On ") + 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 = "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 != String()) {
  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_NOEDITOR | 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 != String()) {
  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 != String()) {
  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 != String()) {
  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_NOEDITOR | 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 1;
  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. "Set", "Add", "Subtract", "Multiply", "Divide", "Mod", "ShiftLeft", "ShiftRight", "BitAnd", "BitOr", "BitXor"
  859. };
  860. String prop = String(opname[assign_op]) + " " + property;
  861. if (index != StringName()) {
  862. prop += "." + String(index);
  863. }
  864. return prop;
  865. }
  866. String VisualScriptPropertySet::get_text() const {
  867. if (!has_input_sequence_port()) {
  868. return "";
  869. }
  870. if (call_mode == CALL_MODE_BASIC_TYPE) {
  871. return String("On ") + Variant::get_type_name(basic_type);
  872. } else if (call_mode == CALL_MODE_INSTANCE) {
  873. return String("On ") + base_type;
  874. } else if (call_mode == CALL_MODE_NODE_PATH) {
  875. return " [" + String(base_path.simplified()) + "]";
  876. } else {
  877. return "On Self";
  878. }
  879. }
  880. void VisualScriptPropertySet::_update_base_type() {
  881. //cache it because this information may not be available on load
  882. if (call_mode == CALL_MODE_NODE_PATH) {
  883. Node *node = _get_base_node();
  884. if (node) {
  885. base_type = node->get_class();
  886. }
  887. } else if (call_mode == CALL_MODE_SELF) {
  888. if (get_visual_script().is_valid()) {
  889. base_type = get_visual_script()->get_instance_base_type();
  890. }
  891. }
  892. }
  893. void VisualScriptPropertySet::set_basic_type(Variant::Type p_type) {
  894. if (basic_type == p_type) {
  895. return;
  896. }
  897. basic_type = p_type;
  898. notify_property_list_changed();
  899. _update_base_type();
  900. ports_changed_notify();
  901. }
  902. Variant::Type VisualScriptPropertySet::get_basic_type() const {
  903. return basic_type;
  904. }
  905. void VisualScriptPropertySet::set_base_type(const StringName &p_type) {
  906. if (base_type == p_type) {
  907. return;
  908. }
  909. base_type = p_type;
  910. notify_property_list_changed();
  911. ports_changed_notify();
  912. }
  913. StringName VisualScriptPropertySet::get_base_type() const {
  914. return base_type;
  915. }
  916. void VisualScriptPropertySet::set_base_script(const String &p_path) {
  917. if (base_script == p_path) {
  918. return;
  919. }
  920. base_script = p_path;
  921. notify_property_list_changed();
  922. ports_changed_notify();
  923. }
  924. String VisualScriptPropertySet::get_base_script() const {
  925. return base_script;
  926. }
  927. void VisualScriptPropertySet::_update_cache() {
  928. if (!Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop())) {
  929. return;
  930. }
  931. if (!Engine::get_singleton()->is_editor_hint()) { //only update cache if editor exists, it's pointless otherwise
  932. return;
  933. }
  934. if (call_mode == CALL_MODE_BASIC_TYPE) {
  935. //not super efficient..
  936. Variant v;
  937. Callable::CallError ce;
  938. Variant::construct(basic_type, v, nullptr, 0, ce);
  939. List<PropertyInfo> pinfo;
  940. v.get_property_list(&pinfo);
  941. for (const PropertyInfo &E : pinfo) {
  942. if (E.name == property) {
  943. type_cache = E;
  944. }
  945. }
  946. } else {
  947. StringName type;
  948. Ref<Script> script;
  949. Node *node = nullptr;
  950. if (call_mode == CALL_MODE_NODE_PATH) {
  951. node = _get_base_node();
  952. if (node) {
  953. type = node->get_class();
  954. base_type = type; //cache, too
  955. script = node->get_script();
  956. }
  957. } else if (call_mode == CALL_MODE_SELF) {
  958. if (get_visual_script().is_valid()) {
  959. type = get_visual_script()->get_instance_base_type();
  960. base_type = type; //cache, too
  961. script = get_visual_script();
  962. }
  963. } else if (call_mode == CALL_MODE_INSTANCE) {
  964. type = base_type;
  965. if (base_script != String()) {
  966. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  967. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  968. }
  969. if (ResourceCache::has(base_script)) {
  970. script = Ref<Resource>(ResourceCache::get(base_script));
  971. } else {
  972. return;
  973. }
  974. }
  975. }
  976. List<PropertyInfo> pinfo;
  977. if (node) {
  978. node->get_property_list(&pinfo);
  979. } else {
  980. ClassDB::get_property_list(type, &pinfo);
  981. }
  982. if (script.is_valid()) {
  983. script->get_script_property_list(&pinfo);
  984. }
  985. for (const PropertyInfo &E : pinfo) {
  986. if (E.name == property) {
  987. type_cache = E;
  988. return;
  989. }
  990. }
  991. }
  992. }
  993. void VisualScriptPropertySet::set_property(const StringName &p_type) {
  994. if (property == p_type) {
  995. return;
  996. }
  997. property = p_type;
  998. index = StringName();
  999. _update_cache();
  1000. notify_property_list_changed();
  1001. ports_changed_notify();
  1002. }
  1003. StringName VisualScriptPropertySet::get_property() const {
  1004. return property;
  1005. }
  1006. void VisualScriptPropertySet::set_base_path(const NodePath &p_type) {
  1007. if (base_path == p_type) {
  1008. return;
  1009. }
  1010. base_path = p_type;
  1011. _update_base_type();
  1012. notify_property_list_changed();
  1013. ports_changed_notify();
  1014. }
  1015. NodePath VisualScriptPropertySet::get_base_path() const {
  1016. return base_path;
  1017. }
  1018. void VisualScriptPropertySet::set_call_mode(CallMode p_mode) {
  1019. if (call_mode == p_mode) {
  1020. return;
  1021. }
  1022. call_mode = p_mode;
  1023. _update_base_type();
  1024. notify_property_list_changed();
  1025. ports_changed_notify();
  1026. }
  1027. VisualScriptPropertySet::CallMode VisualScriptPropertySet::get_call_mode() const {
  1028. return call_mode;
  1029. }
  1030. void VisualScriptPropertySet::_set_type_cache(const Dictionary &p_type) {
  1031. type_cache = PropertyInfo::from_dict(p_type);
  1032. }
  1033. Dictionary VisualScriptPropertySet::_get_type_cache() const {
  1034. return type_cache;
  1035. }
  1036. void VisualScriptPropertySet::set_index(const StringName &p_type) {
  1037. if (index == p_type) {
  1038. return;
  1039. }
  1040. index = p_type;
  1041. _update_cache();
  1042. notify_property_list_changed();
  1043. ports_changed_notify();
  1044. }
  1045. StringName VisualScriptPropertySet::get_index() const {
  1046. return index;
  1047. }
  1048. void VisualScriptPropertySet::set_assign_op(AssignOp p_op) {
  1049. ERR_FAIL_INDEX(p_op, ASSIGN_OP_MAX);
  1050. if (assign_op == p_op) {
  1051. return;
  1052. }
  1053. assign_op = p_op;
  1054. _update_cache();
  1055. notify_property_list_changed();
  1056. ports_changed_notify();
  1057. }
  1058. VisualScriptPropertySet::AssignOp VisualScriptPropertySet::get_assign_op() const {
  1059. return assign_op;
  1060. }
  1061. void VisualScriptPropertySet::_validate_property(PropertyInfo &property) const {
  1062. if (property.name == "base_type") {
  1063. if (call_mode != CALL_MODE_INSTANCE) {
  1064. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  1065. }
  1066. }
  1067. if (property.name == "base_script") {
  1068. if (call_mode != CALL_MODE_INSTANCE) {
  1069. property.usage = PROPERTY_USAGE_NONE;
  1070. }
  1071. }
  1072. if (property.name == "basic_type") {
  1073. if (call_mode != CALL_MODE_BASIC_TYPE) {
  1074. property.usage = PROPERTY_USAGE_NONE;
  1075. }
  1076. }
  1077. if (property.name == "node_path") {
  1078. if (call_mode != CALL_MODE_NODE_PATH) {
  1079. property.usage = PROPERTY_USAGE_NONE;
  1080. } else {
  1081. Node *bnode = _get_base_node();
  1082. if (bnode) {
  1083. property.hint_string = bnode->get_path(); //convert to long string
  1084. }
  1085. }
  1086. }
  1087. if (property.name == "property") {
  1088. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1089. property.hint = PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1090. property.hint_string = Variant::get_type_name(basic_type);
  1091. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1092. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1093. property.hint_string = itos(get_visual_script()->get_instance_id());
  1094. } else if (call_mode == CALL_MODE_INSTANCE) {
  1095. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1096. property.hint_string = base_type;
  1097. if (base_script != String()) {
  1098. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1099. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1100. }
  1101. if (ResourceCache::has(base_script)) {
  1102. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  1103. if (script.is_valid()) {
  1104. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1105. property.hint_string = itos(script->get_instance_id());
  1106. }
  1107. }
  1108. }
  1109. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1110. Node *node = _get_base_node();
  1111. if (node) {
  1112. property.hint = PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1113. property.hint_string = itos(node->get_instance_id());
  1114. } else {
  1115. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1116. property.hint_string = get_base_type();
  1117. }
  1118. }
  1119. }
  1120. if (property.name == "index") {
  1121. Callable::CallError ce;
  1122. Variant v;
  1123. Variant::construct(type_cache.type, v, nullptr, 0, ce);
  1124. List<PropertyInfo> plist;
  1125. v.get_property_list(&plist);
  1126. String options = "";
  1127. for (const PropertyInfo &E : plist) {
  1128. options += "," + E.name;
  1129. }
  1130. property.hint = PROPERTY_HINT_ENUM;
  1131. property.hint_string = options;
  1132. property.type = Variant::STRING;
  1133. if (options == "") {
  1134. property.usage = PROPERTY_USAGE_NONE; //hide if type has no usable index
  1135. }
  1136. }
  1137. }
  1138. void VisualScriptPropertySet::_bind_methods() {
  1139. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptPropertySet::set_base_type);
  1140. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptPropertySet::get_base_type);
  1141. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptPropertySet::set_base_script);
  1142. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptPropertySet::get_base_script);
  1143. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptPropertySet::set_basic_type);
  1144. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptPropertySet::get_basic_type);
  1145. ClassDB::bind_method(D_METHOD("_set_type_cache", "type_cache"), &VisualScriptPropertySet::_set_type_cache);
  1146. ClassDB::bind_method(D_METHOD("_get_type_cache"), &VisualScriptPropertySet::_get_type_cache);
  1147. ClassDB::bind_method(D_METHOD("set_property", "property"), &VisualScriptPropertySet::set_property);
  1148. ClassDB::bind_method(D_METHOD("get_property"), &VisualScriptPropertySet::get_property);
  1149. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptPropertySet::set_call_mode);
  1150. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptPropertySet::get_call_mode);
  1151. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptPropertySet::set_base_path);
  1152. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptPropertySet::get_base_path);
  1153. ClassDB::bind_method(D_METHOD("set_index", "index"), &VisualScriptPropertySet::set_index);
  1154. ClassDB::bind_method(D_METHOD("get_index"), &VisualScriptPropertySet::get_index);
  1155. ClassDB::bind_method(D_METHOD("set_assign_op", "assign_op"), &VisualScriptPropertySet::set_assign_op);
  1156. ClassDB::bind_method(D_METHOD("get_assign_op"), &VisualScriptPropertySet::get_assign_op);
  1157. String bt;
  1158. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1159. if (i > 0) {
  1160. bt += ",";
  1161. }
  1162. bt += Variant::get_type_name(Variant::Type(i));
  1163. }
  1164. List<String> script_extensions;
  1165. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1166. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1167. }
  1168. String script_ext_hint;
  1169. for (const String &E : script_extensions) {
  1170. if (script_ext_hint != String()) {
  1171. script_ext_hint += ",";
  1172. }
  1173. script_ext_hint += "*." + E;
  1174. }
  1175. ADD_PROPERTY(PropertyInfo(Variant::INT, "set_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type"), "set_call_mode", "get_call_mode");
  1176. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1177. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  1178. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_type_cache", "_get_type_cache");
  1179. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  1180. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  1181. ADD_PROPERTY(PropertyInfo(Variant::STRING, "property"), "set_property", "get_property");
  1182. ADD_PROPERTY(PropertyInfo(Variant::STRING, "index"), "set_index", "get_index");
  1183. 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");
  1184. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  1185. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  1186. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  1187. BIND_ENUM_CONSTANT(CALL_MODE_BASIC_TYPE);
  1188. BIND_ENUM_CONSTANT(ASSIGN_OP_NONE);
  1189. BIND_ENUM_CONSTANT(ASSIGN_OP_ADD);
  1190. BIND_ENUM_CONSTANT(ASSIGN_OP_SUB);
  1191. BIND_ENUM_CONSTANT(ASSIGN_OP_MUL);
  1192. BIND_ENUM_CONSTANT(ASSIGN_OP_DIV);
  1193. BIND_ENUM_CONSTANT(ASSIGN_OP_MOD);
  1194. BIND_ENUM_CONSTANT(ASSIGN_OP_SHIFT_LEFT);
  1195. BIND_ENUM_CONSTANT(ASSIGN_OP_SHIFT_RIGHT);
  1196. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_AND);
  1197. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_OR);
  1198. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_XOR);
  1199. }
  1200. class VisualScriptNodeInstancePropertySet : public VisualScriptNodeInstance {
  1201. public:
  1202. VisualScriptPropertySet::CallMode call_mode;
  1203. NodePath node_path;
  1204. StringName property;
  1205. VisualScriptPropertySet *node;
  1206. VisualScriptInstance *instance;
  1207. VisualScriptPropertySet::AssignOp assign_op;
  1208. StringName index;
  1209. bool needs_get;
  1210. //virtual int get_working_memory_size() const { return 0; }
  1211. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1212. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1213. _FORCE_INLINE_ void _process_get(Variant &source, const Variant &p_argument, bool &valid) {
  1214. if (index != StringName() && assign_op == VisualScriptPropertySet::ASSIGN_OP_NONE) {
  1215. source.set_named(index, p_argument, valid);
  1216. } else {
  1217. Variant value;
  1218. if (index != StringName()) {
  1219. value = source.get_named(index, valid);
  1220. } else {
  1221. value = source;
  1222. }
  1223. switch (assign_op) {
  1224. case VisualScriptPropertySet::ASSIGN_OP_NONE: {
  1225. //should never get here
  1226. } break;
  1227. case VisualScriptPropertySet::ASSIGN_OP_ADD: {
  1228. value = Variant::evaluate(Variant::OP_ADD, value, p_argument);
  1229. } break;
  1230. case VisualScriptPropertySet::ASSIGN_OP_SUB: {
  1231. value = Variant::evaluate(Variant::OP_SUBTRACT, value, p_argument);
  1232. } break;
  1233. case VisualScriptPropertySet::ASSIGN_OP_MUL: {
  1234. value = Variant::evaluate(Variant::OP_MULTIPLY, value, p_argument);
  1235. } break;
  1236. case VisualScriptPropertySet::ASSIGN_OP_DIV: {
  1237. value = Variant::evaluate(Variant::OP_DIVIDE, value, p_argument);
  1238. } break;
  1239. case VisualScriptPropertySet::ASSIGN_OP_MOD: {
  1240. value = Variant::evaluate(Variant::OP_MODULE, value, p_argument);
  1241. } break;
  1242. case VisualScriptPropertySet::ASSIGN_OP_SHIFT_LEFT: {
  1243. value = Variant::evaluate(Variant::OP_SHIFT_LEFT, value, p_argument);
  1244. } break;
  1245. case VisualScriptPropertySet::ASSIGN_OP_SHIFT_RIGHT: {
  1246. value = Variant::evaluate(Variant::OP_SHIFT_RIGHT, value, p_argument);
  1247. } break;
  1248. case VisualScriptPropertySet::ASSIGN_OP_BIT_AND: {
  1249. value = Variant::evaluate(Variant::OP_BIT_AND, value, p_argument);
  1250. } break;
  1251. case VisualScriptPropertySet::ASSIGN_OP_BIT_OR: {
  1252. value = Variant::evaluate(Variant::OP_BIT_OR, value, p_argument);
  1253. } break;
  1254. case VisualScriptPropertySet::ASSIGN_OP_BIT_XOR: {
  1255. value = Variant::evaluate(Variant::OP_BIT_XOR, value, p_argument);
  1256. } break;
  1257. default: {
  1258. }
  1259. }
  1260. if (index != StringName()) {
  1261. source.set_named(index, value, valid);
  1262. } else {
  1263. source = value;
  1264. }
  1265. }
  1266. }
  1267. 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) {
  1268. switch (call_mode) {
  1269. case VisualScriptPropertySet::CALL_MODE_SELF: {
  1270. Object *object = instance->get_owner_ptr();
  1271. bool valid;
  1272. if (needs_get) {
  1273. Variant value = object->get(property, &valid);
  1274. _process_get(value, *p_inputs[0], valid);
  1275. object->set(property, value, &valid);
  1276. } else {
  1277. object->set(property, *p_inputs[0], &valid);
  1278. }
  1279. if (!valid) {
  1280. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1281. r_error_str = "Invalid set value '" + String(*p_inputs[0]) + "' on property '" + String(property) + "' of type " + object->get_class();
  1282. }
  1283. } break;
  1284. case VisualScriptPropertySet::CALL_MODE_NODE_PATH: {
  1285. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1286. if (!node) {
  1287. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1288. r_error_str = "Base object is not a Node!";
  1289. return 0;
  1290. }
  1291. Node *another = node->get_node(node_path);
  1292. if (!another) {
  1293. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1294. r_error_str = "Path does not lead Node!";
  1295. return 0;
  1296. }
  1297. bool valid;
  1298. if (needs_get) {
  1299. Variant value = another->get(property, &valid);
  1300. _process_get(value, *p_inputs[0], valid);
  1301. another->set(property, value, &valid);
  1302. } else {
  1303. another->set(property, *p_inputs[0], &valid);
  1304. }
  1305. if (!valid) {
  1306. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1307. r_error_str = "Invalid set value '" + String(*p_inputs[0]) + "' on property '" + String(property) + "' of type " + another->get_class();
  1308. }
  1309. } break;
  1310. case VisualScriptPropertySet::CALL_MODE_INSTANCE:
  1311. case VisualScriptPropertySet::CALL_MODE_BASIC_TYPE: {
  1312. Variant v = *p_inputs[0];
  1313. bool valid;
  1314. if (needs_get) {
  1315. Variant value = v.get_named(property, valid);
  1316. _process_get(value, *p_inputs[1], valid);
  1317. v.set_named(property, value, valid);
  1318. } else {
  1319. v.set_named(property, *p_inputs[1], valid);
  1320. }
  1321. if (!valid) {
  1322. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1323. 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());
  1324. }
  1325. *p_outputs[0] = v;
  1326. } break;
  1327. }
  1328. return 0;
  1329. }
  1330. };
  1331. VisualScriptNodeInstance *VisualScriptPropertySet::instantiate(VisualScriptInstance *p_instance) {
  1332. VisualScriptNodeInstancePropertySet *instance = memnew(VisualScriptNodeInstancePropertySet);
  1333. instance->node = this;
  1334. instance->instance = p_instance;
  1335. instance->property = property;
  1336. instance->call_mode = call_mode;
  1337. instance->node_path = base_path;
  1338. instance->assign_op = assign_op;
  1339. instance->index = index;
  1340. instance->needs_get = index != StringName() || assign_op != ASSIGN_OP_NONE;
  1341. return instance;
  1342. }
  1343. VisualScriptPropertySet::TypeGuess VisualScriptPropertySet::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  1344. if (p_output == 0 && call_mode == CALL_MODE_INSTANCE) {
  1345. return p_inputs[0];
  1346. }
  1347. return VisualScriptNode::guess_output_type(p_inputs, p_output);
  1348. }
  1349. VisualScriptPropertySet::VisualScriptPropertySet() {
  1350. assign_op = ASSIGN_OP_NONE;
  1351. call_mode = CALL_MODE_SELF;
  1352. base_type = "Object";
  1353. basic_type = Variant::NIL;
  1354. }
  1355. template <VisualScriptPropertySet::CallMode cmode>
  1356. static Ref<VisualScriptNode> create_property_set_node(const String &p_name) {
  1357. Ref<VisualScriptPropertySet> node;
  1358. node.instantiate();
  1359. node->set_call_mode(cmode);
  1360. return node;
  1361. }
  1362. //////////////////////////////////////////
  1363. ////////////////GET//////////////////////
  1364. //////////////////////////////////////////
  1365. int VisualScriptPropertyGet::get_output_sequence_port_count() const {
  1366. return 0; // (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?0:1;
  1367. }
  1368. bool VisualScriptPropertyGet::has_input_sequence_port() const {
  1369. return false; //(call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?false:true;
  1370. }
  1371. void VisualScriptPropertyGet::_update_base_type() {
  1372. //cache it because this information may not be available on load
  1373. if (call_mode == CALL_MODE_NODE_PATH) {
  1374. Node *node = _get_base_node();
  1375. if (node) {
  1376. base_type = node->get_class();
  1377. }
  1378. } else if (call_mode == CALL_MODE_SELF) {
  1379. if (get_visual_script().is_valid()) {
  1380. base_type = get_visual_script()->get_instance_base_type();
  1381. }
  1382. }
  1383. }
  1384. Node *VisualScriptPropertyGet::_get_base_node() const {
  1385. #ifdef TOOLS_ENABLED
  1386. Ref<Script> script = get_visual_script();
  1387. if (!script.is_valid()) {
  1388. return nullptr;
  1389. }
  1390. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  1391. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  1392. if (!scene_tree) {
  1393. return nullptr;
  1394. }
  1395. Node *edited_scene = scene_tree->get_edited_scene_root();
  1396. if (!edited_scene) {
  1397. return nullptr;
  1398. }
  1399. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  1400. if (!script_node) {
  1401. return nullptr;
  1402. }
  1403. if (!script_node->has_node(base_path)) {
  1404. return nullptr;
  1405. }
  1406. Node *path_to = script_node->get_node(base_path);
  1407. return path_to;
  1408. #else
  1409. return nullptr;
  1410. #endif
  1411. }
  1412. StringName VisualScriptPropertyGet::_get_base_type() const {
  1413. if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1414. return get_visual_script()->get_instance_base_type();
  1415. } else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  1416. Node *path = _get_base_node();
  1417. if (path) {
  1418. return path->get_class();
  1419. }
  1420. }
  1421. return base_type;
  1422. }
  1423. int VisualScriptPropertyGet::get_input_value_port_count() const {
  1424. return (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 1 : 0;
  1425. }
  1426. int VisualScriptPropertyGet::get_output_value_port_count() const {
  1427. int pc = (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 2 : 1;
  1428. return pc;
  1429. }
  1430. String VisualScriptPropertyGet::get_output_sequence_port_text(int p_port) const {
  1431. return String();
  1432. }
  1433. PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const {
  1434. if (call_mode == CALL_MODE_INSTANCE || call_mode == CALL_MODE_BASIC_TYPE) {
  1435. if (p_idx == 0) {
  1436. PropertyInfo pi;
  1437. pi.type = (call_mode == CALL_MODE_INSTANCE ? Variant::OBJECT : basic_type);
  1438. pi.name = (call_mode == CALL_MODE_INSTANCE ? String("instance") : Variant::get_type_name(basic_type).to_lower());
  1439. return pi;
  1440. }
  1441. }
  1442. return PropertyInfo();
  1443. }
  1444. PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const {
  1445. if (call_mode == CALL_MODE_BASIC_TYPE && p_idx == 0) {
  1446. return PropertyInfo(basic_type, "pass");
  1447. } else if (call_mode == CALL_MODE_INSTANCE && p_idx == 0) {
  1448. return PropertyInfo(Variant::OBJECT, "pass", PROPERTY_HINT_TYPE_STRING, get_base_type());
  1449. } else {
  1450. List<PropertyInfo> props;
  1451. ClassDB::get_property_list(_get_base_type(), &props, false);
  1452. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  1453. if (E->get().name == property) {
  1454. PropertyInfo pinfo = PropertyInfo(E->get().type, String(property) + "." + String(index), E->get().hint, E->get().hint_string);
  1455. _adjust_input_index(pinfo);
  1456. return pinfo;
  1457. }
  1458. }
  1459. }
  1460. PropertyInfo pinfo = PropertyInfo(type_cache, "value");
  1461. _adjust_input_index(pinfo);
  1462. return pinfo;
  1463. }
  1464. String VisualScriptPropertyGet::get_caption() const {
  1465. String prop = String("Get ") + property;
  1466. if (index != StringName()) {
  1467. prop += "." + String(index);
  1468. }
  1469. return prop;
  1470. }
  1471. String VisualScriptPropertyGet::get_text() const {
  1472. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1473. return String("On ") + Variant::get_type_name(basic_type);
  1474. } else if (call_mode == CALL_MODE_INSTANCE) {
  1475. return String("On ") + base_type;
  1476. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1477. return " [" + String(base_path.simplified()) + "]";
  1478. } else {
  1479. return "On Self";
  1480. }
  1481. }
  1482. void VisualScriptPropertyGet::set_base_type(const StringName &p_type) {
  1483. if (base_type == p_type) {
  1484. return;
  1485. }
  1486. base_type = p_type;
  1487. notify_property_list_changed();
  1488. ports_changed_notify();
  1489. }
  1490. StringName VisualScriptPropertyGet::get_base_type() const {
  1491. return base_type;
  1492. }
  1493. void VisualScriptPropertyGet::set_base_script(const String &p_path) {
  1494. if (base_script == p_path) {
  1495. return;
  1496. }
  1497. base_script = p_path;
  1498. notify_property_list_changed();
  1499. ports_changed_notify();
  1500. }
  1501. String VisualScriptPropertyGet::get_base_script() const {
  1502. return base_script;
  1503. }
  1504. void VisualScriptPropertyGet::_update_cache() {
  1505. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1506. //not super efficient..
  1507. Variant v;
  1508. Callable::CallError ce;
  1509. Variant::construct(basic_type, v, nullptr, 0, ce);
  1510. List<PropertyInfo> pinfo;
  1511. v.get_property_list(&pinfo);
  1512. for (const PropertyInfo &E : pinfo) {
  1513. if (E.name == property) {
  1514. type_cache = E.type;
  1515. return;
  1516. }
  1517. }
  1518. } else {
  1519. StringName type;
  1520. Ref<Script> script;
  1521. Node *node = nullptr;
  1522. if (call_mode == CALL_MODE_NODE_PATH) {
  1523. node = _get_base_node();
  1524. if (node) {
  1525. type = node->get_class();
  1526. base_type = type; //cache, too
  1527. script = node->get_script();
  1528. }
  1529. } else if (call_mode == CALL_MODE_SELF) {
  1530. if (get_visual_script().is_valid()) {
  1531. type = get_visual_script()->get_instance_base_type();
  1532. base_type = type; //cache, too
  1533. script = get_visual_script();
  1534. }
  1535. } else if (call_mode == CALL_MODE_INSTANCE) {
  1536. type = base_type;
  1537. if (base_script != String()) {
  1538. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1539. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1540. }
  1541. if (ResourceCache::has(base_script)) {
  1542. script = Ref<Resource>(ResourceCache::get(base_script));
  1543. } else {
  1544. return;
  1545. }
  1546. }
  1547. }
  1548. bool valid = false;
  1549. Variant::Type type_ret;
  1550. type_ret = ClassDB::get_property_type(base_type, property, &valid);
  1551. if (valid) {
  1552. type_cache = type_ret;
  1553. return; //all dandy
  1554. }
  1555. if (node) {
  1556. Variant prop = node->get(property, &valid);
  1557. if (valid) {
  1558. type_cache = prop.get_type();
  1559. return; //all dandy again
  1560. }
  1561. }
  1562. if (script.is_valid()) {
  1563. type_ret = script->get_static_property_type(property, &valid);
  1564. if (valid) {
  1565. type_cache = type_ret;
  1566. return; //all dandy
  1567. }
  1568. }
  1569. }
  1570. }
  1571. void VisualScriptPropertyGet::set_property(const StringName &p_type) {
  1572. if (property == p_type) {
  1573. return;
  1574. }
  1575. property = p_type;
  1576. _update_cache();
  1577. notify_property_list_changed();
  1578. ports_changed_notify();
  1579. }
  1580. StringName VisualScriptPropertyGet::get_property() const {
  1581. return property;
  1582. }
  1583. void VisualScriptPropertyGet::set_base_path(const NodePath &p_type) {
  1584. if (base_path == p_type) {
  1585. return;
  1586. }
  1587. base_path = p_type;
  1588. notify_property_list_changed();
  1589. _update_base_type();
  1590. ports_changed_notify();
  1591. }
  1592. NodePath VisualScriptPropertyGet::get_base_path() const {
  1593. return base_path;
  1594. }
  1595. void VisualScriptPropertyGet::set_call_mode(CallMode p_mode) {
  1596. if (call_mode == p_mode) {
  1597. return;
  1598. }
  1599. call_mode = p_mode;
  1600. notify_property_list_changed();
  1601. _update_base_type();
  1602. ports_changed_notify();
  1603. }
  1604. VisualScriptPropertyGet::CallMode VisualScriptPropertyGet::get_call_mode() const {
  1605. return call_mode;
  1606. }
  1607. void VisualScriptPropertyGet::set_basic_type(Variant::Type p_type) {
  1608. if (basic_type == p_type) {
  1609. return;
  1610. }
  1611. basic_type = p_type;
  1612. notify_property_list_changed();
  1613. ports_changed_notify();
  1614. }
  1615. Variant::Type VisualScriptPropertyGet::get_basic_type() const {
  1616. return basic_type;
  1617. }
  1618. void VisualScriptPropertyGet::_set_type_cache(Variant::Type p_type) {
  1619. type_cache = p_type;
  1620. }
  1621. Variant::Type VisualScriptPropertyGet::_get_type_cache() const {
  1622. return type_cache;
  1623. }
  1624. void VisualScriptPropertyGet::_adjust_input_index(PropertyInfo &pinfo) const {
  1625. if (index != StringName()) {
  1626. Variant v;
  1627. Callable::CallError ce;
  1628. Variant::construct(pinfo.type, v, nullptr, 0, ce);
  1629. Variant i = v.get(index);
  1630. pinfo.type = i.get_type();
  1631. pinfo.name = String(property) + "." + index;
  1632. } else {
  1633. pinfo.name = String(property);
  1634. }
  1635. }
  1636. void VisualScriptPropertyGet::set_index(const StringName &p_type) {
  1637. if (index == p_type) {
  1638. return;
  1639. }
  1640. index = p_type;
  1641. _update_cache();
  1642. notify_property_list_changed();
  1643. ports_changed_notify();
  1644. }
  1645. StringName VisualScriptPropertyGet::get_index() const {
  1646. return index;
  1647. }
  1648. void VisualScriptPropertyGet::_validate_property(PropertyInfo &property) const {
  1649. if (property.name == "base_type") {
  1650. if (call_mode != CALL_MODE_INSTANCE) {
  1651. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  1652. }
  1653. }
  1654. if (property.name == "base_script") {
  1655. if (call_mode != CALL_MODE_INSTANCE) {
  1656. property.usage = PROPERTY_USAGE_NONE;
  1657. }
  1658. }
  1659. if (property.name == "basic_type") {
  1660. if (call_mode != CALL_MODE_BASIC_TYPE) {
  1661. property.usage = PROPERTY_USAGE_NONE;
  1662. }
  1663. }
  1664. if (property.name == "node_path") {
  1665. if (call_mode != CALL_MODE_NODE_PATH) {
  1666. property.usage = PROPERTY_USAGE_NONE;
  1667. } else {
  1668. Node *bnode = _get_base_node();
  1669. if (bnode) {
  1670. property.hint_string = bnode->get_path(); //convert to long string
  1671. }
  1672. }
  1673. }
  1674. if (property.name == "property") {
  1675. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1676. property.hint = PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1677. property.hint_string = Variant::get_type_name(basic_type);
  1678. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1679. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1680. property.hint_string = itos(get_visual_script()->get_instance_id());
  1681. } else if (call_mode == CALL_MODE_INSTANCE) {
  1682. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1683. property.hint_string = base_type;
  1684. if (base_script != String()) {
  1685. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1686. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1687. }
  1688. if (ResourceCache::has(base_script)) {
  1689. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  1690. if (script.is_valid()) {
  1691. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1692. property.hint_string = itos(script->get_instance_id());
  1693. }
  1694. }
  1695. }
  1696. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1697. Node *node = _get_base_node();
  1698. if (node) {
  1699. property.hint = PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1700. property.hint_string = itos(node->get_instance_id());
  1701. } else {
  1702. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1703. property.hint_string = get_base_type();
  1704. }
  1705. }
  1706. }
  1707. if (property.name == "index") {
  1708. Callable::CallError ce;
  1709. Variant v;
  1710. Variant::construct(type_cache, v, nullptr, 0, ce);
  1711. List<PropertyInfo> plist;
  1712. v.get_property_list(&plist);
  1713. String options = "";
  1714. for (const PropertyInfo &E : plist) {
  1715. options += "," + E.name;
  1716. }
  1717. property.hint = PROPERTY_HINT_ENUM;
  1718. property.hint_string = options;
  1719. property.type = Variant::STRING;
  1720. if (options == "") {
  1721. property.usage = PROPERTY_USAGE_NONE; //hide if type has no usable index
  1722. }
  1723. }
  1724. }
  1725. void VisualScriptPropertyGet::_bind_methods() {
  1726. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptPropertyGet::set_base_type);
  1727. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptPropertyGet::get_base_type);
  1728. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptPropertyGet::set_base_script);
  1729. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptPropertyGet::get_base_script);
  1730. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptPropertyGet::set_basic_type);
  1731. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptPropertyGet::get_basic_type);
  1732. ClassDB::bind_method(D_METHOD("_set_type_cache", "type_cache"), &VisualScriptPropertyGet::_set_type_cache);
  1733. ClassDB::bind_method(D_METHOD("_get_type_cache"), &VisualScriptPropertyGet::_get_type_cache);
  1734. ClassDB::bind_method(D_METHOD("set_property", "property"), &VisualScriptPropertyGet::set_property);
  1735. ClassDB::bind_method(D_METHOD("get_property"), &VisualScriptPropertyGet::get_property);
  1736. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptPropertyGet::set_call_mode);
  1737. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptPropertyGet::get_call_mode);
  1738. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptPropertyGet::set_base_path);
  1739. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptPropertyGet::get_base_path);
  1740. ClassDB::bind_method(D_METHOD("set_index", "index"), &VisualScriptPropertyGet::set_index);
  1741. ClassDB::bind_method(D_METHOD("get_index"), &VisualScriptPropertyGet::get_index);
  1742. String bt;
  1743. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1744. if (i > 0) {
  1745. bt += ",";
  1746. }
  1747. bt += Variant::get_type_name(Variant::Type(i));
  1748. }
  1749. List<String> script_extensions;
  1750. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1751. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1752. }
  1753. String script_ext_hint;
  1754. for (const String &E : script_extensions) {
  1755. if (script_ext_hint != String()) {
  1756. script_ext_hint += ",";
  1757. }
  1758. script_ext_hint += "." + E;
  1759. }
  1760. ADD_PROPERTY(PropertyInfo(Variant::INT, "set_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type"), "set_call_mode", "get_call_mode");
  1761. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1762. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  1763. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_type_cache", "_get_type_cache");
  1764. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  1765. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  1766. ADD_PROPERTY(PropertyInfo(Variant::STRING, "property"), "set_property", "get_property");
  1767. ADD_PROPERTY(PropertyInfo(Variant::STRING, "index", PROPERTY_HINT_ENUM), "set_index", "get_index");
  1768. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  1769. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  1770. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  1771. BIND_ENUM_CONSTANT(CALL_MODE_BASIC_TYPE);
  1772. }
  1773. class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
  1774. public:
  1775. VisualScriptPropertyGet::CallMode call_mode;
  1776. NodePath node_path;
  1777. StringName property;
  1778. StringName index;
  1779. VisualScriptPropertyGet *node;
  1780. VisualScriptInstance *instance;
  1781. 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) {
  1782. switch (call_mode) {
  1783. case VisualScriptPropertyGet::CALL_MODE_SELF: {
  1784. Object *object = instance->get_owner_ptr();
  1785. bool valid;
  1786. *p_outputs[0] = object->get(property, &valid);
  1787. if (index != StringName()) {
  1788. *p_outputs[0] = p_outputs[0]->get_named(index, valid);
  1789. }
  1790. if (!valid) {
  1791. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1792. r_error_str = RTR("Invalid index property name.");
  1793. return 0;
  1794. }
  1795. } break;
  1796. case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
  1797. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1798. if (!node) {
  1799. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1800. r_error_str = RTR("Base object is not a Node!");
  1801. return 0;
  1802. }
  1803. Node *another = node->get_node(node_path);
  1804. if (!another) {
  1805. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1806. r_error_str = RTR("Path does not lead Node!");
  1807. return 0;
  1808. }
  1809. bool valid;
  1810. *p_outputs[0] = another->get(property, &valid);
  1811. if (index != StringName()) {
  1812. *p_outputs[0] = p_outputs[0]->get_named(index, valid);
  1813. }
  1814. if (!valid) {
  1815. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1816. r_error_str = vformat(RTR("Invalid index property name '%s' in node %s."), String(property), another->get_name());
  1817. return 0;
  1818. }
  1819. } break;
  1820. default: {
  1821. bool valid;
  1822. Variant v = *p_inputs[0];
  1823. *p_outputs[1] = v.get(property, &valid);
  1824. if (index != StringName()) {
  1825. *p_outputs[1] = p_outputs[1]->get_named(index, valid);
  1826. }
  1827. if (!valid) {
  1828. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1829. r_error_str = RTR("Invalid index property name.");
  1830. }
  1831. *p_outputs[0] = v;
  1832. };
  1833. }
  1834. return 0;
  1835. }
  1836. };
  1837. VisualScriptNodeInstance *VisualScriptPropertyGet::instantiate(VisualScriptInstance *p_instance) {
  1838. VisualScriptNodeInstancePropertyGet *instance = memnew(VisualScriptNodeInstancePropertyGet);
  1839. instance->node = this;
  1840. instance->instance = p_instance;
  1841. instance->property = property;
  1842. instance->call_mode = call_mode;
  1843. instance->node_path = base_path;
  1844. instance->index = index;
  1845. return instance;
  1846. }
  1847. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  1848. call_mode = CALL_MODE_SELF;
  1849. base_type = "Object";
  1850. basic_type = Variant::NIL;
  1851. type_cache = Variant::NIL;
  1852. }
  1853. template <VisualScriptPropertyGet::CallMode cmode>
  1854. static Ref<VisualScriptNode> create_property_get_node(const String &p_name) {
  1855. Ref<VisualScriptPropertyGet> node;
  1856. node.instantiate();
  1857. node->set_call_mode(cmode);
  1858. return node;
  1859. }
  1860. //////////////////////////////////////////
  1861. ////////////////EMIT//////////////////////
  1862. //////////////////////////////////////////
  1863. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1864. return 1;
  1865. }
  1866. bool VisualScriptEmitSignal::has_input_sequence_port() const {
  1867. return true;
  1868. }
  1869. int VisualScriptEmitSignal::get_input_value_port_count() const {
  1870. Ref<VisualScript> vs = get_visual_script();
  1871. if (vs.is_valid()) {
  1872. if (!vs->has_custom_signal(name)) {
  1873. return 0;
  1874. }
  1875. return vs->custom_signal_get_argument_count(name);
  1876. }
  1877. return 0;
  1878. }
  1879. int VisualScriptEmitSignal::get_output_value_port_count() const {
  1880. return 0;
  1881. }
  1882. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1883. return String();
  1884. }
  1885. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const {
  1886. Ref<VisualScript> vs = get_visual_script();
  1887. if (vs.is_valid()) {
  1888. if (!vs->has_custom_signal(name)) {
  1889. return PropertyInfo();
  1890. }
  1891. return PropertyInfo(vs->custom_signal_get_argument_type(name, p_idx), vs->custom_signal_get_argument_name(name, p_idx));
  1892. }
  1893. return PropertyInfo();
  1894. }
  1895. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const {
  1896. return PropertyInfo();
  1897. }
  1898. String VisualScriptEmitSignal::get_caption() const {
  1899. return "Emit " + String(name);
  1900. }
  1901. void VisualScriptEmitSignal::set_signal(const StringName &p_type) {
  1902. if (name == p_type) {
  1903. return;
  1904. }
  1905. name = p_type;
  1906. notify_property_list_changed();
  1907. ports_changed_notify();
  1908. }
  1909. StringName VisualScriptEmitSignal::get_signal() const {
  1910. return name;
  1911. }
  1912. void VisualScriptEmitSignal::_validate_property(PropertyInfo &property) const {
  1913. if (property.name == "signal") {
  1914. property.hint = PROPERTY_HINT_ENUM;
  1915. List<StringName> sigs;
  1916. Ref<VisualScript> vs = get_visual_script();
  1917. if (vs.is_valid()) {
  1918. vs->get_custom_signal_list(&sigs);
  1919. }
  1920. String ml;
  1921. for (const StringName &E : sigs) {
  1922. if (ml != String()) {
  1923. ml += ",";
  1924. }
  1925. ml += E;
  1926. }
  1927. property.hint_string = ml;
  1928. }
  1929. }
  1930. void VisualScriptEmitSignal::_bind_methods() {
  1931. ClassDB::bind_method(D_METHOD("set_signal", "name"), &VisualScriptEmitSignal::set_signal);
  1932. ClassDB::bind_method(D_METHOD("get_signal"), &VisualScriptEmitSignal::get_signal);
  1933. ADD_PROPERTY(PropertyInfo(Variant::STRING, "signal"), "set_signal", "get_signal");
  1934. }
  1935. class VisualScriptNodeInstanceEmitSignal : public VisualScriptNodeInstance {
  1936. public:
  1937. VisualScriptEmitSignal *node;
  1938. VisualScriptInstance *instance;
  1939. int argcount;
  1940. StringName name;
  1941. //virtual int get_working_memory_size() const { return 0; }
  1942. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1943. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1944. 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) {
  1945. Object *obj = instance->get_owner_ptr();
  1946. obj->emit_signal(name, p_inputs, argcount);
  1947. return 0;
  1948. }
  1949. };
  1950. VisualScriptNodeInstance *VisualScriptEmitSignal::instantiate(VisualScriptInstance *p_instance) {
  1951. VisualScriptNodeInstanceEmitSignal *instance = memnew(VisualScriptNodeInstanceEmitSignal);
  1952. instance->node = this;
  1953. instance->instance = p_instance;
  1954. instance->name = name;
  1955. instance->argcount = get_input_value_port_count();
  1956. return instance;
  1957. }
  1958. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1959. }
  1960. static Ref<VisualScriptNode> create_basic_type_call_node(const String &p_name) {
  1961. Vector<String> path = p_name.split("/");
  1962. ERR_FAIL_COND_V(path.size() < 4, Ref<VisualScriptNode>());
  1963. String base_type = path[2];
  1964. String method = path[3];
  1965. Ref<VisualScriptFunctionCall> node;
  1966. node.instantiate();
  1967. Variant::Type type = Variant::VARIANT_MAX;
  1968. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1969. if (Variant::get_type_name(Variant::Type(i)) == base_type) {
  1970. type = Variant::Type(i);
  1971. break;
  1972. }
  1973. }
  1974. ERR_FAIL_COND_V(type == Variant::VARIANT_MAX, Ref<VisualScriptNode>());
  1975. node->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1976. node->set_basic_type(type);
  1977. node->set_function(method);
  1978. return node;
  1979. }
  1980. void register_visual_script_func_nodes() {
  1981. VisualScriptLanguage::singleton->add_register_func("functions/call", create_node_generic<VisualScriptFunctionCall>);
  1982. VisualScriptLanguage::singleton->add_register_func("functions/set", create_node_generic<VisualScriptPropertySet>);
  1983. VisualScriptLanguage::singleton->add_register_func("functions/get", create_node_generic<VisualScriptPropertyGet>);
  1984. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_self",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  1985. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  1986. VisualScriptLanguage::singleton->add_register_func("functions/emit_signal", create_node_generic<VisualScriptEmitSignal>);
  1987. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1988. Variant::Type t = Variant::Type(i);
  1989. String type_name = Variant::get_type_name(t);
  1990. Callable::CallError ce;
  1991. Variant vt;
  1992. Variant::construct(t, vt, nullptr, 0, ce);
  1993. List<MethodInfo> ml;
  1994. vt.get_method_list(&ml);
  1995. for (const MethodInfo &E : ml) {
  1996. VisualScriptLanguage::singleton->add_register_func("functions/by_type/" + type_name + "/" + E.name, create_basic_type_call_node);
  1997. }
  1998. }
  1999. }