visual_script_func_nodes.cpp 71 KB

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