visual_script_func_nodes.cpp 72 KB

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